> ## Documentation Index
> Fetch the complete documentation index at: https://mantrachain.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 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).

> [!NOTE]
>
> **Hardhat Tutorial:**
> For new users, the [Hardhat tutorial](https://hardhat.org/getting-started/) provides step-by-step instructions and explains essential concepts for developing smart contracts.

## Prerequisites

### Node.js

Install Node.js following the [official documentation](https://nodejs.org/en/download). MANTRA Chain requires Node.js ≥ **18.0**, but we recommend using the latest LTS version (currently v22).

> [!NOTE]
>
> `nvm` is an excellent tool to easily manage multiple Node.js versions on your local machine. More details [here](https://github.com/nvm-sh/nvm)!

## Installation

Create a new Hardhat project:

```bash theme={null}
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:

```javascript theme={null}
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:

```bash theme={null}
npx hardhat compile
```

## Testing

Run tests:

```bash theme={null}
npx hardhat test
```

## Deploying

Create a deployment script in `scripts/deploy.js`:

```javascript theme={null}
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:

```bash theme={null}
npx hardhat run scripts/deploy.js --network mantra_testnet
```

## Hardhat Ignition (recommended for structured deployments)

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

1. Add a network entry (example using Hardhat config vars):

```javascript theme={null}
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],
    },
  },
};
```

2. Set the private key (interactive):

```bash theme={null}
npx hardhat vars set TEST_PRIVATE_KEY
```

3. Deploy an Ignition module:

```bash theme={null}
npx hardhat ignition deploy ./ignition/modules/Token.js --network mantra_testnet
```

## Example Project

Check out our [example project](https://github.com/MANTRA-Chain/tutorial) and follow the README for a complete walkthrough.

## Next Steps

* Learn about [deploying contracts](https://mantrachain.mintlify.site/developers/evm/deploying-contracts.md)
* Understand [contract verification](https://mantrachain.mintlify.site/developers/evm/verifying-contracts.md)
* Explore [EVM development](https://mantrachain.mintlify.site/developers/evm/getting-started.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.