This Hardhat Starter Kit is designed to help you overcome version conflicts and how to configure Mantle Network in custom chain to address contract verification issues. If you've encountered problems with verifying your smart contracts on Mantle, this kit is here to simplify the process for you.
You have Node.js running on your computer, as well as yarn.
-
Clone this repository and navigate to it in your terminal.
git clone https://github.com/mantlenetworkio/mantle-tutorial.git cd mantle-tutorial/mantle-hardhat-starter-kit
-
Install the necessary packages.
yarn
-
Duplicate .env.example as .env.
cp .env.example .env
-
Edit the .env file to set the deployment parameters:
PRIVATE_KEY
: the hex private key for an account that has enough MNT for the deployment.ETHERSCAN_API_KEY
: Etherscan api key, you may retrieve from here.
In this section we go over the script line by line to learn how to use this starter kit to deploy and verify contract.
── mantle-hardhat-starter-kit
├── contracts
│ └── Greeter.sol
├── scripts
│ └── deploy.ts
└── hardhat.config.ts
The contracts directory is where you work on your smart contracts. For example, there's a simple Greeter contract provided:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
contract Greeter {
string public greeting = "Hello World!";
function setGreeting(string memory greeting_) public {
greeting = greeting_;
}
}
You can configure the deploy.ts script to suit your deployment needs, such as deploying a contract with arguments, proxy contracts, and more.
async function main() {
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy();
await greeter.deployed();
console.log("Greeter", greeter.address);
console.log(
`run: npx hardhat verify --network ${process.env.HARDHAT_NETWORK} ${greeter.address} to verify.`
);
}
In the hardhat.config.ts file, you'll find configurations for deploying to both the Mantle Mainnet and Mantle Testnet environments.
const config: HardhatUserConfig = {
solidity: "0.8.18",
networks: {
mantle: {
url: process.env.MANTLE_MAINNET_RPC_URL,
accounts: [process.env.PRIVATE_KEY!],
},
mantleTestnet: {
url: process.env.MANTLE_TESTNET_RPC_URL,
accounts: [process.env.PRIVATE_KEY!],
},
},
};
Once your deploy.ts is ready, run the following command from the mantle-hardhat-starter-kit root directory to deploy your smart contract to the Mantle Mainnet (or use mantleTestnet for Testnet):
npx hardhat --network mantle run scripts/deploy.ts
Compiled 1 Solidity file successfully
Greeter 0x8619b3cc4E7B7c5f6A3b6E981CEAd29678C6d03B
run: npx hardhat verify --network mantleTestnet 0x8619b3cc4E7B7c5f6A3b6E981CEAd29678C6d03B to verify.
As shown in the expected output above, this starter kit provides a predefined command to verify your contracts, let's try it out:
npx hardhat verify --network mantleTestnet 0x8619b3cc4E7B7c5f6A3b6E981CEAd29678C6d03B
Successfully submitted source code for contract
contracts/Greeter.sol:Greeter at 0x8619b3cc4E7B7c5f6A3b6E981CEAd29678C6d03B
for verification on the block explorer. Waiting for verification result...
Successfully verified contract Greeter on Etherscan.
https://explorer.testnet.mantle.xyz/address/0x8619b3cc4E7B7c5f6A3b6E981CEAd29678C6d03B#code
You should now be able to deploy and verify smart contracts on Mantle. For more guidance on configuring deploy different type of smart contracts in the deploy.ts file, refer to the Hardhat Docs.