> ## Documentation Index
> Fetch the complete documentation index at: https://rootsfi.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Stability Pool

Explore the Stability Pool, the cornerstone of the Roots protocol's liquidation mechanism and a source of earning opportunities for MEAD depositors. This contract secures the system by absorbing debt from liquidated Troves and rewards participants with the liquidated collateral. Delve into its functions for managing deposits, claiming gains, and tracking rewards, alongside the events that signal its critical operations.

## Functions

### `DECIMAL_PRECISION()`

Returns the decimal precision used in calculations. (View)

```solidity theme={null}
function DECIMAL_PRECISION() -> uint256 [view]
```

### `P()`

Returns the product P, a factor used in calculating depositor rewards. (View)

```solidity theme={null}
function P() -> uint256 [view]
```

### `ROOTS_CORE()`

Returns the address of the `IRootsCore` contract. (View)

```solidity theme={null}
function ROOTS_CORE() -> address (contract IRootsCore) [view]
```

### `SCALE_FACTOR()`

Returns the scale factor used in reward calculations. (View)

```solidity theme={null}
function SCALE_FACTOR() -> uint256 [view]
```

### `SUNSET_DURATION()`

Returns the duration of the sunset period for collateral types. (View)

```solidity theme={null}
function SUNSET_DURATION() -> uint128 [view]
```

### `accountDeposits`

Returns deposit amounts for a specific account. (View)

```solidity theme={null}
function accountDeposits(_account: address) -> (uint128, uint128) [view]
```

### `claimCollateralGains`

Allows a depositor to claim their accrued collateral gains.

```solidity theme={null}
function claimCollateralGains(recipient: address, collateralIndexes: uint256[]) [nonpayable]
```

### `claimReward`

Allows a depositor to claim their MEAD rewards from staking in the Stability Pool.

```solidity theme={null}
function claimReward(recipient: address) -> uint256 [nonpayable]
```

### `claimableReward`

Returns the amount of MEAD reward a depositor can currently claim. (View)

```solidity theme={null}
function claimableReward(_depositor: address) -> uint256 [view]
```

### `collateralGainsByDepositor`

Returns collateral gains for a depositor by collateral index. (View)

```solidity theme={null}
function collateralGainsByDepositor(depositor: address, _index: uint256) -> uint80 [view]
```

### `collateralTokens`

Returns the address of a collateral token at a given index. (View)

```solidity theme={null}
function collateralTokens(_index: uint256) -> address (contract IERC20) [view]
```

### `currentEpoch()`

Returns the current epoch. (View)

```solidity theme={null}
function currentEpoch() -> uint128 [view]
```

### `currentScale()`

Returns the current scale. (View)

```solidity theme={null}
function currentScale() -> uint128 [view]
```

### `debtToken()`

Returns the address of the `IDebtToken` contract. (View)

```solidity theme={null}
function debtToken() -> address (contract IDebtToken) [view]
```

### `depositSnapshots`

Returns snapshot data for a specific depositor. (View)

```solidity theme={null}
function depositSnapshots(_depositor: address) -> (uint256, uint256, uint128, uint128) [view]
```

### `depositSums`

Returns deposit sums for an account and index. (View)

```solidity theme={null}
function depositSums(_account: address, _index: uint256) -> uint256 [view]
```

### `enableCollateral`

Enables a new collateral type to be managed by the Stability Pool.

```solidity theme={null}
function enableCollateral(_collateral: address (contract IERC20)) [nonpayable]
```

### `endCollateralSunset`

Ends the sunset period for a specific collateral index.

```solidity theme={null}
function endCollateralSunset(idx: uint256) [nonpayable]
```

### `epochToScaleToG`

Returns the G value (cumulative sum of collateral gains per unit of stake) for a given epoch and scale. (View)

```solidity theme={null}
function epochToScaleToG(_epoch: uint128, _scale: uint128) -> uint256 [view]
```

### `epochToScaleToSums`

Returns sum values related to epochs and scales for a specific index. (View)

```solidity theme={null}
function epochToScaleToSums(_epoch: uint128, _scale: uint128, _index: uint256) -> uint256 [view]
```

### `factory()`

Returns the factory address. (View)

```solidity theme={null}
function factory() -> address [view]
```

### `getCompoundedDebtDeposit`

Calculates and returns the compounded debt deposit for a depositor. (View)

```solidity theme={null}
function getCompoundedDebtDeposit(_depositor: address) -> uint256 [view]
```

### `getDepositorCollateralGain`

Returns all collateral gains for a specific depositor across all collateral types. (View)

```solidity theme={null}
function getDepositorCollateralGain(_depositor: address) -> uint256[] [view]
```

### `getTotalDebtTokenDeposits()`

Returns the total amount of MEAD (debt token) deposited in the Stability Pool. (View)

```solidity theme={null}
function getTotalDebtTokenDeposits() -> uint256 [view]
```

### `getWeek()`

Returns the current week number. (View)

```solidity theme={null}
function getWeek() -> uint256 [view]
```

### `guardian()`

Returns the guardian address. (View)

```solidity theme={null}
function guardian() -> address [view]
```

### `indexByCollateral`

Returns the index for a given collateral token address. (View)

```solidity theme={null}
function indexByCollateral(collateral: address (contract IERC20)) -> uint256 [view]
```

### `initialize`

Initializes the StabilityPool contract.

```solidity theme={null}
function initialize(_debtTokenAddress: address (contract IDebtToken), _factory: address, _liquidationManager: address, _scheduler: address) [nonpayable]
```

### `lastCollateralError_Offset`

Returns the last collateral error offset for a given index. (View)

```solidity theme={null}
function lastCollateralError_Offset(_index: uint256) -> uint256 [view]
```

### `lastDebtLossError_Offset()`

Returns the last debt loss error offset. (View)

```solidity theme={null}
function lastDebtLossError_Offset() -> uint256 [view]
```

### `lastRewardsError()`

Returns the last rewards error value. (View)

```solidity theme={null}
function lastRewardsError() -> uint256 [view]
```

### `lastUpdate()`

Returns the timestamp of the last update. (View)

```solidity theme={null}
function lastUpdate() -> uint32 [view]
```

### `liquidationManager()`

Returns the address of the Liquidation Manager contract. (View)

```solidity theme={null}
function liquidationManager() -> address [view]
```

### `offset`

Offsets debt with collateral gains when a Trove is liquidated.

```solidity theme={null}
function offset(collateral: address (contract IERC20), _debtToOffset: uint256, _collToAdd: uint256) [nonpayable]
```

### `owner()`

Returns the owner address of the contract. (View)

```solidity theme={null}
function owner() -> address [view]
```

### `periodFinish()`

Returns the timestamp when the current reward period finishes. (View)

```solidity theme={null}
function periodFinish() -> uint32 [view]
```

### `provideToSP`

Deposits a specified amount of MEAD into the Stability Pool.

```solidity theme={null}
function provideToSP(_amount: uint256) [nonpayable]
```

### `rewardRate()`

Returns the current reward rate. (View)

```solidity theme={null}
function rewardRate() -> uint128 [view]
```

### `scheduler()`

Returns the address of the `EmissionScheduler` contract. (View)

```solidity theme={null}
function scheduler() -> address (contract EmissionScheduler) [view]
```

### `startCollateralSunset`

Initiates the sunset period for a specific collateral type.

```solidity theme={null}
function startCollateralSunset(collateral: address (contract IERC20)) [nonpayable]
```

### `withdrawFromSP`

Withdraws a specified amount of MEAD from the Stability Pool.

```solidity theme={null}
function withdrawFromSP(_amount: uint256) [nonpayable]
```

## Events

### `CollateralGainWithdrawn`

Emitted when a depositor withdraws their collateral gains.

```solidity theme={null}
event CollateralGainWithdrawn(_depositor: address, _collateral: uint256[])
```

### `CollateralOverwritten`

Emitted when an old collateral token is overwritten with a new one.

```solidity theme={null}
event CollateralOverwritten(oldCollateral: address, newCollateral: address)
```

### `CollateralSunset`

Emitted when a collateral type starts its sunset period.

```solidity theme={null}
event CollateralSunset(collateral: address (contract IERC20), index: uint256)
```

### `DepositSnapshotUpdated` (Version 1)

Emitted when a depositor's snapshot (P and G values) is updated.

```solidity theme={null}
event DepositSnapshotUpdated(_depositor: address, _P: uint256, _G: uint256)
```

### `DepositSnapshotUpdated` (Version 2)

Emitted when a depositor's snapshot (P value) is updated.

```solidity theme={null}
event DepositSnapshotUpdated(_depositor: address, _P: uint256)
```

### `EpochUpdated`

Emitted when the current epoch is updated.

```solidity theme={null}
event EpochUpdated(_currentEpoch: uint128)
```

### `G_Updated`

Emitted when the G value (cumulative sum of collateral gains) is updated for an epoch and scale.

```solidity theme={null}
event G_Updated(_G: uint256, _epoch: uint128, _scale: uint128)
```

### `P_Updated`

Emitted when the P value (product factor for rewards) is updated.

```solidity theme={null}
event P_Updated(_P: uint256)
```

### `RewardClaimed`

Emitted when a user claims rewards.

```solidity theme={null}
event RewardClaimed(account: address, recipient: address, claimed: uint256)
```

### `S_Updated`

Emitted when the S value (sum of stakes with factor S) is updated for an index, epoch, and scale.

```solidity theme={null}
event S_Updated(idx: uint256, _S: uint256, _epoch: uint128, _scale: uint128)
```

### `ScaleUpdated`

Emitted when the current scale is updated.

```solidity theme={null}
event ScaleUpdated(_currentScale: uint128)
```

### `StabilityPoolDebtBalanceUpdated`

Emitted when the Stability Pool's total MEAD (debt token) balance is updated.

```solidity theme={null}
event StabilityPoolDebtBalanceUpdated(_newBalance: uint256)
```

### `UserDepositChanged`

Emitted when a user's MEAD deposit in the Stability Pool changes.

```solidity theme={null}
event UserDepositChanged(_depositor: address, _newDeposit: uint256)
```
