1

I am looking at the ERC20 implementation from openzeppelin: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/ae1bafcb48fe220257d76bfd93a237db3ebaf3df/contracts/token/ERC20/ERC20.sol

It implements the protocol so that others can just extend this contract to build their own transfer logic. But the problem I am facing is that how to transfer funds at the first time. The code:

mapping(address account => uint256) private _balances;

initiate an empty map that all addresses' balance is 0. If anyone calls transfer method, it will throw ERC20InsufficientBalance error. How can I top up the balance for each accounts at the beginning?

1 Answer 1

1

You need to call the mint() function to mint new tokens. After the tokens are minted then the transfer function comes into play.

The mint function can be used in any way, for example, many ERC20 tokens have it inside the constructor itself, so that at the time of deployment, the tokens get minted into the provided address.

Or else you can call it manually after the contract is deployed.

2
  • the contract doesn't know all accounts' addresses. it only knows the owner's address. If _mint is called in constructor, it only grant balances to the own address. How can I mint for other addresses who use this contract? Feb 16 at 12:45
  • You can add a line, mint(msg.sender); , this will mint the tokens to the address that deploys the contract, or you can also give any other address you want to send the tokens. After getting the tokens you can transfer the tokens to all other address. Feb 17 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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