# Margin Manager SDK

*[Documentation index](/llms.txt) · [Full index](/llms-full.txt)*

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.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `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.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `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`: `TransactionArgument` representing the margin manager.
- `initializer`: `TransactionArgument` representing the initializer.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `depositDuringInitialization`

Use `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).

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `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.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `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.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `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.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `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.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `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`: `TransactionArgument` representing the coin to use for repayment.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `setMarginManagerReferral`

Use `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.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `unsetMarginManagerReferral`

Use `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.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

## Read-only functions

The following functions query margin manager state without modifying it.

### `managerState`

Query comprehensive state information for a margin manager, including risk ratio, assets, debts, and Pyth price data.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `owner`, `deepbookPool`, `marginPoolId`

Query basic margin manager information.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `baseBalance`, `quoteBalance`, `deepBalance`

Query individual asset balances held in the margin manager.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `borrowedShares`, `borrowedBaseShares`, `borrowedQuoteShares`, `hasBaseDebt`

Query borrowed position information.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

### `balanceManager`, `calculateAssets`, `calculateDebts`

Query balance and debt information.

<!-- External code reference: packages/deepbook-v3/src/transactions/marginManager.ts -->

## Examples

The following examples demonstrate common margin manager operations.

### Create a margin manager

```tsx
/**
 * @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

```tsx
// Example: Deposit 100 SUI as collateral
depositCollateral = (tx: Transaction) => {
	const managerKey = 'MARGIN_MANAGER_1';
	tx.add(this.marginContract.depositBase(managerKey, 100));
};
```

### Borrow assets

```tsx
// Example: Borrow 500 USDC
borrowFunds = (tx: Transaction) => {
	const managerKey = 'MARGIN_MANAGER_1';
	tx.add(this.marginContract.borrowQuote(managerKey, 500));
};
```

### Repay loan

```tsx
// 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

```tsx
// 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));
};
```
