Step 1
First weโre going to create a new folder and install hardhat:
Copy $ mkdir mock-token$ cd mock-token
$ npm init
$ npm install --save-dev hardhat
Now letโs bootstrap a hardhat project. Select the option "Create an empty hardhat.config.js":
Letโs also create a couple of folders for our files:
Copy $ mkdir scripts contracts
Now install some dependencies to our project:
Copy $ npm install --save-dev @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai
$ npm install --save-dev @nomiclabs/hardhat-etherscan
Step 2
Letโs now create a first Solidity contract at contracts/Token.sol :
Copy // SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MockToken is ERC20 {
constructor() ERC20("Mock Token", "Mock") {
_mint(msg.sender, 1000000 * 10**decimals());
}
}
We need to install openzeppelin library to our file:
Copy $ npm install @openzeppelin/contracts
Try to compile the contract with:
Copy $ npx hardhat compile
Step 3
We should prepare our hardhat.config.js file as:
Copy require("@nomiclabs/hardhat-etherscan");
require("@nomiclabs/hardhat-waffle");
const PRIVATE_KEY = "PRIVATEKEY";
module.exports = {
solidity: "0.8.2",
defaultNetwork: "fuji",
networks: {
mainnet: {
url: `https://api.avax.network/ext/bc/C/rpc`,
accounts: [PRIVATE_KEY]
},
fuji: {
url: `https://api.avax-test.network/ext/bc/C/rpc`,
accounts: [PRIVATE_KEY]
}
},
etherscan: {
apiKey: {
fuji: "avascan" // apiKey is not required, just set a placeholder
},
customChains: [
{
network: "fuji",
chainId: 43113,
urls: {
apiURL: "https://api.avascan.info/v2/network/testnet/evm/43113/etherscan",
browserURL: "https://testnet.avascan.info/blockchain/c"
}
}
]
},
};
Step 4
Now letโs add the deploy script. To keep it simple weโll simply reuse the deploy script from the Hardhat tutorial, so letโs create the file scripts/deploy.js :
Copy const { ethers } = require("hardhat");
async function main() {
const [deployer] = await ethers.getSigners();
const Token = await ethers.getContractFactory("MockToken");
const token = await Token.deploy();
console.log("Contract address:", token.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
After wrote our script, we can call deploy file:
Copy $ npx hardhat run scripts/deploy.js --network fuji
$ npx hardhat verify --contract contracts/Token.sol:MockToken --network fuji <contractaddress>