In this tutorial, I will guide you through the process of creating and deploying your own ERC20 token using the Hardhat development environment.
Prerequisites:
Before we get started, make sure you have the following set up on your machine:
- Node.js and npm (Node Package Manager) installed
- Basic knowledge of Solidity programming language
- Familiarity with the Ethereum ecosystem and ERC20 tokens
Step 1: Setting Up the Project
- Create a new directory for your project and navigate to it in your terminal.
- Initialize a new Node.js project by running the command:
npm init -y
. - Install Hardhat as a development dependency by running:
npm install --save-dev hardhat
. - Open the project in your preferred code editor.
Step 2: Writing the ERC20 Token Contract
- Install the OpenZeppelin Contracts library, which provides pre-audited and secure contract implementations:
npm install @openzeppelin/contracts
. - Create a new file named
Zen.sol
in thecontracts
directory. - Add the following code to the
Zen.sol
file:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract Zen is ERC20 {
uint256 constant initialSupply = 1000000 * (10 ** 18);
constructor() ERC20("Zen", "ZEN") {
_mint(msg.sender, initialSupply);
}
function decimals() public pure override returns (uint8) {
return 18;
}
}
Step 3: Writing the Deployment Script
- Create a new directory named
scripts
in the project root. - Inside the
scripts
directory, create a new file nameddeploy.js
. - Add the following code to the
deploy.js
file:
const hre = require("hardhat");
const fs = require("fs");
async function main() {
const [deployer] = await hre.ethers.getSigners();
console.log("Deployer address:", deployer.address);
const zen = await hre.ethers.deployContract("Zen");
await zen.waitForDeployment();
const contractAddress = zen.target;
console.log("Contract Address:", contractAddress);
const zenArtifact = require("../artifacts/contracts/Zen.sol/Zen.json");
const zenAbi = zenArtifact.abi;
const contractData = {
address: contractAddress,
abi: zenAbi,
};
fs.writeFileSync("./abi/zen.json", JSON.stringify(contractData, null, 2));
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Step 4: Configuring Hardhat
- Open the
hardhat.config.js
file in the project root directory. - Update the file with the following configuration:
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
const { POLYGON_MUMBAI_RPC_PROVIDER, PRIVATE_KEY, POLYGONSCAN_API_KEY } = process.env;
module.exports = {
defaultNetwork: "hardhat",
networks: {
hardhat: {
chainId: 1337,
},
mumbai: {
url: POLYGON
_MUMBAI_RPC_PROVIDER,
accounts: [`0x${PRIVATE_KEY}`],
},
},
solidity: {
version: "0.8.9",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
etherscan: {
apiKey: POLYGONSCAN_API_KEY,
},
};
Step 5: Deploying the ERC20 Token
- Run the deployment script by executing the command:
npx hardhat run scripts/deploy.js --network <network>
. Replace<network>
with the desired network, such asmumbai
for the Polygon Mumbai testnet. - The script will deploy the
Zen
contract and output the deployer's address and the deployed contract address. - The contract's ABI will be saved to the
./abi/zen.json
file.
Conclusion:
Congratulations! You have successfully created and deployed your own ERC20 token using Hardhat. You can now use the contract address and ABI to interact with the token on the Ethereum network. ERC20 tokens are the building blocks for decentralized applications, enabling various use cases such as digital currencies, loyalty points, and more. Experiment with your token and explore the endless possibilities of decentralized finance.
Feel free to customize and extend the functionality of your ERC20 token contract according to your specific requirements. Happy coding!
Top comments (1)
in starting
is missing from steps.