Hardhat

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.

1. Create Project Directory and Install Hardhat

$ mkdir meverse-test
$ cd meverse-test

To install Hardhat, go to empty folder and initialize npm (npm init)

$ npm install --save-dev hardhat

In Hardhat, you can create projects with Javascript and Typescript, and proceed smart contract compile, test and deployment. (This guideline used Javascript)

$ npx hardhat
888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "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.

$ cd contracts
$ touch MEVerseGreeter.sol
$ vi MEVerseGreeter.sol

Write the following code in MEVerseGreeter.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.5.6;
contract Mortal {
    /* Define Variable owner of the type address */
    address payable 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 */
    function kill() public payable { if (msg.sender == owner) selfdestruct(owner); }
}

contract MEVerseGreeter is Mortal {
    /* Define variable greeting of the type string */
    string greeting;
    /* This runs when the contract is executed */
    constructor (string memory _greeting) public {
        greeting = _greeting;
    }
    /* Main function */
    function greet() public view returns (string memory) {
        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.
const hre = require("hardhat");

async function main() {
  
  const MEVerseGreeter = await hre.ethers.getContractFactory("MEVerseGreeter");
  const mev = await MEVerseGreeter.deploy('Hello, MEVerse');

  console.log(
    `MEVerseGreeter deployed to ${mev.address}, \n - Transaction Hash : ${mev.deployTransaction.hash}`
  );
  console.log(
    `Smart construct deploy successful... MEVerseGreeter.greet() : ${await mev.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;
});

5. Deploy Smart Contract and Check

Deploy smart contract with following command.

$ npx hardhat run scripts/deploy.js --network MEVerseTestnet
MEVerseGreeter deployed to 0xe917d0B105a6305E0b011Ce2388E5D7797b90aff
 - Transaction Hash : 0x219fedab595ca2e0b4056cf7b15c6066a5e838a0b9a52caca982d2d22ece53f1
Smart contract deploy successful... MEVerseGreeter.greet() : Hello, MEVerse

To check the deployment, check contract Address in Console, and view in MEVerse Testnet Scan with tx hash.

Last updated