Back to Sui

Margin Manager

docs/content/standards/deepbook-margin/contract-information/margin-manager.mdx

latest6.1 KB
Original Source

The MarginManager is a shared object that wraps a BalanceManager and provides the necessary capabilities to deposit, withdraw, trade, and manage leveraged positions. It enables users to borrow assets from margin pools to amplify their trading positions while managing risk through collateralization.

Each MarginManager is associated with a specific DeepBook pool and can borrow from margin pools that allow trading on that pool. The margin manager tracks borrowed positions and enforces risk ratio limits to maintain system solvency.

API

Following are the different public functions that the MarginManager exposes.

<details> <summary>Create a `MarginManager`</summary>

The new() function creates and shares a MarginManager in one transaction. It validates that margin trading is enabled for the specified pool.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" fun="new" noComments />

</details> <details> <summary>Create a `MarginManager` with initializer</summary>

The new_with_initializer() function creates a MarginManager and returns it along with an initializer hot potato. The initializer ensures the margin manager is properly shared after creation using the share() function.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" fun="new_with_initializer,share" noComments />

</details> <details> <summary>Set or unset referral</summary>

The owner of a MarginManager can set or unset a pool-specific referral for trading fee benefits. The referral must be a DeepBookPoolReferral minted for the pool associated with the margin manager. Each margin manager can have different referrals for different pools.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" fun="set_margin_manager_referral,unset_margin_manager_referral" />

</details> <details> <summary>Deposit funds</summary>

Only the owner can deposit funds into the MarginManager. The deposited asset must be either the base asset, quote asset, or DEEP token.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" fun="deposit" />

</details> <details> <summary>Withdraw funds</summary>

Only the owner can withdraw funds from the MarginManager. Withdrawals are subject to risk ratio limits when the manager has active loans.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" fun="withdraw" />

</details> <details> <summary>Borrow assets</summary>

Borrow base or quote assets from margin pools to increase position sizes. Borrowing is subject to risk ratio limits and the margin pool must allow trading on the manager's DeepBook pool.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" fun="borrow_base,borrow_quote" />

</details> <details> <summary>Repay loans</summary>

Repay borrowed assets to reduce debt. You can specify an exact amount or repay all available balance up to the total debt.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" fun="repay_base,repay_quote" />

</details> <details> <summary>Liquidate position</summary>

Liquidate an undercollateralized margin manager. The liquidator provides repayment and receives collateral assets plus a liquidation reward. The margin pool might also receive a reward or incur bad debt depending on the position's health.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" fun="liquidate" />

</details> <details> <summary>Calculate risk ratio</summary>

Returns the risk ratio of the margin manager, which represents the ratio of assets to debt. Higher ratios indicate healthier positions.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" fun="risk_ratio" />

</details> <details> <summary>Read endpoints</summary>

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" fun="balance_manager,calculate_assets,calculate_debts,owner,deepbook_pool,margin_pool_id,borrowed_shares,borrowed_base_shares,borrowed_quote_shares,has_base_debt" />

</details>

Events

<details> <summary>`MarginManagerCreatedEvent`</summary>

Emitted when a new margin manager is created.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" struct="MarginManagerCreatedEvent" />

</details> <details> <summary>`DepositCollateralEvent`</summary>

Emitted when collateral is deposited into a margin manager.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" struct="DepositCollateralEvent" />

</details> <details> <summary>`WithdrawCollateralEvent`</summary>

Emitted when collateral is withdrawn from a margin manager.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" struct="WithdrawCollateralEvent" />

</details> <details> <summary>`LoanBorrowedEvent`</summary>

Emitted when assets are borrowed from a margin pool.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" struct="LoanBorrowedEvent" />

</details> <details> <summary>`LoanRepaidEvent`</summary>

Emitted when borrowed assets are repaid.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" struct="LoanRepaidEvent" />

</details> <details> <summary>`LiquidationEvent`</summary>

Emitted when a margin manager is liquidated.

<ImportContent source="packages/deepbook_margin/sources/margin_manager.move" mode="code" org="MystenLabs" repo="deepbookv3" struct="LiquidationEvent" />

</details>