PageSourceSearch

https://elastos-wiki.netlify.app/assets/js/7fbc61c5.550e004e.js

js elastos-wiki.netlify.app collected 2026-10-03 10:43:25 UTC 11,036 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunk=self.webpackChunk||[]).push([[4651],{3905:(e,t,n)=>{n.d(t,{Zo:()=>p,kt:()=>m});var a=n(7294);function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function l(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(e,t){if(null==e)return{};var n,a,r=function(e,t){if(null==e)return{};var n,a,r={},o=Object.keys(e);for(a=0;a<o.length;a++)n=o[a],t.indexOf(n)>=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(a=0;a<o.length;a++)n=o[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}var s=a.createContext({}),c=function(e){var t=a.useContext(s),n=t;return e&&(n="function"==typeof e?e(t):l(l({},t),e)),n},p=function(e){var t=c(e.components);return a.createElement(s.Provider,{value:t},e.children)},u="mdxType",d={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},h=a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,o=e.originalType,s=e.parentName,p=i(e,["components","mdxType","originalType","parentName"]),u=c(n),h=r,m=u["".concat(s,".").concat(h)]||u[h]||d[h]||o;return n?a.createElement(m,l(l({ref:t},p),{},{components:n})):a.createElement(m,l({ref:t},p))}));function m(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var o=n.length,l=new Array(o);l[0]=h;var i={};for(var s in t)hasOwnProperty.call(t,s)&&(i[s]=t[s]);i.originalType=e,i[u]="string"==typeof e?e:r,l[1]=i;for(var c=2;c<o;c++)l[c]=n[c];return a.createElement.apply(null,l)}return a.createElement.apply(null,n)}h.displayName="MDXCreateElement"},1359:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>p,contentTitle:()=>s,default:()=>m,frontMatter:()=>i,metadata:()=>c,toc:()=>u});var a=n(7462),r=n(3366),o=(n(7294),n(3905)),l=["components"],i={title:"Deploy using Hardhat"},s=void 0,c={unversionedId:"develop/contracts/hardhat",id:"develop/contracts/hardhat",title:"Deploy using Hardhat",description:"Overview",source:"@site/../docs/3.develop/contracts/hardhat.md",sourceDirName:"3.develop/contracts",slug:"/develop/contracts/hardhat",permalink:"/develop/contracts/hardhat",draft:!1,editUrl:"https://github.com/elastos/Elastos.Wiki/edit/master/website/../docs/3.develop/contracts/hardhat.md",tags:[],version:"current",lastUpdatedBy:"racollette",lastUpdatedAt:1678891784,formattedLastUpdatedAt:"Mar 15, 2023",frontMatter:{title:"Deploy using Hardhat"},sidebar:"developSidebar",previous:{title:"Deploy using Remix",permalink:"/develop/contracts/remix"},next:{title:"Deploy using Truffle",permalink:"/develop/contracts/truffle"}},p={},u=[{value:"Overview",id:"overview",level:2},{value:"What you will learn",id:"what-you-will-learn",level:3},{value:"What you will do",id:"what-you-will-do",level:3},{value:"Setting up the development environment",id:"setting-up-the-development-environment",level:2},{value:"Creating a project",id:"creating-a-project",level:2},{value:"Sample project",id:"sample-project",level:3},{value:"Checking the contract",id:"checking-the-contract",level:3},{value:"Setting up the contract",id:"setting-up-the-contract",level:3},{value:"Compiling the contract",id:"compiling-the-contract",level:3},{value:"Testing the Contract",id:"testing-the-contract",level:3},{value:"Deploying on the Elastos testnet",id:"deploying-on-the-elastos-testnet",level:3}],d={toc:u},h="wrapper";function m(e){var t=e.components,n=(0,r.Z)(e,l);return(0,o.kt)(h,(0,a.Z)({},d,n,{components:t,mdxType:"MDXLayout"}),(0,o.kt)("h2",{id:"overview"},"Overview"),(0,o.kt)("p",null,"Hardhat is an Ethereum development environment that provides an easy way to deploy contracts, run tests and debug Solidity code locally."),(0,o.kt)("h3",{id:"what-you-will-learn"},"What you will learn"),(0,o.kt)("p",null,"In this tutorial, you will learn how to set up Hardhat and use it to build, test and deploy a simple smart contract."),(0,o.kt)("h3",{id:"what-you-will-do"},"What you will do"),(0,o.kt)("ul",null,(0,o.kt)("li",{parentName:"ul"},"Set up Hardhat"),(0,o.kt)("li",{parentName:"ul"},"Create a simple smart contract"),(0,o.kt)("li",{parentName:"ul"},"Compile contract"),(0,o.kt)("li",{parentName:"ul"},"Test contract"),(0,o.kt)("li",{parentName:"ul"},"Deploy contract")),(0,o.kt)("h2",{id:"setting-up-the-development-environment"},"Setting up the development environment"),(0,o.kt)("p",null,"There are a few technical requirements before we start. Please install the following:"),(0,o.kt)("ul",null,(0,o.kt)("li",{parentName:"ul"},(0,o.kt)("a",{parentName:"li",href:"https://nodejs.org/en/"}
1,"Node.js v10+ LTS and npm")," (comes with Node)"),(0,o.kt)("li",{parentName:"ul"},(0,o.kt)("a",{parentName:"li",href:"https://git-scm.com/"},"Git"))),(0,o.kt)("p",null,"Once we have those installed, you need to create an npm project by going to an empty folder, running ",(0,o.kt)("inlineCode",{parentName:"p"},"npm init"),", and following its instructions to install Hardhat. Once your project is ready, you should run:"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-bash"},"npm install --save-dev hardhat\n")),(0,o.kt)("p",null,"To create your Hardhat project, run ",(0,o.kt)("inlineCode",{parentName:"p"},"npx hardhat")," in your project folder.\nLet\u2019s create the sample project and go through these steps to try out a sample task and compile, test and deploy the sample contract."),(0,o.kt)("admonition",{type:"note"},(0,o.kt)("p",{parentName:"admonition"},"The sample project used here comes from the ",(0,o.kt)("a",{parentName:"p",href:"https://hardhat.org/getting-started/#quick-start"},(0,o.kt)("ins",null,"Hardhat Quickstart guide")),", as well as its instructions.")),(0,o.kt)("h2",{id:"creating-a-project"},"Creating a project"),(0,o.kt)("h3",{id:"sample-project"},"Sample project"),(0,o.kt)("p",null,"To create a sample project, run ",(0,o.kt)("inlineCode",{parentName:"p"},"npx hardhat")," in your project folder. You should see the following prompt:"),(0,o.kt)("p",null,"Choose the JavaScript project and go through these steps to compile, test and deploy the sample contract."),(0,o.kt)("h3",{id:"checking-the-contract"},"Checking the contract"),(0,o.kt)("p",null,"The ",(0,o.kt)("inlineCode",{parentName:"p"},"contracts")," folder contains ",(0,o.kt)("inlineCode",{parentName:"p"},"Lock.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."),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-js",metastring:'title="Lock.sol"',title:'"Lock.sol"'},'// SPDX-License-Identifier: UNLICENSED\npragma solidity ^0.8.0;\n\n// Import this file to use console.log\nimport "hardhat/console.sol";\n\ncontract Lock {\n    uint public unlockTime;\n    address payable public owner;\n\n    event Withdrawal(uint amount, uint when);\n\n    constructor(uint _unlockTime) payable {\n        require(\n            block.timestamp < _unlockTime,\n            "Unlock time should be in the future"\n        );\n\n        unlockTime = _unlockTime;\n        owner = payable(msg.sender);\n    }\n\n    function withdraw() public {\n        // Uncomment this line to print a log in your terminal\n        // console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);\n\n        require(block.timestamp >= unlockTime, "You can\'t withdraw yet");\n        require(msg.sender == owner, "You aren\'t the owner");\n\n        emit Withdrawal(address(this).balance, block.timestamp);\n\n        owner.transfer(address(this).balance);\n    }\n}\n')),(0,o.kt)("h3",{id:"setting-up-the-contract"},"Setting up the contract"),(0,o.kt)("ul",null,(0,o.kt)("li",{parentName:"ul"},"Go to ",(0,o.kt)("inlineCode",{parentName:"li"},"hardhat.config.js")),(0,o.kt)("li",{parentName:"ul"},"Update the ",(0,o.kt)("inlineCode",{parentName:"li"},"hardhat-config")," with elastos-network-credentials"),(0,o.kt)("li",{parentName:"ul"},"Create ",(0,o.kt)("inlineCode",{parentName:"li"},".env")," file in the root to store your private key")),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-js"},'require("dotenv").config();\nrequire("@nomiclabs/hardhat-ethers");\nrequire("@nomiclabs/hardhat-etherscan");\n\nmodule.exports = {\n  defaultNetwork: "esc_testnet",\n  networks: {\n    hardhat: {},\n    esc_testnet: {\n      url: "https://esc-testnet.elastos.io/api",\n      accounts: [process.env.PRIVATE_KEY],\n    },\n  },\n  solidity: {\n    version: "0.8.0",\n    settings: {\n      optimizer: {\n        enabled: true,\n        runs: 200,\n      },\n    },\n  },\n};\n')),(0,o.kt)("admonition",{type:"note"},(0,o.kt)("p",{parentName:"admonition"},"Note that the file above requires DOTENV, for managing environment variables and also ethers and etherscan. Make sure to install all those packages."),(0,o.kt)("p",{parentName:"admonition"},"Find more instructions on how to use DOTENV on ",(0,o.kt)("a",{parentName:"p",href:"https://www.npmjs.com/package/dotenv"},(0,o.kt)("ins",null,"this page")),".")),(0,o.kt)("h3",{id:"compiling-the-contract"},"Compiling the contract"),(0,o.kt)("p",null,"To compile the contract, you first need to install Hardhat Toolbox:"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-bash"},"npm install --save-dev @nomicfoundation/hardhat-toolbox\n")),(0,o.kt)("p",null,"Then, simply run to compile:"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-bash"},"npx hardhat compile\n")),(0,o.kt)("h3",{id:"testing-the-contract"},"Testing the Contract"),(0,o.kt)("p",null,"To run tests with Hardhat, you just need to type the following:"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-bash"},"npx hardhat test\n")),(0,o.kt)("p",null,"And this is an expected output:"),(0,o.kt)("h3",{id:"deploying-on-the-elastos-testnet"},"Deploying on the Elastos testnet"),(0,o.kt)("p",null,"Run this command in root of the project directory:"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-bash"},"npx hardhat run scripts/deploy.js --network esc_testnet\n")),(0,o.kt)("p",null,"The contract will be deployed on ESC, and 
1you can check the deployment status ",(0,o.kt)("a",{parentName:"p",href:"https://esc-testnet.elastos.io/"},"here"),"."),(0,o.kt)("p",null,(0,o.kt)("strong",{parentName:"p"},"Congratulations! You have successfully deployed Greeter Smart Contract. Now you can interact with the Smart Contract.")))}m.isMDXComponent=!0}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.