Skip to content

Implement release_stake and release_unbonded for delegations#138

Draft
ismellike wants to merge 10 commits into
mainfrom
delegations/release-stake
Draft

Implement release_stake and release_unbonded for delegations#138
ismellike wants to merge 10 commits into
mainfrom
delegations/release-stake

Conversation

@ismellike
Copy link
Copy Markdown
Collaborator

@ismellike ismellike commented Jan 8, 2025

closes #61

WIP - thinking in code for #128

@ismellike ismellike self-assigned this Jan 8, 2025
@ismellike
Copy link
Copy Markdown
Collaborator Author

ismellike commented Jan 8, 2025

How can we handle rewards with delegator stake lazy calculation? There's going to be a state mismatch since we can't call update_stake for all users of a delegate that has released stake: https://github.com/Lay3rLabs/commitments/blob/297b7d0869fe4e9004ce22b4d875d98ec1bc703a/packages/apis/src/rewards/state.rs#L160-L167

@ueco-jb
Copy link
Copy Markdown
Member

ueco-jb commented Jan 9, 2025

@ismellike

we can't call update_stake for all users of a delegate that has released stake

We could iterate over the keys of STAKED_AT using denom and delegate as prefixes and get a list of all of the stakers.
Or do you mean it's going to be too expensive for this unlucky user?

No need to filter_map staker_info anymore
Need historical access to staker delegations for processing of all release_stake requests
@ismellike
Copy link
Copy Markdown
Collaborator Author

ismellike commented Jan 9, 2025

@ismellike

we can't call update_stake for all users of a delegate that has released stake

We could iterate over the keys of STAKED_AT using denom and delegate as prefixes and get a list of all of the stakers. Or do you mean it's going to be too expensive for this unlucky user?

I meant every delegator needs their stake updated through the rewards calculator, because points_correction will not reflect the actual state. We cannot do this though, because we have an unbounded # of delegators per delegate.

Initial State (T0):
Delegate total_staked: 1000 tokens
points_per_share: 0
Stakers:
- A: stake 600, points_correction = 0, withdrawn_rewards = 0
- B: stake 400, points_correction = 0, withdrawn_rewards = 0

T1 (100 rewards distributed):
points_per_share = 100_000
Unclaimed rewards:
A: (600 * 100_000 - 0) / 1_000_000 = 60
B: (400 * 100_000 - 0) / 1_000_000 = 40

T2 (release_stake 500):
Total stake: 1000 -> 500
Without update_stake:
- A still recorded as 600
- B still recorded as 400
- points_corrections unchanged

T3 (C stakes 300):
Total: 500 -> 800
C: points_correction = 300 * 100_000 = 30_000_000

T4 (100 more rewards):
points_per_share = 225_000

Total unclaimed rewards would be:
A: (600 * 225_000 - 0) / 1_000_000 = 135 (60 from T1 + 75 from T3)
B: (400 * 225_000 - 0) / 1_000_000 = 90 (40 from T1 + 50 from T3)
C: (300 * 225_000 - 30_000_000) / 1_000_000 = 37.5 (just from T3)

Total rewards distributed would be: 262.5
But we only distributed 200 rewards (100 at T1 + 100 at T3)

Simplifies event releases to just use Vec<Coin>
@ismellike
Copy link
Copy Markdown
Collaborator Author

We also can't immediately normalize outflows, because this is defined over all denoms. addr -> outflows

TODO:
- more thorough tests
- test for rewards implications
- revise STAKER_INFO vs STAKED_AT
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.

Implement Release Stake on Delegation contract

2 participants