Margin Manager SDK
Managing margin accounts is essential for leveraged trading on DeepBook. The Margin Manager SDK provides functions for creating margin managers, depositing collateral, borrowing assets, and managing risk.
Margin Manager functions
The DeepBook Margin SDK provides the following functions for managing margin accounts.
newMarginManager
Use newMarginManager to create and share a new margin manager in one transaction. The call returns a function that takes a Transaction object.
Parameters
poolKey: String that identifies the DeepBook pool.
packages/deepbook-v3/src/transactions/marginManager.ts. You probably need to run `pnpm prebuild` and restart the site.newMarginManagerWithInitializer
Use newMarginManagerWithInitializer to create a margin manager and return it with an initializer. You must call shareMarginManager afterward to share it. The call returns an object with manager and initializer.
Parameters
poolKey: String that identifies the DeepBook pool.
packages/deepbook-v3/src/transactions/marginManager.ts. You probably need to run `pnpm prebuild` and restart the site.shareMarginManager
Use shareMarginManager to share a margin manager created with newMarginManagerWithInitializer. The call returns a function that takes a Transaction object.
Parameters
poolKey: String that identifies the DeepBook pool.manager:TransactionArgumentrepresenting the margin manager.initializer:TransactionArgumentrepresenting the initializer.
packages/deepbook-v3/src/transactions/marginManager.ts. You probably need to run `pnpm prebuild` and restart the site.depositBase, depositQuote, depositDeep
Use these functions to deposit assets into a margin manager. The call returns a function that takes a Transaction object.
Parameters
managerKey: String that identifies the margin manager.amount: Number representing the amount to deposit.
packages/deepbook-v3/src/transactions/marginManager.ts. You probably need to run `pnpm prebuild` and restart the site.withdrawBase, withdrawQuote, withdrawDeep
Use these functions to withdraw assets from a margin manager. Withdrawals are subject to risk ratio limits. The call returns a function that takes a Transaction object.
Parameters
managerKey: String that identifies the margin manager.amount: Number representing the amount to withdraw.
packages/deepbook-v3/src/transactions/marginManager.ts. You probably need to run `pnpm prebuild` and restart the site.borrowBase, borrowQuote
Use these functions to borrow assets from margin pools. Borrowing is subject to risk ratio limits. The call returns a function that takes a Transaction object.
Parameters
managerKey: String that identifies the margin manager.amount: Number representing the amount to borrow.
packages/deepbook-v3/src/transactions/marginManager.ts. You probably need to run `pnpm prebuild` and restart the site.repayBase, repayQuote
Use these functions to repay borrowed assets. If no amount is specified, it repays the maximum available balance up to the total debt. The call returns a function that takes a Transaction object.
Parameters
managerKey: String that identifies the margin manager.amount: Optional number representing the amount to repay.
packages/deepbook-v3/src/transactions/marginManager.ts. You probably need to run `pnpm prebuild` and restart the site.liquidate
Use liquidate to liquidate an undercollateralized margin manager. The call returns a function that takes a Transaction object.
Parameters
managerAddress: String representing the address of the margin manager to liquidate.poolKey: String that identifies the DeepBook pool.debtIsBase: Boolean indicating whether the debt is in the base asset.repayCoin:TransactionArgumentrepresenting the coin to use for repayment.
packages/deepbook-v3/src/transactions/marginManager.ts. You probably need to run `pnpm prebuild` and restart the site.Read-only functions
The following functions query margin manager state without modifying it.
owner, deepbookPool, marginPoolId
Query basic margin manager information.
packages/deepbook-v3/src/transactions/marginManager.ts. You probably need to run `pnpm prebuild` and restart the site.borrowedShares, borrowedBaseShares, borrowedQuoteShares, hasBaseDebt
Query borrowed position information.
packages/deepbook-v3/src/transactions/marginManager.ts. You probably need to run `pnpm prebuild` and restart the site.balanceManager, calculateAssets, calculateDebts
Query balance and debt information.
packages/deepbook-v3/src/transactions/marginManager.ts. You probably need to run `pnpm prebuild` and restart the site.Examples
The following examples demonstrate common margin manager operations.
Create a margin manager
/**
* @description Create a new margin manager
* @param {string} poolKey The key to identify the pool
* @returns A function that takes a Transaction object
*/
newMarginManager = (poolKey: string) => (tx: Transaction) => {};
// Example usage
createMarginManager = (tx: Transaction) => {
const poolKey = 'SUI_DBUSDC';
tx.add(this.marginContract.newMarginManager(poolKey));
};
Deposit collateral
// Example: Deposit 100 SUI as collateral
depositCollateral = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
tx.add(this.marginContract.depositBase(managerKey, 100));
};
Borrow assets
// Example: Borrow 500 USDC
borrowFunds = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
tx.add(this.marginContract.borrowQuote(managerKey, 500));
};
Repay loan
// Example: Repay all borrowed quote assets
repayLoan = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
// No amount specified = repay all
tx.add(this.marginContract.repayQuote(managerKey));
};
Liquidate a position
// Example: Liquidate an undercollateralized position
liquidatePosition = (tx: Transaction) => {
const managerAddress = '0x...'; // Address of margin manager to liquidate
const poolKey = 'SUI_DBUSDC';
const debtIsBase = false; // Debt is in USDC (quote)
const repayCoin = tx.splitCoins(tx.gas, [500 * 1_000_000]); // 500 USDC
tx.add(this.marginContract.liquidate(managerAddress, poolKey, debtIsBase, repayCoin));
};
Related links
The DeepBook Margin package on GitHub.
The DeepBookV3 SDK node package on NPM.