Using Hardhat
Creating and Deploying an ERC-20 Token on BitciChain with Node.js and Hardhat
Introduction
This guide will take you through the process of creating and deploying an ERC-20 token on the Ethereum blockchain using Node.js and Hardhat.
Prerequisites
Step 1: Set Up the Project
Create a new directory for your project.
Open a terminal and navigate to the project directory.
Run
npm init
to initialize a new Node.js project. Follow the prompts to set up yourpackage.json
file.
Step 2: Install Hardhat
Install Hardhat: Run
npm install --save-dev hardhat
.Run
npx hardhat
to create the Hardhat project structure. Follow the prompts to set up your Hardhat configuration.
Step 3: Write the ERC-20 Smart Contract
In the
contracts
directory, create a new file (e.g.,MyToken.sol
).Write the ERC-20 token smart contract code in Solidity. Below is a basic example:
// MyToken.sol
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor() ERC20("MyToken", "MTK") {
_mint(msg.sender, 1000000 * 10**18);
}
}
Step 4: Install OpenZeppelin Contracts
Run
npm install @openzeppelin/contracts
to install the OpenZeppelin Contracts library.
Step 5: Configure Hardhat
In the
hardhat.config.js
file, add bitcichain as a new network.
require('@nomiclabs/hardhat-waffle');
require('@openzeppelin/hardhat-upgrades');
module.exports = {
networks: {
bitcichain: {
chainId: 1907,
url: "https://rpc.bitci.com",
accounts: ["your-private-key"],
},
}
};
Step 6: Deploy the ERC-20 Token
In the
scripts
directory, create a new deployment script (e.g.,deploy.js
).
// deploy.js
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:
npx hardhat run scripts/deploy.js --network bitcichain
.
Step 7: Interact with the Deployed ERC-20 Token
Use the Hardhat console or create a separate script to interact with the deployed ERC-20 token.
// interact.js
async function main() {
const MyToken = await ethers.getContractFactory("MyToken");
const myToken = await MyToken.attach("0xYourDeployedTokenAddress");
const balance = await myToken.balanceOf("0xYourAddress");
console.log("Your balance:", balance.toString());
}
main().then(() => process.exit(0)).catch(error => {
console.error(error);
process.exit(1);
});
Run the interaction script:
npx hardhat run scripts/interact.js --network bitcichain
.
Congratulations! You have created and deployed an ERC-20 token on BitciChain using Node.js and Hardhat.
Additional Resources
Last updated