Hardhat
Hardhat
This is an example of deploying a contract using Hardhat.
1. Create a Hardhat project
Run the command below to initialize the Hardhat project.
mkdir erc20-project
cd erc20-project
npm init -y
npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox @nomicfoundation/hardhat-verify dotenv
npx hardhat
2. Write the contract code
Create a contracts
directory and save the ERC20 contract as ERC20.sol
file inside.
3. Configure the environment
Set the CROSS Chain RPC address and private key in the .env
file.
CHAIN_ID=612044
RPC_URL="https://testnet.cross-nexus.com:22001"
EXPLORER="https://testnet-explorer.cross-nexus.com"
EXPLORER_API="https://testnet-explorer.cross-nexus.com/api"
PRIVATE_KEY="<배포할 계정의 개인키 입력>"
4. Configure Hardhat configuration file
Configure the hardhat.config.js
file like below.
require("dotenv").config();
require("@nomicfoundation/hardhat-toolbox");
require("@nomicfoundation/hardhat-verify");
module.exports = {
solidity: "0.8.28",
networks: {
crossTestnet: {
url: process.env.RPC_URL,
accounts: [process.env.PRIVATE_KEY],
},
},
sourcify: {
enabled: true
},
etherscan: {
apiKey: {
crossTestnet: "DUMMY_API_KEY",
},
customChains: [
{
network: "crossTestnet",
chainId: process.env.CHAIN_ID,
urls: {
apiURL: process.env.EXPLORER_API,
browserURL: process.env.EXPLORER,
},
},
],
},
};
5. Compile the contract
Compile the contract with the following command
npx hardhat compile
6. Deploy the contract
Create a scripts
directory and create a scripts/deploy.js
file and execute the code below.
const { ethers } = require("hardhat");
async function main() {
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with account:", deployer.address);
const ERC20 = await ethers.getContractFactory("ERC20");
const erc20 = await ERC20.deploy("My ERC20", "M20", 18, ethers.parseUnits("1000000", 18));
await erc20.waitForDeployment();
console.log("ERC20 deployed at:", await erc20.getAddress());
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Deploy an ERC20 contract with the following command
npx hardhat run scripts/deploy.js --network crossTestnet
When the deployment is complete, the contract address will appear.
7. Verify the contract
Run the following command to verify the contract.
npx hardhat verify --network crossTestnet <deployed contract address> “My ERC20” “M20” 18 1000000000000000000000000
Note: Deployed contracts can be found in the CROSS Chain Testnet Explorer.
Updated 26 days ago