# SystemDebtEngine

The `SystemDebtEngine` contract is responsible for managing the system's surplus and bad debt. It acts as the central financial manager within a FXD system, specifically designed to handle scenarios where debts become unmanageable or when excess funds are accumulated beyond what is required for the system's stability.

#### Storage Variables

* `bookKeeper`: Reference to the `IBookKeeper` contract, which handles the ledger of all financial operations within the system.
* `surplusBuffer`: The minimum amount of surplus (in RAD, 10^45) that should always be maintained in the system to cover unexpected liabilities.
* `live`: A flag indicating whether the system is active (1) or has been shut down (0).

#### Functions

* `withdrawCollateralSurplus(bytes32 _collateralPoolId, address _to, uint256 _amount)`: Allows the owner to withdraw surplus collateral from a specific collateral pool.
* `withdrawStablecoinSurplus(address _to, uint256 _value)`: Allows the owner to withdraw surplus stablecoins from the system, provided there is no outstanding bad debt and the withdrawal does not drop the balance below the surplus buffer.
* `setSurplusBuffer(uint256 _data)`: Sets the minimum surplus buffer required in the system.
* `settleSystemBadDebt(uint256 _value)`: Allows anyone to settle bad debt using the surplus funds available in the system.
* `cage()`: Can be called by the owner or a designated emergency role to shut down the system in case of a significant issue. It settles as much bad debt as possible with available funds during the shutdown.
* `pause()`: Pauses all critical functionalities of the contract, preventing state-changing operations.
* `unpause()`: Resumes all operations after a pause.

#### Events

* `LogSetSurplusBuffer(address indexed _caller, uint256 _data)`: Emitted when the surplus buffer is updated.
* `LogCage()`: Emitted when the system is shut down.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fathom.fi/fxd-stablecoin/interfaces/systemdebtengine.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
