Section 10 of 15

Build
+15 Lynx

StabilityPool: ETH Gains via P/S Decay

Key takeaway: This section turns on the Stability Pool's liquidation absorption. offset is 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 product P decays by (1 - lossFraction) to shrink every depositor's balance proportionally. The sum S increases by (ETHGainPerLUSD × current P) so each depositor's pending ETH gain is initialDeposit × (S_current - S_snapshot) / P_snapshot / 1e18. The two formulas are the multiplicative and additive halves of the same scalable-reward pattern. With MIN_LUSD_IN_SP preventing drainage, the system runs forever — P is strictly positive, S grows monotonically within each scale, and depositors' balances are derivable from their single deposit-time snapshot.

What You Are Building

Three things wire up:

  1. offset(debtToOffset, collToAdd) — TroveManager calls this when a liquidation can be absorbed. The Stability Pool burns debtToOffset LUSD and receives collToAdd ETH.
  2. The decay update — inside offset, _computeRewardsPerUnitStaked and _updateRewardSumAndProduct mutate P and scaleToSum[currentScale].
  3. The lazy read pathsgetDepositorETHGain and _getETHGainFromSnapshots compute a depositor's pending ETH gain from their snapshot using the formula initialDeposit × (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

Solution.sol
Solidity
Loading editor...

Requirements

Write your implementation, then click Run Tests. Tests execute on the server.

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 continue

Already have an account? Log in