Easily deploy your contracts, run tests and debug Solidity code without dealing with live environments. Hardhat Network is a local Ethereum network designed for development.
To install Hardhat, go to empty folder and initialize npm (npm init)
$npminstall--save-devhardhat
In Hardhat, you can create projects with Javascript and Typescript, and proceed smart contract compile, test and deployment. (This guideline used Javascript)
$npxhardhat88888888888888888888888888888888888888888888888888888888888b.888d888.d8888888888b.8888b.888888888888"88b 888P"d88" 888 888 "88b"88b 888888 888 .d888888 888 888 888 888 888 .d888888 888888 888 888 888 888 Y88b 888 888 888 888 888 Y88b.888 888 "Y888888888"Y88888 888 888 "Y888888"Y888👷 Welcome to Hardhat v2.12.2 👷? What do you want to do? …❯ Create a JavaScript project Create a TypeScript project Create an empty hardhat.config.js Quit
2. Write Simple Smart Contract in Solidity
Create MEVerseGreeter.sol in meverse-test/contracts directory.
// SPDX-License-Identifier: MITpragmasolidity ^0.5.6;contract Mortal {/* Define Variable owner of the type address */addresspayable owner;/* This function is executed at initialization and sets the owner of the contract */constructor () public { owner = msg.sender; }/* Function to recover the funds on the contract */functionkill() publicpayable { if (msg.sender == owner) selfdestruct(owner); }}contractMEVerseGreeterisMortal {/* Define variable greeting of the type string */string greeting;/* This runs when the contract is executed */constructor (stringmemory_greeting) public { greeting = _greeting; }/* Main function */functiongreet() publicviewreturns (stringmemory) {return greeting; }}
Compile and test with following commands. (To proceed test, you need to write separate test script in test directory)
$ npx hardhat compile
$ npx hardhat test
3. To Connect MEVerse Testnet, Edit hardhat.config.js
Edit MEVerse Testnet network in hardhat.config.js
// hardhat.config.js...networks : { MEVerseTestnet: { url:"https://rpc.meversetestnet.io", chainId:4759,// MEVerse testnet chain id accounts: ["adf...583a9",// Private key of the account that will deploy contract ], timeout:1000000,// Unit milisec }}....
4. Write deploy.js to deploy
// We require the Hardhat Runtime Environment explicitly here. This is optional// but useful for running the script in a standalone fashion through `node <script>`.//// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat// will compile your contracts, add the Hardhat Runtime Environment's members to the// global scope, and execute the script.consthre=require("hardhat");asyncfunctionmain() {constMEVerseGreeter=awaithre.ethers.getContractFactory("MEVerseGreeter");constmev=awaitMEVerseGreeter.deploy('Hello, MEVerse');console.log(`MEVerseGreeter deployed to ${mev.address}, \n - Transaction Hash : ${mev.deployTransaction.hash}` );console.log(`Smart construct deploy successful... MEVerseGreeter.greet() : ${awaitmev.greet()}` );}// We recommend this pattern to be able to use async/await everywhere// and properly handle errors.main().catch((error) => {console.error(error);process.exitCode =1;});