Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pack BucketState struct into a single slot #645

Merged
merged 18 commits into from
Mar 4, 2023
Merged

Conversation

MikeHathaway
Copy link
Collaborator

Description of change

High level

  • Pack BucketState struct into a single slot for gas savings.
    • The new size supports up to 10^27 WADs in a single bucket which is substantially more than any known token's supply (which further wouldn't be all in one bucket)

Gas usage

Pre Change

| Function Name                                  | min             | avg    | median | max     | # calls |
| calculateRewards                               | 38552           | 89642  | 56275  | 244508  | 25      |
| claimRewards                                   | 611             | 110885 | 97346  | 471585  | 24      |
| getBucketStateStakeInfo                        | 750             | 4748   | 4750   | 4750    | 59112   |
| getStakeInfo                                   | 683             | 683    | 683    | 683     | 14      |
| stake                                          | 2386            | 512410 | 594112 | 1119518 | 26      |
| unstake                                        | 522             | 178296 | 145950 | 421024  | 12      |
| updateBucketExchangeRatesAndClaim              | 15883           | 311968 | 315432 | 529206  | 22      |

Post Change

| Function Name                                  | min             | avg    | median | max    | # calls |
| calculateRewards                               | 38474           | 48524  | 39389  | 242488 | 195     |
| claimRewards                                   | 611             | 110379 | 87749  | 469565 | 194     |
| getBucketStateStakeInfo                        | 682             | 2681   | 2682   | 2682   | 59112   |
| getStakeInfo                                   | 683             | 683    | 683    | 683    | 184     |
| stake                                          | 2386            | 315274 | 176596 | 901258 | 46      |
| unstake                                        | 522             | 177802 | 145362 | 420260 | 12      |
| updateBucketExchangeRatesAndClaim              | 15883           | 276726 | 239241 | 529206 | 30      |

Mike and others added 16 commits February 20, 2023 13:28
* Add Position struct to keep lps and deposit time of memorialized position. remove positionLPs and positionDepositTime mappings, keep only one positions mapping to Position struct
Emit events at the end of functions
Small gas improvements by reusing pool address / reading only once from storage
Rename RemoveLiquidityFailed error to RemovePositionFailed

* Approve and reset partial LPs
Consistent naming LPs, replaced all LP tokens occurences

* Change transferLps to allow partial transfer of deposit, transfer the min(approvedAmount, availableAmount), account bucket bankruptcy on new owner
Add tests

* changes after review - create IPool only once
Copy link
Contributor

@mattcushman mattcushman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. I don't see any downsides to limiting LPB to 127 bits.

@grandizzy
Copy link
Contributor

lgtm. I don't see any downsides to limiting LPB to 127 bits.

since we're packing this with bucket rate we could increase the uint128 for LPs and reduce the one for rate? (I suspect uint128 for rate is huge but we'd like to be safer for lps?)

Base automatically changed from bb-rewards-manager to develop March 4, 2023 21:28
Copy link
Contributor

@EdNoepel EdNoepel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description's comment about token supply doesn't seem relevant, because neither field stores a token balance. If exchange rates get too low, LPs will go to infinity. That said, we have bigger problems if the exchange rate goes to 0. Since this is isolated to the rewards contract, I'm fine with it.

@MikeHathaway MikeHathaway merged commit b00e3e5 into develop Mar 4, 2023
@MikeHathaway MikeHathaway deleted the packed-struct branch March 4, 2023 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants