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)
$ 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)
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;
});