 {"id":953,"date":"2023-01-04T05:11:53","date_gmt":"2023-01-03T19:11:53","guid":{"rendered":"https:\/\/test.secrypt.tech\/?page_id=953"},"modified":"2023-01-05T17:57:08","modified_gmt":"2023-01-05T07:57:08","slug":"using-hardhat","status":"publish","type":"page","link":"https:\/\/test.secrypt.tech\/index.php\/using-hardhat\/","title":{"rendered":"Using Hardhat"},"content":{"rendered":"<style>\/*! elementor - v3.9.2 - 21-12-2022 *\/\n.elementor-widget-image{text-align:center}.elementor-widget-image a{display:inline-block}.elementor-widget-image a img[src$=\".svg\"]{width:48px}.elementor-widget-image img{vertical-align:middle;display:inline-block}<\/style>\n<p>\t\t\t\t\t\t\t\t\t\t\t\t\t<a href=\"https:\/\/test.secrypt.tech\/\"><br \/>\n\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/test.secrypt.tech\/wp-content\/uploads\/elementor\/thumbs\/image-removebg-preview-pusb43b4fkgjv12hawycptkh3fmhfjngdaesxsjilk.png\" title=\"image-removebg-preview\" alt=\"image-removebg-preview\" \/>\t\t\t\t\t\t\t\t<\/a><\/p>\n<style>\/*! elementor - v3.9.2 - 21-12-2022 *\/\n.elementor-column .elementor-spacer-inner{height:var(--spacer-size)}.e-con{--container-widget-width:100%}.e-con-inner>.elementor-widget-spacer,.e-con>.elementor-widget-spacer{width:var(--container-widget-width,var(--spacer-size));--align-self:var(--container-widget-align-self,initial);--flex-shrink:0}.e-con-inner>.elementor-widget-spacer>.elementor-widget-container,.e-con-inner>.elementor-widget-spacer>.elementor-widget-container>.elementor-spacer,.e-con>.elementor-widget-spacer>.elementor-widget-container,.e-con>.elementor-widget-spacer>.elementor-widget-container>.elementor-spacer{height:100%}.e-con-inner>.elementor-widget-spacer>.elementor-widget-container>.elementor-spacer>.elementor-spacer-inner,.e-con>.elementor-widget-spacer>.elementor-widget-container>.elementor-spacer>.elementor-spacer-inner{height:var(--container-widget-height,var(--spacer-size))}<\/style>\n<style>\/*! elementor - v3.9.2 - 21-12-2022 *\/\n.elementor-heading-title{padding:0;margin:0;line-height:1}.elementor-widget-heading .elementor-heading-title[class*=elementor-size-]>a{color:inherit;font-size:inherit;line-height:inherit}.elementor-widget-heading .elementor-heading-title.elementor-size-small{font-size:15px}.elementor-widget-heading .elementor-heading-title.elementor-size-medium{font-size:19px}.elementor-widget-heading .elementor-heading-title.elementor-size-large{font-size:29px}.elementor-widget-heading .elementor-heading-title.elementor-size-xl{font-size:39px}.elementor-widget-heading .elementor-heading-title.elementor-size-xxl{font-size:59px}<\/style>\n<h2>Deploy a Smart Contract Using Hardhat<\/h2>\n<h4><strong>Overview<\/strong><\/h4>\n<p>Hardhat is an Ethereum development environment that provides an easy way to deploy smart contracts, run tests and debug Solidity code locally.<\/p>\n<p>In this tutorial, you will learn how to set up Hardhat and use it to build, test and deploy a simple smart contract.<\/p>\n<h4><strong>What you will do<\/strong><\/h4>\n<ul>\n<li>Set up Hardhat<\/li>\n<li>Create a simple smart contract<\/li>\n<li>Compile contract<\/li>\n<li>Test contract<\/li>\n<li>Deploy contract<\/li>\n<\/ul>\n<h4><strong>Setting up the development environment<\/strong><\/h4>\n<p>There are a few technical requirements before we start. Please install the following:<\/p>\n<ul>\n<li>Node.js v10+ LTS and npm\u00a0(comes with Node)<\/li>\n<li>Git<\/li>\n<\/ul>\n<p>Once we have those installed, you need to create an npm project by going to an empty folder, running\u00a0npm init, and following its instructions to install Hardhat. Once your project is ready, you should run:<\/p>\n<p>npm install &#8211;save-dev hardhat<\/p>\n<p>To create your Hardhat project, run\u00a0npx hardhat\u00a0in your project folder. Let\u2019s create the sample project and go through these steps to try out a sample task and compile, test and deploy the sample contract.<\/p>\n<p><b>NOTE<\/b><\/p>\n<p>The sample project used here comes from the\u00a0<u>Hardhat Quickstart guide<\/u>, as well as its instructions.<\/p>\n<h4><strong>Creating a project<\/strong><\/h4>\n<p>To create a sample project, run\u00a0npx hardhat\u00a0in your project folder. You should see the following prompt:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/test.secrypt.tech\/wp-content\/uploads\/2023\/01\/1-1-1024x527.png\" alt=\"\" width=\"1024\" height=\"527\" \/><\/p>\n<p>Choose the JavaScript project and go through these steps to compile, test and deploy the sample contract.<\/p>\n<h4><strong>Checking the contract<\/strong><\/h4>\n<p>The\u00a0contracts\u00a0folder contains\u00a0Lock.sol, which is a sample contract which consistis of a simple digital lock, where users could only withdraw funds after a given period of time.<\/p>\n<p><em>\/\/ SPDX-License-Identifier: UNLICENSED<\/em><br \/>pragma solidity ^0.8.9;<\/p>\n<p><em>\/\/ Import this file to use console.log<\/em><br \/>import &#8220;hardhat\/console.sol&#8221;;<\/p>\n<p>contract Lock {<br \/>\u00a0\u00a0\u00a0 uint public unlockTime;<br \/>\u00a0\u00a0\u00a0 address payable public owner;<\/p>\n<p>\u00a0\u00a0\u00a0 event Withdrawal(uint amount, uint when);<\/p>\n<p>\u00a0\u00a0\u00a0 constructor(uint _unlockTime) payable {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 require(<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 block.timestamp &lt; _unlockTime,<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;Unlock time should be in the future&#8221;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 );<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 unlockTime = _unlockTime;<br \/>\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0owner = payable(msg.sender);<br \/>\u00a0\u00a0\u00a0 }<\/p>\n<p>\u00a0\u00a0\u00a0 function withdraw() public {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>\/\/ Uncomment this line to print a log in your terminal<\/em><br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>\/\/ console.log(&#8220;Unlock time is %o and block timestamp is %o&#8221;, unlockTime, block.timestamp);<\/em><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 require(block.timestamp &gt;= unlockTime, &#8220;You can&#8217;t withdraw yet&#8221;);<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 require(msg.sender == owner, &#8220;You aren&#8217;t the owner&#8221;);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 emit Withdrawal(address(this).balance, block.timestamp);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 owner.transfer(address(this).balance);<br \/>\u00a0\u00a0\u00a0 }<br \/>}<\/p>\n<h4><strong>Setting up the contract<\/strong><strong>\u200b<\/strong><\/h4>\n<ul>\n<li>Go to\u00a0hardhat.config.js<\/li>\n<li>Update the\u00a0hardhat-config\u00a0with sxc-network-credentials<\/li>\n<li>Create\u00a0.env\u00a0file in the root to store your private key<\/li>\n<li>Add SECRYPT scan API key to\u00a0.env\u00a0file to verify the contract on SECRYPT scan. You can generate an API key by\u00a0creating an account<\/li>\n<\/ul>\n<p>require(&#8216;dotenv&#8217;).config();<br \/>require(&#8220;@nomiclabs\/hardhat-ethers&#8221;);<br \/>require(&#8220;@nomiclabs\/hardhat-etherscan&#8221;);<\/p>\n<p>module.exports = {<br \/>\u00a0 defaultNetwork: &#8220;sxc&#8221;,<br \/>\u00a0 networks: {<br \/>\u00a0\u00a0\u00a0 hardhat: {<br \/>\u00a0\u00a0\u00a0 },<br \/>\u00a0\u00a0\u00a0 secrypt_testnet: {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0 url: &#8220;http:\/\/ttv-wkqbnkghpr.dynamic-m.com:8543\/&#8221;,<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0 accounts: [process.env.PRIVATE_KEY]<br \/>\u00a0\u00a0\u00a0 }<br \/>\u00a0 },<br \/>\u00a0 etherscan: {<br \/>\u00a0\u00a0\u00a0 apiKey: process.env. SECRYPT SCAN_API_KEY<br \/>\u00a0 },<br \/>\u00a0 solidity: {<br \/>\u00a0\u00a0\u00a0 version: &#8220;0.8.9&#8221;,<br \/>\u00a0\u00a0\u00a0 settings: {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0 optimizer: {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 enabled: true,<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 runs: 200<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\u00a0\u00a0\u00a0 }<br \/>\u00a0 },<br \/>}<\/p>\n<p><b>NOTE<\/b><\/p>\n<p>Note that the file above requires DOTENV, for managing environment variables and also ethers and etherscan. Make sure to install all those packages.<\/p>\n<p>Find more instructions on how to use DOTENV on\u00a0<u>this page<\/u>.<\/p>\n<p>You can deploy on SXC(SECRYPT mainnet) if you change SECRYPT_Testnet by SXC<\/p>\n<h4><strong>Compiling the contract<\/strong><\/h4>\n<p>To compile the contract, you first need to install Hardhat Toolbox:<\/p>\n<p>npm install &#8211;save-dev @nomicfoundation\/hardhat-toolbox<\/p>\n<p>Then, simply run to compile:<\/p>\n<p>npx hardhat compile<\/p>\n<h4><strong>Testing the Contract<\/strong><\/h4>\n<p>To run tests with Hardhat, you just need to type the following:<\/p>\n<p>npx hardhat test<\/p>\n<p>And this is an expected output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/test.secrypt.tech\/wp-content\/uploads\/2023\/01\/2-1.png\" alt=\"\" width=\"979\" height=\"460\" \/><\/p>\n<h4><strong>Deploying on SECRYPT Network<\/strong><\/h4>\n<p>Run this command in root of the project directory:<\/p>\n<p>npx hardhat run scripts\/deploy.js &#8211;network secrypt_testnet<\/p>\n<p>The contract will be deployed on SXC&#8217;s SECRYPT Testnet, and you can check the deployment status here:\u00a0https:\/\/explorer-testnet.secrypt.tech\/tx<\/p>\n<p>Congratulations! You have successfully deployed Greeter Smart Contract. Now you can interact with the Smart Contract.<\/p>\n<p>QUICKLY VERIFY CONTRACTS ON SECRYPT SCAN<\/p>\n<p>Run the following commands to quickly verify your contract on SECRYPT scan. This makes it easy for anyone to see the source code of your deployed contract. For contracts that have a constructor with a complex argument list, see\u00a0<u>here<\/u>.<\/p>\n<p>npm install &#8211;save-dev @nomiclabs\/hardhat-etherscan<br \/>npx hardhat verify &#8211;network SECRYPT_testnet 0x4b75233D4FacbAa94264930aC26f9983e50C11AF<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Deploy a Smart Contract Using Hardhat Overview Hardhat is an Ethereum development environment that provides an easy way to deploy smart contracts, run tests and debug Solidity code locally. In this tutorial, you will learn how to set up Hardhat and use it to build, test and deploy a simple smart contract. What you will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"elementor_header_footer","meta":{"site-sidebar-layout":"no-sidebar","site-content-layout":"page-builder","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"disabled","ast-breadcrumbs-content":"","ast-featured-img":"disabled","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-953","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/test.secrypt.tech\/index.php\/wp-json\/wp\/v2\/pages\/953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/test.secrypt.tech\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/test.secrypt.tech\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/test.secrypt.tech\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/test.secrypt.tech\/index.php\/wp-json\/wp\/v2\/comments?post=953"}],"version-history":[{"count":14,"href":"https:\/\/test.secrypt.tech\/index.php\/wp-json\/wp\/v2\/pages\/953\/revisions"}],"predecessor-version":[{"id":1417,"href":"https:\/\/test.secrypt.tech\/index.php\/wp-json\/wp\/v2\/pages\/953\/revisions\/1417"}],"wp:attachment":[{"href":"https:\/\/test.secrypt.tech\/index.php\/wp-json\/wp\/v2\/media?parent=953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}