0

I noticed that balances as a mapping

mapping(address => uint) public balances;

and acts as a dictionary where addresses are mapped to balances.(address -> balance). For a typical dictionary to do so, we need to first provide/initialise the dictionary with some entries of address -> balance pair(s). But in solidity, its still able to fetch balances.

How is this accomplished ?

2 Answers 2

1

When the token contract is deployed, the mapping is empty - there is no data.

Often, during the deployment, some tokens are minted (created out of thin air) and assigned to the deployer's address. So an entry is added which maps the deployer's address to a uint balance. Basically it says "this address has this balance". Whenever the balance of any address is modified, the same mapping's uint value (for the given address) is modified.

When someone queries the balance for that address, the same mapping is used to retrieve the balance for the given address.

0

Well _transfer and _mint function (the 2 only way to get tokens) update the users balances

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.