Section 10 of 15
StabilityPool: ETH Gains via P/S Decay
Key takeaway: This section turns on the Stability Pool's liquidation absorption.
offsetis the function TroveManager calls when a liquidation can be absorbed by the pool — it takes(_debtToOffset, _collToAdd)and updates two running terms in O(1). The productPdecays by(1 - lossFraction)to shrink every depositor's balance proportionally. The sumSincreases by(ETHGainPerLUSD × current P)so each depositor's pending ETH gain isinitialDeposit × (S_current - S_snapshot) / P_snapshot / 1e18. The two formulas are the multiplicative and additive halves of the same scalable-reward pattern. WithMIN_LUSD_IN_SPpreventing drainage, the system runs forever —Pis strictly positive,Sgrows monotonically within each scale, and depositors' balances are derivable from their single deposit-time snapshot.
What You Are Building
Three things wire up:
offset(debtToOffset, collToAdd)— TroveManager calls this when a liquidation can be absorbed. The Stability Pool burnsdebtToOffsetLUSD and receivescollToAddETH.- The decay update — inside
offset,_computeRewardsPerUnitStakedand_updateRewardSumAndProductmutatePandscaleToSum[currentScale]. - The lazy read paths —
getDepositorETHGainand_getETHGainFromSnapshotscompute a depositor's pending ETH gain from their snapshot using the formulainitialDeposit × (S_current - S_snapshot) / P_snapshot / 1e18.
Plus the supporting plumbing: the receive() for ETH coming in from ActivePool, _sendETHGainToDepositor to send a depositor their ETH on provideToSP or withdrawFromSP, and the error-correction variables lastETHError_Offset and lastLUSDLossError_Offset that preserve precision across many small 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