Skip to content

Hardhat (EVM example) ​

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 commit 44c9e16 of 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

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:verify

Source files ​

contracts/Counter.sol ​

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.ts ​

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.js ​

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.ts ​

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.json ​

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 examples

Curated independently. Source-attributed reference for The Root Network. Not affiliated with Futureverse / TRN Labs.