Skip to main content

Hardhat

Overview​

Hardhat is an Ethereum development environment. You can use Hardhat to compile your contracts and deploy the contracts to Soneium Minato.

Create a Hardhat Project​

To create a Hardhat project, follow these steps:

  1. Create your project directory:

    $ mkdir tutorial
    $ cd ./tutorial
  2. Initialize a pnpm project:

    $ pnpm init
  3. Install Hardhat:

    $ pnpm i -D hardhat
  4. Create the sample Hardhat project:

    $ npx hardhat init
  5. Select an option to create the project:

    888    888                      888 888               888
    888 888 888 888 888
    888 888 888 888 888
    8888888888 8888b. 888d888 .d88888 88888b. 8888b. 888888
    888 888 "88b 888P" d88" 888 888 "88b "88b 888
    888 888 .d888888 888 888 888 888 888 .d888888 888
    888 888 888 888 888 Y88b 888 888 888 888 888 Y88b.
    888 888 "Y888888 888 "Y88888 888 888 "Y888888 "Y888

    πŸ‘· Welcome to Hardhat v2.22.13 πŸ‘·β€

    ? What do you want to do? …
    Create a JavaScript project
    ❯ Create a TypeScript project
    Create a TypeScript project (with Viem)
    Create an empty hardhat.config.js
    Quit

    Now, the project includes a sample contract (contracts/Lock.sol) that locks funds until the timestamp provided as a parameter when the contract is deployed.

Setting Up the Deploy Configuration​

Update hardhat.config.ts file by adding Soneium Minato network information:

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

const PK = vars.get("PRIVATE_KEY");

const config: HardhatUserConfig = {
solidity: "0.8.27",
defaultNetwork: "minato",
networks: {
hardhat: {},
minato: {
url: "https://rpc.minato.soneium.org",
accounts: [PK],
},
},
etherscan: {
apiKey: {
minato: "NO API KEY",
},
customChains: [
{
network: "minato",
chainId: 1946,
urls: {
apiURL: "https://soneium-minato.blockscout.com/api",
browserURL: "https://soneium-minato.blockscout.com",
},
},
],
},
};

export default config;

Here, we use Hardhat’s built-in configuration variables manager to set the private key for the deployment account:

const PK = vars.get("PRIVATE_KEY");

To assigns a value to a configuration variable:

$ npx hardhat vars set PRIVATE_KEY

Hardhat recommends using its built-in configuration variables manager instead of dotenv. This minimizes the risk of accidentally exposing sensitive data by sharing .env files publicly. It also makes it easier to manage values across different Hardhat projects. You can find more commands here.

Deploying and Verifying the Contract​

Follow these steps to deploy and verify your contract:

  1. Compile the Contract:

    $ npx hardhat compile
  2. Deploy the Contract:

    We will deploy the contract using a Hardhat Ignition module:

    $ npx hardhat ignition deploy ./ignition/modules/Lock.ts
    βœ” Confirm deploy to network minato (1946)? … yes
    Compiled 1 Solidity file successfully (evm target: paris).
    Hardhat Ignition πŸš€

    Deploying [ LockModule ]

    Batch #1
    Executed LockModule#Lock

    [ LockModule ] successfully deployed πŸš€

    Deployed Addresses

    LockModule#Lock - 0x25b046FB5Af9d85264f89BcdD8229bfda01bAF89

    You will see the deployed contract address upon successful deployment. In this example, it’s 0x25b046FB5Af9d85264f89BcdD8229bfda01bAF89.

    tip

    If you encounter inconsistencies in the deployed environments (e.g., changing deployer accounts), you might need to run npx hardhat clean and manually delete the ignition/deployments folder.

  3. Verify the Contract:

    To verify the contract, input the contract address and the parameters used during deployment:

    $ npx hardhat verify [CONTRACT_ADDRESS] [CONTRACT_PARAMETERS]

    For this example:

    $ npx hardhat verify 0x25b046FB5Af9d85264f89BcdD8229bfda01bAF89 1893456000
    info

    In the ignition/modules/Lock.ts file, the unlock time is set as 1893456000 (1st Jan, 2030).

Contract Module Configuration​

Inside the ignition/modules/Lock.ts folder you will find a file with the following code. The contract parameters are specified in the 2nd parameter at m.contract method. For this example, the parameter is unlockTime:

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

const JAN_1ST_2030 = 1893456000;
const ONE_GWEI: bigint = 1_000_000_000n;

const LockModule = buildModule("LockModule", (m) => {
const unlockTime = m.getParameter("unlockTime", JAN_1ST_2030);
const lockedAmount = m.getParameter("lockedAmount", ONE_GWEI);

const lock = m.contract("Lock", [unlockTime], {
value: lockedAmount,
});

return { lock };
});

export default LockModule;

Finally, you can find the verified contract page here: Example Link