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:
-
Create your project directory:
$ mkdir tutorial
$ cd ./tutorial -
Initialize a pnpm project:
$ pnpm init
-
Install Hardhat:
$ pnpm i -D hardhat
-
Create the sample Hardhat project:
$ npx hardhat init
-
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
QuitNow, 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:
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:
-
Compile the Contract:
$ npx hardhat compile
-
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 - 0x25b046FB5Af9d85264f89BcdD8229bfda01bAF89You will see the deployed contract address upon successful deployment. In this example, itβs
0x25b046FB5Af9d85264f89BcdD8229bfda01bAF89
.tipIf you encounter inconsistencies in the deployed environments (e.g., changing deployer accounts), you might need to run
npx hardhat clean
and manually delete theignition/deployments
folder. -
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
infoIn the
ignition/modules/Lock.ts
file, the unlock time is set as1893456000
(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
:
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