EVM Development

Using Hardhat

Deploy and test smart contracts on MANTRA Chain using Hardhat

Hardhat is a development environment for Ethereum software that helps developers manage and automate recurring tasks involved in building smart contracts and decentralized applications (dApps).

Info

Hardhat Tutorial: For new users, the Hardhat tutorial provides step-by-step instructions and explains essential concepts for developing smart contracts.

Prerequisites

Node.js

Install Node.js following the official documentation. MANTRA Chain requires Node.js ≥ 18.0, but we recommend using the latest LTS version (currently v22).

Info

nvm is an excellent tool to easily manage multiple Node.js versions on your local machine. More details here!

Installation

Create a new Hardhat project:

mkdir my-project
cd my-project
npm init -y
npm install --save-dev hardhat
npx hardhat init

Follow the prompts to set up your project. Choose:

  • JavaScript or TypeScript project
  • Create a sample project to get started quickly

Configuring for MANTRA Chain

Update hardhat.config.js (or hardhat.config.ts) to add MANTRA Chain networks:

require("@nomicfoundation/hardhat-toolbox");

module.exports = {
  solidity: "0.8.23",
  networks: {
    mantra_mainnet: {
      url: "https://evm.mantrachain.io",
      chainId: 5888,
      accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : [],
    },
    mantra_testnet: {
      url: "https://evm.dukong.mantrachain.io",
      chainId: 5887,
      accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : [],
    },
  },
};

Building Contracts

Compile your contracts:

npx hardhat compile

Testing

Run tests:

npx hardhat test

Deploying

Create a deployment script in scripts/deploy.js:

async function main() {
  const MyContract = await ethers.getContractFactory("MyContract");
  const myContract = await MyContract.deploy();
  
  await myContract.waitForDeployment();
  console.log("Contract deployed to:", await myContract.getAddress());
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Deploy to testnet:

npx hardhat run scripts/deploy.js --network mantra_testnet

Hardhat Ignition is useful when you want repeatable deployments as “modules”.

  1. Add a network entry (example using Hardhat config vars):
require("@nomicfoundation/hardhat-toolbox");
const { vars } = require("hardhat/config");

const TEST_PRIVATE_KEY = vars.get("TEST_PRIVATE_KEY");

module.exports = {
  solidity: "0.8.23",
  networks: {
    mantra_testnet: {
      url: "https://evm.dukong.mantrachain.io",
      chainId: 5887,
      accounts: [TEST_PRIVATE_KEY],
    },
  },
};
  1. Set the private key (interactive):
npx hardhat vars set TEST_PRIVATE_KEY
  1. Deploy an Ignition module:
npx hardhat ignition deploy ./ignition/modules/Token.js --network mantra_testnet

Example Project

Check out our example project and follow the README for a complete walkthrough.

Next Steps