Search code examples
ethereumsolidityerc20

Solidity - Add Total Supply


I'm learning solidity on remix I'm also referencing this open source api for token creation.

Right here they provide a _totalSupply() function that I'd like to wire to my smart contract so it shows the total amount of tokens why I deploy it.

What am doing wrong here?

pragma solidity ^0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";

contract Foobar is ERC20 {
    constructor(uint256 initialSupply) public ERC20("Foobar", "FOO") {
        _mint(msg.sender, initialSupply);
       // add totalSupply here
        _totalSupply(uint256 5000000000000000000000000000000000000000);
    }
}

Solution

  • OpenZeppelin ERC20 _totalSupply is a private property, which means you can't access it from a derived contract (in your case Foobar).

    Also your syntax is incorrect. If the property were at least internal, you could set its value as

    _totalSupply = 5000000000000000000000000000000000000000;
    

    or in a more readable way

    _totalSupply = 5 * 1e39;
    

    If you want to change its visibility, you'll need to copy the (parent) ERC20 contract to your IDE and change the import statement to reflect the new (local) location. Then you'll be able to update the property visibility in your local copy of the contract.

    Mind that the OpenZeppelin ERC20 contains relative import paths (e.g. import "./IERC20.sol";). You'll need to rewrite these in your local copy as well, so that they point back to the GitHub locations. Otherwise, the compiler would be trying to import non-existing local files.