1

Suppose I have a pool that contains some coin say ETH. Imagine there are 100 users that benefit from this pool and I want to keep track of how much ETH in that pool is allocated to each user. When new rewards are sent to that pool (via a transaction) I want the allocation for each user to be updated.

Now when this transaction happens, to update allocations, I could keep track of the allocation of each user (via some mapping) and simply loop through all users and update the new allocation. This would be very gas inefficiant

I am looking for some suggestions that would make this work Thank you!!

qubitz
  • 287
  • 3
  • 9

1 Answers1

1

Looping through all users is not just very gas inefficient, it may become impossible once your contract has a lot of users and the looping consumes more gas than the block gas limit.

I would recommend checking out ERC-4626, a standard for a tokenized vaults.

Each depositor receives an amount of ERC-20 shares once they deposit the vault's asset tokens. The contract computes the amount of assets reedemable for the user's shares via a constant computation.

There exist ERC-4626 implementations from solmate or OpenZeppelin. For more info, also see erc4626.info.

Note that ERC-4626 does not support native tokens, eg ETH. However, you could just use the WETH token instead of ETH.

merkleplant
  • 128
  • 6