@deepbook/margin-trading

Version 8 - mainnet - 0x40...f5625f

Published by@deepbook · 4 packages

Readme

Versions

8

Dependencies

0

Dependents

0

Analytics

On the way

DeepBook Margin

DeepBook Margin is a decentralized margin trading protocol built on top of DeepBookV3 on Sui. It enables leveraged trading by allowing users to borrow assets against their collateral, execute margin orders on DeepBook's central limit order book, and manage risk with advanced features like take profit and stop loss conditional orders. The protocol includes a lending pool where suppliers can earn interest by providing liquidity for margin traders to borrow.

DeepBook Margin Information

Margin Manager

The MarginManager is a shared object that represents a single margin trading account. It holds collateral balances, tracks borrowed positions, and interfaces with both DeepBookV3 for trading and the MarginPool for borrowing. Each MarginManager is linked to a specific DeepBook pool and its corresponding MarginPool.

Users can deposit collateral (base, quote, or DEEP tokens), borrow assets to increase their trading position, and repay loans. The MarginManager tracks the user's risk ratio, which determines liquidation eligibility. If a position becomes undercollateralized, it can be liquidated by any party.

Margin Pool

The MarginPool is a lending pool that provides liquidity for margin traders. It consists of:

  1. Supply Side - Users can supply assets to earn interest from borrowers. Suppliers receive shares representing their portion of the pool.
  2. Borrow Side - Margin traders borrow from the pool to leverage their positions. Interest accrues based on utilization rate.
  3. Interest Model - Dynamic interest rates adjust based on pool utilization to balance supply and demand.

Suppliers mint a SupplierCap to track their deposits and can withdraw their supplied assets plus accrued interest at any time, subject to available liquidity.

Take Profit / Stop Loss (TPSL)

DeepBook Margin supports conditional orders that automatically execute when price conditions are met:

  • Take Profit - Automatically close a position when the price reaches a favorable target
  • Stop Loss - Automatically close a position to limit losses when price moves against you

Conditional orders are stored on-chain and can be executed by anyone (permissionlessly) once the trigger price is reached. This enables automated risk management without requiring users to monitor positions constantly.

A triggered order that can't execute yet stays queued: for example when the order book can't fill it inside the allowed price band, or the manager doesn't hold enough funds. A market order that the book fills only in part stays queued with the unfilled quantity. A conditional limit order is cancelled once it is past its expiry, or if it triggers while the manager is below the liquidation risk ratio.

Liquidation

When a MarginManager's risk ratio exceeds the maximum threshold (position becomes undercollateralized), it becomes eligible for liquidation. Any user can call the liquidate function to:

  1. Repay the outstanding debt
  2. Receive the collateral plus a liquidation bonus

This mechanism ensures the protocol remains solvent and incentivizes liquidators to maintain system health.