yearn / yearn-protocol

Yearn smart contracts
https://yearn.finance
GNU Affero General Public License v3.0
441 stars 211 forks source link

feat: fix and optimize 3CRV claim logic in StrategyProxy #103

Closed iamdefinitelyahuman closed 3 years ago

iamdefinitelyahuman commented 3 years ago

What I did

This PR fixes an issue within StrategyProxy where externally claimed 3CRV is not detected:

Additionally, there is a claim performed on every single call. This is inefficient for two reasons:

  1. Claiming only is possible once a week. The majority of these calls have no effect.
  2. When it is possible to claim, the user epoch only advances by 50 points per claim. Due to he number of locking / extending actions performed each week, there are usually >50 points to advance. In the past week alone there were over 400.

How I did it

The logical flow introduced in this PR works as such:

  1. FeeDistributor.time_cursor() is stored locally as lastTimeCursor. This variable is used within FeeDistributor to determine when new fees should be made claimable. At the start of each claim, if now < lastTimeCursor we can be certain there are no fees that can be claimed.
  2. FeeDistributor calculates claimable fees by iterating until last_token_time < time_cursor_of[acct]. We can claim in a loop until we see that this is condition is met, and be certain that all fees are claimed. By using claim_many we perform up to 20 "rounds" with each call.
  3. Finally, we use 3CRV.balanceOf to get the actual available balance. This way, even fees that were claimed via a third-party call will be seen.