I have some basic questions about implementing Tokens. I want to use the ERC20 standard, which defines the following interfaces:

  • balanceOf(address _owner) constant returns (uint256 balance)
  • transfer(address _to, uint256 _value) returns (bool success)

Therefore i must somehow save, which address has got how many tokens. I looked around the internet and they implement it alwasy with an array definded by "mapping (address => uint256) public balanceOf". I know how to implement the transfer(..) function with this array, but i still i have two questions:

  • The array balanceOf becomes bigger and bigger when more addresses own Tokens. Isn't there a risk, that the array becomes so big that i can't search users in it anymore because of the gas limit of a block?
  • The array balanceOf is some kind of user registration, which i don't want. Is there a way how i can send tokens directly to a user wallet? If yes, how would it look like in solidity?

Thanks for your help, DerBär

1 Answer 1

  • A mapping is similar to a hash table, so you should not run into issues regarding search/scaling as more users use your token. Unlike an array where you might need to search for an element, a mapping uses the key (address in this case) as a defined index for where the value (uint256 balance) will be located in memory.
  • A common misconception is that tokens are "stored in the user's wallet". This is not true. A user "owning" a token is exactly equivalent of the user having a non zero amount in the balanceOf mapping we reference above. Even when you are trying to check what tokens a user has, you need to first look at the address of the token in question, and then make a query in the context of that contract. There is no place (within Ethereum) where at the account level, you will get a list of all the tokens that user owns.

Also, you should probably not be reinventing the wheel in terms of implementing the ERC20 standard. Instead, use something like this from a trusted source.


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.