docs/content/standards/deepbook-margin/design.mdx
At a high level, the DeepBook Margin design revolves around four main shared objects that work together to enable leveraged trading:
MarginPool: A shared object that manages liquidity for a specific asset, handling supply and borrow operations with interest accrual. See the MarginPool shared object section to learn more.MarginManager: A shared object that wraps a BalanceManager and provides capabilities for leveraged trading, including borrowing, repaying, and risk management. See the MarginManager shared object section to learn more.MarginRegistry: A central registry that manages all margin pools and margin managers, enforces system-wide configurations, and tracks enabled pools. See the MarginRegistry section to learn more.BalanceManager: Inherited from DeepBookV3, used to source funds for trading. See BalanceManager to learn more.MarginPool shared object {#margin-pool}The MarginPool is responsible for managing liquidity for a single asset type. It consists of several key components:
The State component tracks the total supply and borrow amounts, along with their corresponding shares. It uses a shares-based accounting system where:
Interest accrues continuously based on the utilization rate (ratio of borrowed to supplied assets). The state updates on every supply, borrow, repay, and withdraw operation, ensuring interest is always current.
The interest rate is calculated dynamically based on the pool's utilization rate, following a piecewise linear model defined in the InterestConfig. Higher utilization leads to higher interest rates, incentivizing more supply and less borrowing.
The ProtocolConfig stores critical parameters that govern the margin pool's behavior:
These parameters can be updated by the pool operator using the MarginPoolCap.
The ProtocolFees component manages the distribution of interest earned. When borrowers pay interest, the protocol spread (ex., 10%) is taken as fees, with the remaining portion (ex., 90%) distributed to suppliers based on their shares.
The protocol spread is then distributed as follows:
For example, if 100 USDC in interest is paid, 10 USDC goes to protocol fees (at 10% spread) and 90 USDC goes to suppliers. The 10 USDC is then split: 5 USDC to referrals, 2.5 USDC to protocol, and 2.5 USDC to the maintainer.
The protocol tracks shares owned by each referral and calculates their proportional fees based on the liquidity they've referred.
The PositionManager tracks individual supplier positions, including:
SupplierCap.MarginManager shared object {#margin-manager}The MarginManager wraps a BalanceManager and adds margin trading capabilities. Each MarginManager is associated with a specific DeepBook pool and tracks:
A MarginManager can only borrow from one margin pool at a time (either base or quote asset). This simplifies risk calculations and prevents complex cross-collateral scenarios.
Risk is measured using a risk ratio calculated as:
$$\text{Risk Ratio} = \frac{\text{Total Assets}}{\text{Total Debt}}$$
Different risk ratio thresholds determine allowable actions:
When a MarginManager risk ratio falls below the liquidation threshold, anyone can liquidate the position:
Liquidations can be partial or full, depending on the position's health and the liquidator's input.
MarginRegistry {#margin-registry}The MarginRegistry serves as the central coordination point for the margin system:
The registry enforces that only one margin pool can exist per asset type and ensures margin managers can only trade on enabled DeepBook pools.
The following describes a typical liquidation scenario:
MarginManager falls below the liquidation threshold due to price movements or interest accrual.liquidate() providing repayment coins.Interest accrues continuously in DeepBook Margin based on the utilization rate:
$$\text{Utilization Rate} = \frac{\text{Total Borrowed}}{\text{Total Supplied}}$$
The interest rate follows a kinked model:
Interest compounds every time the state is updated (on any supply, borrow, repay, or withdraw operation). The protocol spread determines what portion of the interest goes to fees versus suppliers.