0

For using openzeppelin's ERC20 one can tansfer token info by constructor. But for token making by myself, it seems not work. I can use something like

ERC20(token address).burnfrom(...) 

for Public function in ERC20, but for private variables in ERC20, it does not work.

For example, I have made an ERC20 token "masaToken" at BSC testnet. The token address is 0x3114DbeAE60a6Ea05d04A8b361B261990b04606e.

// SPDX-License-Identifier: no license
pragma solidity ^0.8.0;

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

contract MyTokenContract is ERC20 {
    address MyToken = 0x3114DbeAE60a6Ea05d04A8b361B261990b04606e;

    constructor() ERC20("masaToken", "MASASET") {
    }


    function total1() public view returns (uint256){
        return totalSupply();
    }


    function total2() public view returns (uint256){

        return ERC20(MyToken).totalSupply();
    }
}

When I used above code to test it, I found that the correct token info cannot be transfered by constructor.The result of total1() is 0, but total2() is 1000000000000000000000.

0

To create your own ERC20 token and pass all the token info, you need to inherit from openzeppelins ERC20 token. It would look like this:

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

contract MyToken is ERC20 {

    constructor() ERC20("MyToken", "MYT") {

    }

}

Edit: When you inherit from ERC20, you can increase your total supply by minting new token.

If you want to have fixed totalSupply you have to implement IERC20 yourself and set your totalSupply property.

You can take a look at open-zeppelin ERC20 contract here: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol

2
  • @Masa I updated my answer, take a look.
    – ashhanai
    Oct 14 at 8:00
  • @ashhanai        Thank you very much. For totalSupply, I can use IERC20, but for other function that need token address such as burn(), and burnfrom() it seems not work. And there are not such function in IERC20.
    – Masa
    Oct 14 at 9:44

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.