# BalanceManager

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

The `BalanceManager` shared object holds all balances for different assets. To perform trades, pass a combination of `BalanceManager` and `TradeProof` into a [pool](/onchain-finance/deepbookv3/design#pool). `TradeProof`s are generated in one of two ways, either by the `BalanceManager` owner directly, or by any `TradeCap` owner. The owner can generate a `TradeProof` without the risk of equivocation. The `TradeCap` owner, because it's an owned object, risks equivocation when generating a `TradeProof`. Generally, a high frequency trading engine trades as the default owner.

With exception to swaps, all interactions with DeepBookV3 require a `BalanceManager` as one of its inputs. When orders are matched, funds are transferred to or from the `BalanceManager`. You can use a single `BalanceManager` between all pools.

## API

Following are the different public functions that the `BalanceManager` exposes.

**Create a `BalanceManager`**

The `new()` function creates a `BalanceManager`. Combine it with `share`, or else the transaction fails. You can combine the transaction with deposit calls, allowing you to create, deposit, then share the balance manager in one transaction.

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

**Create a `BalanceManager` with custom owner**

The `new_with_custom_owner()` function creates a `BalanceManager` with a custom owner. Combine it with `share`, or else the transaction fails. You can combine the transaction with deposit calls, allowing you to create, deposit, then share the balance manager in one transaction.

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

**Create a `BalanceManager` with custom owner and capabilities**

The `new_with_custom_owner_caps<App>()` function creates a `BalanceManager` with a custom owner and returns all three capabilities (`DepositCap`, `WithdrawCap`, and `TradeCap`) in a single call. This function requires authorization through the DeepBook Registry with a specific `App` type. Combine the balance manager with `share`, or else the transaction fails. This is a convenient way to set up a complete balance manager with all necessary capabilities in one transaction.

:::caution
Move code using DeepBookV3 uses `DepositCap`, `WithdrawCap`, and `TradeCap`, while the DeepBookV3 SDK uses `depositCap`, `withdrawCap`, and `tradeCap`.
:::

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

**Mint a `TradeCap`**

The owner of a `BalanceManager` can mint a `TradeCap` and send it to another address. Upon receipt, that address will have the capability to place orders with this `BalanceManager`. The address owner cannot deposit or withdraw funds, however. The maximum total number of `TradeCap`, `WithdrawCap`, and `DepositCap` that can be assigned for a `BalanceManager` is `1000`. If this limit is reached, one or more existing caps must be revoked before minting new ones. You can also use `revoke_trade_cap` to revoke `DepositCap` and `WithdrawCap`.

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

**Mint a `DepositCap` or `WithdrawCap`**

The owner of a `BalanceManager` can mint a `DepositCap` or `WithdrawCap` and send it to another address. Upon receipt, that address will have the capability to deposit in or withdraw from `BalanceManager`. The address owner cannot execute trades, however. The maximum total number of `TradeCap`, `WithdrawCap`, and `DepositCap` that can be assigned for a `BalanceManager` is `1000`. If this limit is reached, one or more existing caps must be revoked before minting new ones.

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

**Generate a `TradeProof`**

To call any function that requires a balance check or transfer, the user must provide their `BalanceManager` as well as a `TradeProof`. There are two ways to generate a trade proof, one used by the owner and another used by a `TradeCap` owner.

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

**Deposit funds**

Only the owner can call this function to deposit funds into the `BalanceManager`.

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

**Withdraw funds**

Only the owner can call this function to withdraw funds from the `BalanceManager`.

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

**Deposit funds using `DepositCap`**

Only holders of a `DepositCap` for the `BalanceManager` can call this function to deposit funds into the `BalanceManager`.

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

**Withdraw funds using WithdrawCap**

Only holders of a `WithdrawCap` for the `BalanceManager` can call this function to withdraw funds from the `BalanceManager`.

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

**Set and unset referral**

The owner of a `TradeCap` can set or unset a pool-specific referral for the balance manager. Setting a referral allows the balance manager to be associated with a `DeepBookPoolReferral` for that pool, which can track and earn referral fees. Each balance manager can have different referrals for different pools.

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

**Register balance manager**

Register a balance manager with the registry. This adds the balance manager to the owner's list of managers in the registry.

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

**Read endpoints**

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

## Events

**`BalanceManagerEvent`**

Emitted when a new balance manager is created.

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->

**BalanceEvent**

Emitted when a deposit or withdrawal occurs.

<!-- External code reference: packages/deepbook/sources/balance_manager.move -->
