fix(stake): deposit-cap principal basis (#154) + clear deposit record on full withdraw (#155) - #172
Conversation
… on full withdraw (#155) #154 (MEDIUM): process_deposit / process_deposit_junior enforced the deposit cap against total_pool_value(), which is fee-inclusive for mode-1 trading pools. As fees accrued, the cap was hit and NEW deposits were locked out even though contributed principal was under the cap. Add StakePool::principal_tvl() (deposited − withdrawn − flushed + returned, NO fees) and enforce the cap on it. LP pricing still uses total_pool_value() (fee-inclusive). For mode-0 principal_tvl == total_pool_value. Unit test: test_154_*. #155 (HIGH): process_withdraw decremented deposit.lp_amount but never cleared is_initialized or the _reserved[8] tranche flag when the position reached zero. PERC-303's anti-mixing guard keys off those persisted fields, so a wallet that fully withdrew was permanently blocked from depositing into the OTHER tranche (junior↔senior). Reset is_initialized + _reserved[8] when lp_amount hits 0 so the (pool,user) PDA is reusable for either tranche. Safe vs PERC-303 (no residual position to mix when fully withdrawn); metadata-only. The deposit re-init paths correctly re-initialize a zeroed record (verified). Runtime e2e regression deferred to the OpenSSL-blocked LiteSVM suite (follow-up). NOT included: #157 (senior-stall) — on analysis it is NOT a reachable bug. A fee-enriched junior (jb > principal) always has jb > net_loss (net_loss ≤ flushed ≤ deposited ≤ principal), so junior (first-loss) always covers the loss and the gross_senior saturation never changes the outcome. Reverted the speculative change; flagged on the issue. Build-sbf clean; full suite green except the 4 pre-existing v17_stake_insurance_e2e failures. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughAdds ChangesPrincipal TVL deposit-cap enforcement and deposit PDA reuse
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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 |
Fixes #154 (deposit cap counted accrued fees → spurious lockout) and #155 (deposit-PDA tranche flag persisted after full withdrawal → wallet permanently blocked from the other tranche). See commit for the mechanism + safety analysis. #157 deliberately NOT fixed — on my own analysis it is not a reachable bug (a fee-enriched junior always covers any loss ≤ deposited, so the gross_senior saturation never affects the result); I reverted the speculative change and will note it on #157. Build-sbf clean; suite green except the 4 pre-existing
v17_stake_insurance_e2eLiteSVM failures.Summary by CodeRabbit
Release Notes
Bug Fixes
Tests