0

A bit of messing around with Hardhat and ERC20 deployments. I've deployed a created and tried to mint tokens for an account using this small snippet of code:

const ERC20 = await hre.ethers.getContractFactory("ERC20");
const deployUSDC = await ERC20.deploy("USD Coin", "USDC");
console.log("USDC token address: ", deployUSDC.address);

const mintToken = await deployUSDC._mint(deployer.address, { value: ethers.utils.parseEther("2048") })
console.log(mintToken.hash)

However I'm getting this error when trying to call the _mint function:

TypeError: deployUSDC._mint is not a function

I've also tried calling mint just in case, but I am getting the same error.

Anyone would a solution or an explanation of what I am doing wrong here please? So that I'd be able to mint tokens.

1
  • Do you have the mint function in the contract ?? Recheck and share the contract code as well if possible. Commented Sep 22, 2022 at 15:47

2 Answers 2

0

I'm pretty sure _mint is an internal function. It can't be called externally like you're doing, that's why it can't be seen.

In the solidity code you don't have a default mint function, but you can add it like this:

// import OpenZeppelin ERC20 here
// ...

contract MyToken is ERC20 {
    constructor(string memory name, string memory symbol) ERC20(name, symbol) {}

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

Now you can use it:

const ERC20 = await hre.ethers.getContractFactory("MyToken");
const deployUSDC = await ERC20.deploy("USD Coin", "USDC");
const mintToken = await deployUSDC.mint(deployer.address, ethers.utils.parseEther("2048"))

I also added the mint amount as a parameter instead of sending ETH.

0

As @0xSanson said, the _mint function probably doesn't have an external/public visibility and can't be called from outside the contract. Wrap it with a mint function which calls _mint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.