threshold-network / token-dashboard

22 stars 23 forks source link

Staking rewards to be claim are calculated in a inefficient way #765

Open manumonti opened 2 months ago

manumonti commented 2 months ago

The issue

The dashboard calculates how many rewards are going to be received when a staker pushes the "Claim" button.

Currently, this calculation is made by checking the last updated Merkle Distribution file (rewards.json) and subtracting the number of rewards that have been claimed already.

To get the rewards already claimed, the dashboard checks the claimed events emitted by the contract to the Ethereum network.

https://github.com/threshold-network/token-dashboard/blob/fccb16fa5caf44d7b4729ea04fe215752114e526/src/hooks/useFetchStakingRewards.ts#L38

But checking these past events is not a reliable way to check this, since:

  1. It is quite inefficient and it is getting more inefficient over time: as new blocks are generated in the blockchain, the earlier events are left behind.
  2. Some providers can restrict the oldest events: maybe the provider returns only the events that happened in the last x Ethereum blocks.

The fix

Instead of querying for events, the claimed rewards of a stake can be queried directly to CumulativeClaimed in the Merkle distribution contract:

https://github.com/threshold-network/merkle-distribution/blob/252dabcbf6a252104218512ffd62d5e2272cb777/contracts/CumulativeMerkleDrop.sol#L20