Skip to main content

Deploy a smart contract on Monad using Hardhat

Hardhat is a comprehensive development environment consisting of different components for editing, compiling, debugging and deploying your smart contracts and dApps.

Requirements

Before you begin, you need to install the following dependencies:

  • npm (recommended >7) or yarn
  • Node.js v18.0.0 or later.
note

If you are on Windows, we strongly recommend using WSL 2 to follow this guide.

1. Create a sample Hardhat project

Create a new empty directory and navigate to it.

mkdir my-hardhat-project && cd my-hardhat-project

Initialize a new Hardhat project within the directory:

npx hardhat init

Select your preferences when prompted by the CLI or use the recommended preferences below.

✔ What do you want to do? · Create a TypeScript project (with Viem)
✔ Hardhat project root: · /path/to/my-hardhat-project
✔ Do you want to add a .gitignore? (Y/n) · y
✔ Do you want to install this sample project's dependencies with npm (hardhat @nomicfoundation/hardhat-toolbox-viem)? (Y/n) · y

2. Setting up configuration variables

A Hardhat project can use configuration variables for user-specific values or for data that shouldn't be included in the code repository.

note

Currently, the RPC is not public; this page will be updated as soon as it is.

We appreciate your patience.

To set a configuration variable you can use the following command:

npx hardhat vars set <variable_name>

For example, to set the MONAD_RPC_URL variable, you would enter the following command:

npx hardhat vars set MONAD_RPC_URL

Then enter the desired value for this variable at the prompt:

Enter value: ********************************

Similarly you can set up the PRIVATE_KEY and MONAD_CHAIN_ID variables.

warning

Configuration variables are stored in plain text on your disk. Avoid using this feature for data you wouldn’t normally save in an unencrypted file. Run npx hardhat vars path to find the storage's file location.

3. Update your hardhat.config.ts file to include the monadDevnet configuration

import type { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox-viem";
import { vars } from "hardhat/config";

const config: HardhatUserConfig = {
solidity: "0.8.27",
...
networks: {
...
monadDevnet: {
url: vars.get("MONAD_RPC_URL"),
accounts: [vars.get("PRIVATE_KEY")],
chainId: Number(vars.get("MONAD_CHAIN_ID")),
},
...
},
};

export default config;

4. Write a smart contract

You can create new contracts in the contracts directory.

In the below example I created GMonad.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract GMonad {
function sayGmonad() public pure returns (string memory) {
return "gmonad";
}
}

5. Compile the smart contract

npx hardhat compile

On successful compilation of smart contract, the output should be similar to the following:

Compiled 2 Solidity file successfully (evm target: paris).
note

2 files are compiled if you did not delete the default Lock.sol contract from Hardhat.

6. Deploy the smart contract

To deploy your contracts, you can use Hardhat Ignition, the declarative deployment system.

You can deploy the GMonad contract from the sample project by using its accompanying Ignition module.

Creating a Hardhat Ignition module

Create a file named GMonad.ts in the ignition/modules directory, with the following code:

import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

const GMonadModule = buildModule("GMonadModule", (m) => {
const gmonad = m.contract("GMonad");

return { gmonad };
});

module.exports = GMonadModule;

Now you can use the following command to deploy the contract.

Deploying the smart contract

npx hardhat ignition deploy ./ignition/modules/GMonad.ts --network monadDevnet

Choose yes when prompted to Confirm

✔ Confirm deploy to network monadDevnet (<chain_id>)? … yes

On successful deployment of smart contract, the output should be similar to the following:

✔ Confirm deploy to network monadDevnet (<chain_id>)? … yes
Hardhat Ignition 🚀

Deploying [ GMonadModule ]

Batch #1
Executed GMonadModule#GMonad

[ GMonadModule ] successfully deployed 🚀

Deployed Addresses

GMonadModule#GMonad - <contract_address>

Next Steps

Check out how to verify the deployed smart contract on Monad Explorer.