2.36 Hardhat (EVM example) At a glance What it is Deploy and verify a Solidity contract with hardhat-deploy. Futureverse’s EVM example for The Root Network, with its README and source. Where it sits Part II — Building · Chapter 2.36 Covers Examples · Source files Source github.com/futureversecom/trn-examples/tree/main/examples/evm/use-hardhat
| At a glance | |
|---|---|
| What it is | Deploy and verify a Solidity contract with hardhat-deploy. Futureverse’s EVM example for The Root Network, with its README and source. |
| Where it sits | Part II — Building · Chapter 2.36 |
| Covers | Examples · Source files |
| Source | github.com/futureversecom/trn-examples/tree/main/examples/evm/use-hardhat |
Deploy and verify a Solidity contract with hardhat-deploy. Reference: smart contract cookbook.
Copied from Futureverse’s
futureversecom/trn-examples, deleted with its GitHub organisation, at commit44c9e16of 2024-10-31. Apache-2.0: license. Before you run it, read Running the examples.
This project demonstrates a basic smart contract deployment and verification to Porcini using hardhat deploy plugin. It comes with a sample contract, a test for that contract, and a script that deploys that contract.
IMPORTANT
Ensure the following ENV vars are available before running the examples
CALLER_PRIVATE_KEY- Private key of an account that submits the transaction. Follow this guide to create and fund an account with some test tokens on Porcini (testnet) if you don't have one yet.
2.36.1 Examples
bash
# change your working directory to this example first
cd examples/substrate/use-hardhat
# export all required environments
export CALLER_PRIVATE_KEY=
# deploy contract using `hardhat deploy`
pnpm hardhat:deploy
# verify contract using `hardhat sourcify`
pnpm hardhat:verify2.36.2 Source files
contracts/Counter.sol2.36.2.1
solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;
contract Counter {
int256 count;
constructor(int256 _count) {
count = _count;
}
function increment() public {
count += 1;
}
function decrement() public {
count -= 1;
}
function getCount() public view returns (int256) {
return count;
}
}deploy/00_deploy_counter.ts2.36.2.2
ts
import type { DeployFunction } from "hardhat-deploy/types";
import type { HardhatRuntimeEnvironment } from "hardhat/types";
const func: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
const { getNamedAccounts, deployments } = hre;
const { deploy } = deployments;
const { deployer } = await getNamedAccounts();
if (!deployer) {
throw new Error("Missing named accounts");
}
await deploy("Counter", {
contract: "Counter",
from: deployer,
args: [0], // Optional: Use if your smart contract requires arguments
log: true,
});
};
func.tags = ["Counter"];
export default func;test/Counter.js2.36.2.3
js
const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers");
const { expect } = require("chai");
describe("Counter", function () {
async function deployFixture() {
const CounterFactory = await ethers.getContractFactory("Counter");
const initialCount = 0;
const counter = await CounterFactory.deploy(initialCount);
return { counter, initialCount };
}
describe("Deployment", function () {
it("Should set the correct initial count", async function () {
const { counter, initialCount } = await loadFixture(deployFixture);
expect(await counter.getCount()).to.equal(initialCount);
});
});
describe("Interaction", function () {
it("Should set increase the current count value by 1", async function () {
const { counter } = await loadFixture(deployFixture);
await counter.increment();
expect(await counter.getCount()).to.equal(1);
});
it("Should set decrease the current count value by 1", async function () {
const { counter } = await loadFixture(deployFixture);
await counter.decrement();
expect(await counter.getCount()).to.equal(-1);
});
});
});hardhat.config.ts2.36.2.4
ts
import "@nomicfoundation/hardhat-ethers";
import "@nomicfoundation/hardhat-toolbox";
import { getPublicProviderUrl } from "@therootnetwork/api";
import { cleanEnv, str } from "envalid";
import { Wallet } from "ethers";
import "hardhat-deploy";
import type { HardhatUserConfig } from "hardhat/config";
const env = cleanEnv(process.env, {
CALLER_PRIVATE_KEY: str(),
});
const wallet = new Wallet(env.CALLER_PRIVATE_KEY);
const config: HardhatUserConfig = {
solidity: {
version: "0.8.17",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
networks: {
porcini: {
url: getPublicProviderUrl("porcini", false),
chainId: 7672,
accounts: [env.CALLER_PRIVATE_KEY],
},
},
namedAccounts: {
deployer: wallet.address,
},
};
export default config;package.json2.36.2.5
json
{
"scripts": {
"hardhat:deploy": "hardhat deploy --network porcini",
"hardhat:verify": "hardhat sourcify --network porcini",
"hardhat:clean": "hardhat clean && rm -rf cache/",
"hardhat:test": "hardhat test"
},
"devDependencies": {
"@ethersproject/hash": "^5.7.0",
"@ethersproject/web": "^5.7.1",
"@nomicfoundation/ethereumjs-trie": "^6.0.1",
"@nomicfoundation/ethereumjs-util": "^9.0.1",
"@nomicfoundation/hardhat-chai-matchers": "^2.0.1",
"@nomicfoundation/hardhat-ethers": "^3.0.2",
"@nomicfoundation/hardhat-network-helpers": "^1.0.8",
"@nomicfoundation/hardhat-toolbox": "^3.0.0",
"@nomicfoundation/hardhat-verify": "^1.0.3",
"@therootnetwork/api": "^1.0.2",
"@typechain/hardhat": "^8.0.0",
"chai": "^4.3.7",
"dotenv": "^16.3.1",
"ethers": "^6.6.1",
"hardhat": "^2.16.0",
"hardhat-deploy": "^0.11.34",
"hardhat-gas-reporter": "^1.0.9",
"solidity-coverage": "^0.8.3"
}
}Source:
futureversecom/trn-examples, examples/evm/use-hardhat (deleted) · commit 44c9e16, 2024-10-31 · Apache-2.0, license · part of Code examplesAssociated docs
Where else you might go from here. Following one is optional — this chapter stands without them.
- Back matter — read in order
See also— The whole of Back matter as one continuous page, reprinted from its chapters without change.- Code examples
See also— Futureverse’s code examples for The Root Network: scripts for the native API and the EVM, and two contract projects, each with its README and source.- Contents
See also— Every chapter in this book, each with the one line that tells you what you get by reading it.- Front matter — read in order
See also— The whole of Front matter as one continuous page, reprinted from its chapters without change.- Guides for the code examples
See also— How Futureverse’s TRN code examples get a test account: create one in MetaMask and fund it from the Porcini faucet.- How do I deploy a smart contract on The Root Network using hardhat?
See also— The code below is the minimum hardhat configuration to deploy smart contracts on The Root Network.- Index · C
See also— 550 index terms that start with C, each linked to the pages that define or use it.- Index · D
See also— 493 index terms that start with D, each linked to the pages that define or use it.