Margin Manager
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.
Create a MarginManager
The new() function creates and shares a MarginManager in one transaction. It validates that margin trading is enabled for the specified pool.
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Create a MarginManager with initializer
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.
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Set or unset referral
The owner of a MarginManager can set or unset a referral for trading fee benefits.
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Deposit funds
Only the owner can deposit funds into the MarginManager. The deposited asset must be either the base asset, quote asset, or DEEP token.
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Withdraw funds
Only the owner can withdraw funds from the MarginManager. Withdrawals are subject to risk ratio limits when the manager has active loans.
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Borrow assets
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.
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Repay loans
Repay borrowed assets to reduce debt. You can specify an exact amount or repay all available balance up to the total debt.
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Liquidate position
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.
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Calculate risk ratio
Returns the risk ratio of the margin manager, which represents the ratio of assets to debt. Higher ratios indicate healthier positions.
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Read endpoints
packages/deepbook_margin/sources/margin_manager.move. You probably need to run `pnpm prebuild` and restart the site.Related links
The DeepBook Margin package on GitHub.