Skip to main content

Hardhat

Hardhat testing and deployment


Hardhatは、Ethereum仮想マシンの構築を容易にする優れたツールです。 これは、スマートコントラクトとdAppsの構築プロセスに固有の定期的なタスクを管理し、自動化するのに役立ちます。 このワークフローに関してより多くの機能を簡単に導入することができます これには、まさにコアでのコンパイルとテストが含まれます。 Flexible deployment options also allow you to point to the Autonomys EVM domain RPC to deploy your contracts and dApps.

Official documentation for Hardhat is available on their website, but this guide will cover everything required to get you started.

Prerequisites Make sure you have NodeJS version >=16.0 installed.

  1. 新しいターミナルを開き、これらのコマンドを実行して、プロジェクトの新しいフォルダーを作成します。
mkdir subspace-hardhat
cd subspace-hardhat
  1. 次に、以下に示すように npm プロジェクトを初期化します。 You'll be prompted to answer some questions.
npm install --save-dev hardhat
npm install --save-dev @openzeppelin/contracts
npx hardhat

Select "Create a JavaScript Project" from the list of the available options. Select project root folder and select to create a .gitignore file (optional).

Hardhat-1

  1. Right after you create your workspace, you will notice several folders. All of your contracts will reside inside the contracts folder, deployment scripts are available inside the scripts folder, and tests can be found inside the test folder. Click on the contracts folder and open Lock.sol.

Hardhat-3

  1. When in Lock.sol, you can change the name of your contract (in the example, to Counter), the name of the token (in this example, we're calling it AutonomysTestToken) and the token symbol (we're using TSSCtest).

Let’s add a simple smart contract that has three functions - setNumber(), increment() and decrement().

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

import '@openzeppelin/contracts/token/ERC20/ERC20.sol';

contract Counter is ERC20 {
constructor() ERC20("AutonomysTestToken", "TSSCtest") {}

uint256 public number;

function setNumber(uint256 newNumber) public {
number = newNumber;
}

function increment() public {
number++;
}

function decrement() public {
number--;
}
}

Let's also rename the filename to Counter.sol for consistency.

  1. Deploying a smart contract can be an expensive procedure due to the gas costs associated with the transaction. Hence, it’s advisable to thoroughly test the smart contracts for correctness before proceeding with deployment. To test the contract, open the tests folder and examine the Lock.js file created for us. Replace the internals of the file with the following code:
const { expect } = require("chai");

describe("Counter", function() {
let Counter;
let counter;
let owner;
let addr1;

beforeEach(async function() {
Counter = await ethers.getContractFactory("Counter");
[owner, addr1] = await ethers.getSigners();

counter = await Counter.deploy();
});

describe("Counter operations", function() {
it("Should return initial value of zero", async function() {
expect(await counter.number()).to.equal(0);
});

it("Should set number to a new value", async function() {
await counter.setNumber(5);
expect(await counter.number()).to.equal(5);
});

it("Should increment the number", async function() {
await counter.setNumber(5);
await counter.increment();
expect(await counter.number()).to.equal(6);
});

it("Should decrement the number", async function() {
await counter.setNumber(5);
await counter.decrement();
expect(await counter.number()).to.equal(4);
});
});
});

For consistency, let's also rename Lock.js to CounterTest.js

  1. To run the test, simply type npx hardhat test

Hardhat-4

Great, looks like everything is working as expected. We’re all set for the deployment!

  1. In order to deploy the contract, we need to set a deployment network for hardhat. Open hardhat.config.js file and add the subspace to the list of networks.
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: "0.8.19",
networks: {
subspace: {
url: "https://nova-0.gemini-3h.subspace.network/ws",
accounts: ["private_key_to_your_account"]
}
}
};
tip

Be careful to not commit hardhat.config.js file as it contain your private key. You can use NPM tools like dotenv to securely store your private keys in the .env file.

  1. Open deploy.js file and replace the content with the code.

Hardhat-5

const hre = require("hardhat");

async function main() {
const Contract = await hre.ethers.getContractFactory("Counter");
const contract = await Contract.deploy();

console.log("Contract deployed to:", contract.target);
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
  1. You're all set to deploy your smart contract on Autonomys Network! In order to deploy, run npx hardhat run scripts/deploy.js --network subspace.

This command will deploy your smart contract on the network we've just specified in hardhat.config.js file.

In case of success deployment, you should see Contract deployed to: transaction hash.

Hardhat-6

caution

In some cases when deploying the script, you may experience "No manual gas limit set" or "Gas estimation failed" issues. Please refer to this section for the solution.

  1. Congratulations, you've successfully deployed your smart contract on the Autonomys EVM domain!