0

I am trying to mint ERC20 tokens from another contract. I will add this contract address to the 'minter' role on the ERC20 contract.

I've noted below where I get the error. It's unclear to me why totalSupply() works but _mint(...) does not.

Also I am interested in why some functions on Open Zepplin contracts begin with an underscore.

Thank you !

pragma solidity ^0.8.3;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract claimTest {

    ERC20 public token;
    ERC20 public token2;
    address private tokenAddress;
    uint private fixedAmount;

    constructor () {
    }

    function setToken(ERC20 _token) public {
        token = _token;
    }

    function setAddress(address _address) public {
        tokenAddress = _address;
        token2 = ERC20(_address);
     }

    // Error
    // Member "mint" not found or not visible after argument-dependent lookup in contract ERC20
    function claim() public {
        token._mint(msg.sender, fixedAmount);
    }

    // This works
    function totalSupply()  public view returns (uint) {
        return token.totalSupply();
    }

}

1 Answer 1

1

_mint is an internal function, you need to inherit ERC20. If you want to create your own ERC20 token, you can do something like that. Note you need to inherit ERC20 like MyToken is ERC20.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
        
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

error MyToken__MintNotAuthorized(address caller);

contract MyToken is ERC20 {
    constructor(string memory name, string memory symbol) ERC20(name, symbol) {
        // Mint 100 tokens to msg.sender
        // 1 token = 1 * (10 ** decimals)
        _mint(msg.sender, 100 * 10**uint(decimals()));
    }

    // mint can be called by authorised caller to mint MyToken in existence.  
    function mint(address beneficiary, uint256 mintAmount) external {
        // Checks: the caller is the Authorised  caller
        if (msg.sender != "0xabc...") {
            revert MyToken__MintNotAuthorized(msg.sender);
        }
        
        // Effects: print the new MyTokens into existence.
        _mint(beneficiary, mintAmount);
            
    }
}

Check out this or this example to see how to create an ERC20 and how to call it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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