Section 9 of 15
StabilityPool: Deposits and Compounded Balance
Key takeaway: The Stability Pool is the protocol's first line of defense against undercollateralized Troves. Depositors lock LUSD here; in exchange, they receive a share of every liquidated Trove's collateral. The accounting innovation: deposits are tracked with a multiplicative running product
Pand a per-scale sumscaleToSum. Each depositor's storage is a single snapshot taken at deposit time. Their current compounded balance is derived in O(1) asinitialValue × P_current / P_snapshot. Every liquidation updatesP(multiplied by1 - lossFraction) in a single write, and every depositor's balance shrinks proportionally without ever being touched. This is the cleanest example of scalable per-share reward distribution in DeFi.
What You Are Building
In this section you build the deposit side of the Stability Pool: provideToSP, withdrawFromSP, getCompoundedLUSDDeposit, and the storage that supports them. The ETH gain accounting (getDepositorETHGain, _updateRewardSumAndProduct, and the liquidation offset callback) lives in section 10.
For section 9, the ETH gain helper is stubbed to return 0, and P stays at its initial value 1e18 because no liquidations have happened yet. Even so, the deposit math is correct — compounded = initial × P / snapshot.P = initial × 1e18 / 1e18 = initial. Section 10 turns on the P decay and proves the formula works under liquidations.
Your Code
Requirements
Sign up free — keep reading + earn 15 Lynx
Zealynx Academy is free. Track your progress, earn Lynx, and climb the leaderboard.
Sign up free to continueAlready have an account? Log in