fix: beneficiary reward share snapshot#210
Conversation
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis PR refactors staking rewards distribution to ensure deterministic pricing. The distribution function now snapshots total share and stake amounts once before distributing to beneficiaries, computes reward shares from that snapshot for each beneficiary, and mints via a shares-based helper instead of recalculating prices per iteration. ChangesDeterministic Price Snapshot for Rewards
🎯 2 (Simple) | ⏱️ ~12 minutes
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| let total_staked_near_amount = self.total_staked_near_amount; | ||
| for (account_id, bps) in hashmap.iter() { | ||
| let reward_near_amount: Balance = bps_mul(rewards, *bps); | ||
| let shares = Self::num_shares_from_staked_amount_rounded_down_with_totals( |
There was a problem hiding this comment.
We'd better add comments about why the change is needed.
Summary
This PR fixes beneficiary reward distribution to use a stable share-price snapshot during each reward distribution round.
Problem
When staking rewards are distributed to multiple beneficiaries, the previous logic calculated and minted shares one beneficiary at a time using the live total share
supply. Each mint increases total shares and changes the share price used by the next beneficiary, making the result depend on iteration order.
The current single-beneficiary behavior is already live in production and must remain fully compatible.
Fix
Snapshot
total_share_amountandtotal_staked_near_amountbefore iterating beneficiaries, then use that same snapshot to calculate every beneficiary mint in theround.
This keeps the existing single-beneficiary formula unchanged while preventing future multi-beneficiary order-dependent distribution.
Testing
CI runs the full test suite.
Summary by CodeRabbit
Bug Fixes
Tests