A step-by-step guide on how to create a simple ERC20 contract to mint 100 token
To create and deploy an ERC20 contract and mint at least 100 tokens using Hardhat, you can follow these step-by-step instructions:
Install Hardhat:
Make sure you have Node.js installed on your machine.
Open your terminal and run the following command to install Hardhat globally:
npm install -g hardhat
Create a new Hardhat project:
Create a new directory for your project and navigate into it.
Run the following command to initialize a new Hardhat project:
npx hardhat init
Write the ERC20 contract:
Open the
contracts
directory in your project and create a new file calledMyToken.sol
.Write the ERC20 contract code in
MyToken.sol
. Here's an example of a basic ERC20 contract:Solidity// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract MyToken is ERC20 { constructor() ERC20("MyToken", "MTK") { _mint(msg.sender, 100 * 10**18); // Mint 100 tokens to the contract deployer } }
Install OpenZeppelin Contracts:
Open your terminal and navigate to your project directory.
Run the following command to install OpenZeppelin Contracts:
npm install @openzeppelin/contracts
Configure Hardhat:
Open the
hardhat.config.js
file in your project.Uncomment the
require("@nomiclabs/hardhat-waffle");
line to enable the Waffle plugin.Add the following lines at the top of the file to import the necessary libraries:
JavaScriptrequire("@nomiclabs/hardhat-waffle"); require("@openzeppelin/hardhat-upgrades");
Deploy the contract:
Open the
scripts
directory in your project and create a new file calleddeploy.js
.Write the deployment script in
deploy.js
. Here's an example:JavaScriptconst { ethers } = require("hardhat"); async function main() { const MyToken = await ethers.getContractFactory("MyToken"); const myToken = await MyToken.deploy(); console.log("MyToken deployed to:", myToken.address); } main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); });
Run the deployment script:
Open your terminal and navigate to your project directory.
Run the following command to deploy the contract:
npx hardhat run scripts/deploy.js --network <network-name>
Replace
<network-name>
with the name of the network you want to deploy to (e.g.,localhost
for a local development network).
Verify the contract on Etherscan (optional):
After deploying the contract, you can verify it on Etherscan for transparency.
Follow the instructions provided by Etherscan to verify your contract.
That's it! You have now created and deployed an ERC20 contract using Hardhat and minted at least 100 tokens. Remember to customize the contract and deployment script according to your specific requirements.