2

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);
    }
}
Null isTrue
  • 1,882
  • 6
  • 26
  • 46

2 Answers2

2

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.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • thank you @Petr Hejda - that explains why I can't compile my code on BSC so it's visible under their contract tab. It does not recognize the imports. – Null isTrue Apr 28 '21 at 22:38
2

OpenZeppelin contracts automatically update totalSupply when you mint or burn tokens. They also automatically expose this as a variable you can read. You do not need, and you should not and you cannot set totalSupply by hand, because then the number of distributed tokens would not match the total supply.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435