0

I'm trying to allow all users to deposit tokens into my contract which I keep tracking of using a mapping mapping(address => uint256) public playerBalance; which is fantastic. But I later need to know all the users who have made a deposit (ie. an array of all addresses). The issue I am facing is potential gas problems by storing potentially hundreds/thousands of addresses in an array.

I need to loop over all users who made a deposit and check their balances to make a distribution of tokens to them. So I wanted to create an array that adds their address when they make a deposit and removes it when they withdraw from the array but is this going to be an expensive operation to do, is there a better way?

The issue is I'd be looping through a potentially large array whenever a user makes a withdrawal, and then again (once weekly) for the distribution.

1
  • One option is to create a token holders snapshot off-chain and then distribute the tokens in several transactions.
    – Ismael
    May 20 at 1:28

1 Answer 1

0

You should use the withdrawal pattern

The withdrawal pattern is also known as a pull-over-push pattern. In this pattern, ether or token transfer (push) from the contract is avoided; rather, the user is allowed to pull ether or token from the contract.

from the book Mastering Blockchain Programming with Solidity

enter image description here

the contract should maintain or update the balances of each user, so that they can withdraw the balance amount from the contract, as, in the preceding code, the balances mapping must be updated via other functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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