The advent of web3 has brought with it an ability to design tokens that act as a store of value or as virtual concurrency on the blockchain, the ERC20 is an Ethereum standard for creating fungible tokens, it provides the standard functionalities that should be incorporated to make it follow best practices. We'll be looking at how to set up and deploy a custom coin with the necessary attributes of an ERC20 token.
To get this done we'll need a decentralized wallet like Metamask and a web3 node provider like Alchemy. An Ethereum development environment like Truffle or hardhat plays an essential role in producing a smooth code-building experience we'll use Hardhat to write, configure and deploy our solidity code for this exercise.
Initializing our project
Make sure you have Node js installed on the system, create and navigate to the token project folder and run npm init to initialize the project
mkdir PokeCoin
cd PokeCoin
npm init -y
Install and create hardhat project
npm install --save-dev hardhat
npx hardhat
A welcome message and some options will be shown, go ahead to select ' Create a JavaScript Project'. Delete the default Lock.sol and Lock.js files in the contracts and test folders respectively.
Install the openzeppelin library with the below command to access the ERC20 standard that will be imported to create our token
npm i @openzeppelin/contracts
Under the contract folder create a file called PokeCoin.sol, this is where we'll write the smart contract that implements our PokeCoin token. The contract is quite self-explanatory, the first two lines specify the license type of the contract and the solidity compiler version to be used for the file, the ERC20 library is then imported and then immediately inherited by the PokeCoin contract, the token name and symbol are then loaded and then passed through the constructor parameters, we'll go ahead to use the base function mint in the ERC20 contract, this will mint the initial desired amount of token and send it to the creator's wallet.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract PokeCoin is ERC20{
uint constant _initial_supply= 100 * (10**18); // Initial deposit in creators wallet
constructor() ERC20("PokeCoin", "POK") { // Token description
_mint(msg.sender, _initial_supply);
}
}
Connect Wallet and Node Provider
We'll use Metamask as our wallet, and Alchemy as the node provider, our wallet's private key and the unique Alchemy project key, these will be stored and used in a .env file for the deployment and interaction of the contract. Follow these steps to get your Metamask private key and the below image for Alchemy
Make sure when committing this project to your git repository that your .env file is part of the .gitignore list of files and folders as the information there are quite sensitive and could be easily used by hackers to tamper with your accounts.
Create your .env file with this command..
npm install dotenv --save
and put the necessary keys in the .env file like this
API_URL = "https://eth-goerli.g.alchemy.com/v2/your-api-key"
PRIVATE_KEY = "your-metamask-private-key"
Install Ether.js and configure deployment
We'll use the hardhat plugin hardhat-ethers that is integrated with Ethers.js this allows for simple interactions with the Ethereum blockchain
npm install --save-dev @nomiclabs/hardhat-ethers "ethers@^5.0.0"
Then go ahead to configure the hardhat.config.js to require the ether library and the Goerli test network we're going to be using
require("@nomicfoundation/hardhat-toolbox");
require('dotenv').config();
require("@nomiclabs/hardhat-ethers");
const { API_URL, PRIVATE_KEY } = process.env;
module.exports = {
solidity: "0.8.17",
defaultNetwork: "goerli",
networks: {
hardhat: {},
goerli: {
url: API_URL,
accounts: [`0x${PRIVATE_KEY}`]
}
},
};
We can go ahead to compile the solidity file with the command
npx hardhat compile
Once this is successful, we'll configure our deployment script deploy.js to work like this
async function main() {
const [deployer]= await ethers.getSigners();
console.log("Deploying contracts with the account: ",deployer.address);
const weiAmount = (await deployer.getBalance()).toString();
console.log("Account balance:", (await ethers.utils.formatEther(weiAmount)));
const Token = await ethers.getContractFactory("Mikoin");
const token = await Token.deploy();
console.log("Token Address", token.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
}
)
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
Go ahead to deploy your contract to the goerli testnet
npx hardhat run scripts/deploy.js --network goerli
When the script is run successfully from the deployment script the console output logs the contract address, account balance, and token address. Congratulations! You can check out the record of the goerli transaction using the contract or token address using https://goerli.etherscan.io/. Your custom token has also been sent to your Metamask account. To see your token, use your token's contract address to import your token.
Awesome! What other token functionalities can you implement? Tinker with your smart contract and find out!
Top comments (0)