docs/content/standards/deepbook-margin-sdk/margin-manager.mdx
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.
The DeepBook Margin SDK provides the following functions for managing margin accounts.
newMarginManagerUse 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.newMarginManagerWithInitializerUse 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.shareMarginManagerUse 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: TransactionArgument representing the margin manager.initializer: TransactionArgument representing the initializer.depositDuringInitializationUse depositDuringInitialization to deposit funds into a margin manager during its creation, before it is shared. This must be called in the same transaction as newMarginManagerWithInitializer and before shareMarginManager. The call returns a function that takes a Transaction object.
Parameters
manager: TransactionArgument representing the margin manager returned by newMarginManagerWithInitializer.poolKey: String that identifies the DeepBook pool.coinType: String identifying the coin type to deposit (for example, 'SUI', 'DBUSDC', 'DEEP').amount: Number representing the amount to deposit (provide either amount or coin, not both).coin: TransactionArgument representing a coin object to deposit (provide either amount or coin, not both).depositBase, depositQuote, depositDeepUse 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.withdrawBase, withdrawQuote, withdrawDeepUse 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.borrowBase, borrowQuoteUse 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.repayBase, repayQuoteUse 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.liquidateUse 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: TransactionArgument representing the coin to use for repayment.setMarginManagerReferralUse setMarginManagerReferral to set a pool-specific referral for the margin manager. The referral must be a DeepBookPoolReferral minted for the pool associated with the margin manager. The call returns a function that takes a Transaction object.
Parameters
managerKey: String that identifies the margin manager.referral: String representing the referral ID.unsetMarginManagerReferralUse unsetMarginManagerReferral to remove the referral association from a margin manager for a specific pool. The call returns a function that takes a Transaction object.
Parameters
managerKey: String that identifies the margin manager.poolKey: String that identifies the DeepBook pool.The following functions query margin manager state without modifying it.
managerStateQuery comprehensive state information for a margin manager, including risk ratio, assets, debts, and Pyth price data.
<ImportContent source="packages/deepbook-v3/src/transactions/marginManager.ts" mode="code" org="MystenLabs" repo="ts-sdks" fun="managerState" signatureOnly />owner, deepbookPool, marginPoolIdQuery basic margin manager information.
<ImportContent source="packages/deepbook-v3/src/transactions/marginManager.ts" mode="code" org="MystenLabs" repo="ts-sdks" fun="ownerByPoolKey,deepbookPool,marginPoolId" signatureOnly />baseBalance, quoteBalance, deepBalanceQuery individual asset balances held in the margin manager.
<ImportContent source="packages/deepbook-v3/src/transactions/marginManager.ts" mode="code" org="MystenLabs" repo="ts-sdks" fun="baseBalance,quoteBalance,deepBalance" signatureOnly />borrowedShares, borrowedBaseShares, borrowedQuoteShares, hasBaseDebtQuery borrowed position information.
<ImportContent source="packages/deepbook-v3/src/transactions/marginManager.ts" mode="code" org="MystenLabs" repo="ts-sdks" fun="borrowedShares,borrowedBaseShares,borrowedQuoteShares,hasBaseDebt" signatureOnly />balanceManager, calculateAssets, calculateDebtsQuery balance and debt information.
<ImportContent source="packages/deepbook-v3/src/transactions/marginManager.ts" mode="code" org="MystenLabs" repo="ts-sdks" fun="balanceManager,calculateAssets,calculateDebts" signatureOnly />The following examples demonstrate common margin manager operations.
/**
* @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));
};
// Example: Deposit 100 SUI as collateral
depositCollateral = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
tx.add(this.marginContract.depositBase(managerKey, 100));
};
// Example: Borrow 500 USDC
borrowFunds = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
tx.add(this.marginContract.borrowQuote(managerKey, 500));
};
// 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));
};
// 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));
};