Skip to content

How to write and deploy a ERC20 token using Hardhat.js

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";

contract PCoin is ERC20, Ownable, ERC20Permit {
    constructor(address initialOwner)
        ERC20("PCoin", "P")
        Ownable(initialOwner)
        ERC20Permit("PCoin")
    {}

    function mint(address to, uint256 amount) public onlyOwner {
        _mint(to, amount);
    }
}

Lines for scripts/deploy.js

// Add in main function
const PCoin = await ethers.getContractFactory("PCoin");

const PCoinDeployed = await PCoin.deploy("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

let balance = await PCoinDeployed.balanceOf("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")

console.log(balance)

Testing Code

npm install
npx hardhat node
# NEW TAB
npx hardhat run ./scripts/deploy.sh --network localhost