Auto EVMHardHat Guide

Hardhat Guide

Hardhat is a tool that facilitates building, testing and deploying on the Ethereum Virtual Machine. It helps developers manage and automate the recurring tasks that are inherent to the process of building smart contracts and dApps, and allows them to easily introduce more functionality around this workflow. This includes compiling and testing at the very core. Flexible deployment options also allow you to point to the Autonomys EVM domain RPC to deploy your contracts and dApps.

EVM Version Compatibility

Auto EVM is compatible with most EVM versions but doesn’t support some features introduced in newer versions like “Paris” or “Shanghai”. When using development tools, you may need to specify an EVM version explicitly. Supported versions: “Istanbul”, “London”.

Getting started

Prerequisites

NodeJS version >=16.0 installed

  1. Open a new terminal and create a new folder for the project:
mkdir subspace-hardhat
cd subspace-hardhat
  1. Initialize an npm project:
npm install --save-dev hardhat
npm install --save-dev @openzeppelin/contracts
npx hardhat

You’ll be prompted to answer some questions. Select Create a JavaScript Project from the list of available options, select the project root folder, and, optionally, create a .gitignore file.

Hardhat-1

  1. In your created 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. Change the name of your contract in Lock.sol (Counter), the name of the token (AutonomysTestToken) and the token symbol (AI3test). As an example, 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", "TAI3test") {}

    uint256 public number;

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

    function increment() public {
        number++;
    }

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

For consistency, let’s also rename ‘Lock.sol’ to Counter.sol.

  1. Before proceeding with deployment, thoroughly test your smart contracts for correctness, as mistakes can lead to unforeseen gas costs. To test the contract, open the Lock.js file in the test folder, and 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, type npx hardhat test.

Hardhat-4

Everything is working as expected so we’re ready for deployment!

  1. To deploy the contract, we need to set a deployment network for hardhat. Open the hardhat.config.js file and add the Taurus testnet to the list of networks:
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: {
    compilers: [
        {
            version: "0.8.17",
            settings: {
            evmVersion: "london"
            }
        }
        ]
},
networks: {
    subspace: {
    url: "https://auto-evm.taurus.autonomys.xyz/ws",
    accounts: ["private_key_to_your_account"]
    }
}
};

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

  1. Open the deploy.js file and replace the contents with the code:
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;
});

Hardhat-5

  1. You’re now ready to deploy your smart contract on the Autonomys Network. 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 the hardhat.config.js file. If deployment is successful, you should see Contract deployed to: transaction hash.

Hardhat-6

Note: Do not tip when submitting transactions in an attempt to accelerate them as this could result in dual charges for gas fees. When deploying smart contracts to our Auto EVM domain, you may encounter an error related to gas estimation, typically presenting as: "No manual gas limit set" or "Gas estimation failed". For more information and solutions, visit the Auto EVM Introduction.

Congratulations, you’ve successfully deployed your smart contract on the Auto EVM!