-2

Hey i am unable to implement _totalSupply and _balances in Remix but able to use remaining functions like name() and decimal() and symbol() this is my code

pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract METoken is ERC20 {
constructor(uint256 initialSupply) ERC20 ("MAstering ther","MET")
{
    _totalSupply=initialSupply;
    _balances[msg.sender] = initialSupply;
    emit Transfer(address(0), msg.sender, initialSupply);
}
}

i am using ERC20 and i have seen from ERC20.sol file from Github that there are _totalSupply and other variables to use the totalSupply() method but i am not able to use them plz help!

  • what was the error you getting ? .. it seems like your code is working fine; – sms Aug 18 '22 at 12:41
  • i was getting undeclared identifier on both _totalSupply and _balances but then i used _mint function for setting the totalSupply but i am still unable to use _totalSupply maybe we cannot use them directly – Paras Kumar Aug 18 '22 at 14:04

1 Answers1

1

In the OpenZeppelin ERC20 implementation, that you're importing to your contract, properties _totalSupply and _balances have the private visibility.

From the Solidity docs page:

Private state variables are like internal ones but they are not visible in derived contracts.

So these specific properties (with private visibility) are not visible in your contract that derives from the contract that declares them.

Solution: The parent contract defines the _mint() function that effectively increases the balance of a selected address, the total supply, as well as emits the Transfer event.

constructor(uint256 initialSupply) ERC20 ("MAstering ther","MET")
{
    // increases `msg.sender`'s balance, total supply, and emits the `Transfer` event
    _mint(msg.sender, initialSupply);
}
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • Hey Peter thanks for your Answer i was hoping this answer but i thought maybe there is a way to change the values directly ✌ – Paras Kumar Aug 19 '22 at 03:12
  • @ParasKumar If you forked the OZ code and updated the properties visibility in your local fork, then you could change them directly. But that seems like a very long stretch and it might bring another types of issues... Why do you need to update the values directly? Maybe there's some other way (apart these that I just mentioned) more suitable to your use case. – Petr Hejda Aug 19 '22 at 07:31