0

I'm working on a smart contract in which we need to store some addresses along with their pending rewards periodically,

so the most first approach is to have a mapping of addresses to an int, like below:

mapping(address => uint256) pendingRewards;

The problem is that when you want to set/update all these data at once, it will cost a lot of gas to loop through the mapping and update them all.

so in other words, let's say I have a table of addresses and pending rewards, consider it's a huge huge table, what would be the best practice to set/update these data into a smart contract?

!! MerkleTree would not solve the problem because we can't retrieve pendingReward with the address as the key from that.

2
  • By the way, the Merkle Trees is the most common solution, you store the huge mapping offchain - Db, Excel, Json, etc, and users will claim rewards by providing their reward amount from offchain storage.
    – tenbits
    Commented Nov 29, 2023 at 19:51
  • thank you @tenbits this is actually what I was thinking about. also we can keep the totalClaimed in the contract, which will solve my issue. so merkleTree + offchain data is the solution!
    – S Arv
    Commented Nov 30, 2023 at 12:28

1 Answer 1

3

The problem is that when you want to set/update all these data at once, it will cost a lot of gas to loop through the mapping and update them all.

You need to design your smart contract logic in a way that there is no need to do this kind of batch update operations.

This is quite well understood problem. There are multiple well-known decentralised projects like Uniswap, Sushi, Balancer which you can study to understand how it is done.

3
  • could you please provide more detail on where exactly should I look for?
    – S Arv
    Commented Nov 29, 2023 at 19:49
  • Start with Sushiswap v2 source code and their reward pools. Commented Nov 30, 2023 at 4:16
  • i'm not looking for a reward pool or staking logic, you didn't get my question. but thank you anyways.
    – S Arv
    Commented Nov 30, 2023 at 12:26

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.