Skip to main content

Stability Module

Introduction

The StabilityModule contract is a sophisticated piece of Solidity code designed to facilitate the expansion and contraction of a system coin's supply, manage debt ceilings, and control deposits and withdrawals of collateral in a decentralized finance (DeFi) environment. It operates with a variety of adapters to interact with different DeFi protocols, enabling complex financial operations such as minting new coins, burning excess supply, and executing trades via delegate calls to adapters. The contract incorporates extensive error handling and emits events for significant actions, enhancing transparency and auditability.

Key Methods

Mutative Functions

expandAndBuy(bytes32 adapterName, bytes calldata data, uint256 mintAmount)

Allows the expansion of the system coin supply by minting coins and executing delegate function calls on approved adapters. This function is critical for engaging with external DeFi protocols to facilitate trades or other operations, leveraging the minted system coins.

contractAndSell(bytes32 adapterName, bytes calldata data)

Enables the contraction of the system coin supply by executing delegate function calls on approved adapters. This method allows the system to respond to market conditions, reducing the coin supply as necessary through interactions with external protocols.

deposit(uint256 amount)

Allows users to deposit approved collateral in exchange for system coins. This function is fundamental for adding liquidity to the system, with the collateral getting locked in the contract and the equivalent amount of system coins minted to the depositor's account.

Helper Functions

_payKeeper(uint256 equityDelta, address token)

Calculates and transfers a keeper fee based on the change in equity resulting from expansion or contraction operations. This incentivizes keepers to execute these operations.

_burnCoin(int256 currentDebt)

Burns any system coins present in the StabilityModule and reduces its debt by the burned amount. This is a mechanism to manage the system's monetary supply actively.

Access Control Functions

addAdapter(bytes32 adapterName_, address adapter_)

Adds a new adapter to the system, allowing the StabilityModule to interact with more external protocols via delegate calls. This function ensures that the system remains flexible and extensible.

changeTreasury(address newTreasury_)

Changes the treasury address, which is critical for redirecting the flow of funds (e.g., collected fees or liquidated collateral) to a new destination.

windDown()

Winds down the module by transferring all collateral to the treasury and minting system coins to cover any credits. This is an emergency or end-of-life operation.

changeMaxDeposit(uint256 newMaxDeposit)

Modifies the maximum total deposit threshold, allowing the system to adjust its risk exposure and liquidity requirements.

changeDebtCeiling(uint256 newDebtCeiling)

Adjusts the debt ceiling, which is a crucial parameter for managing the system's overall leverage and exposure.

changeBasisFee(uint256 newBasisFee)

Updates the basis fee, affecting the keeper fee calculation. This allows the system to adapt its incentive structures for operational activities.

Key Mechanisms and Concepts

Debt Management

The contract manages debt through a private variable _debt, which is adjusted during deposit operations, expansions, and contractions. The debt ceiling is a critical parameter that limits the maximum debt the system can accrue.

Collateral Handling

Collateral is managed through deposits and wind-down operations. The contract supports depositing approved collateral types, adjusting for scaling factors between different token decimals. The wind-down operation allows for the orderly removal of collateral from the system.

Expansion and Contraction

These operations are central to the Stability Module's functionality, allowing for the dynamic adjustment of the system coin's supply in response to market conditions. They involve minting or burning system coins and interacting with external DeFi protocols via adapters.

Keeper Incentives

Keepers are incentivized through fees paid for executing expansion and contraction operations. This mechanism ensures that the system remains responsive and efficient.

Adapters

Adapters are external contracts with which the Stability Module interacts to execute specific DeFi operations. They are a flexible way to extend the system's capabilities and integrate with the broader DeFi ecosystem.

Error Handling

The contract employs comprehensive error handling, using custom errors for different failure scenarios. This enhances the contract's security and reliability by providing clear feedback on operation failures.

Events

A wide range of events are emitted for critical operations, providing transparency and allowing for off-chain monitoring and analysis. These events cover activities such as adding adapters, changing parameters, deposits, expansions, contractions, and keeper fee payments.