# Swaps

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

DeepBookV3 provides a swap-like interface commonly seen in automatic market makers (AMMs). The DeepBookV3 SDK provides functions to leverage the features of this interface. See [Swaps](/onchain-finance/deepbookv3/contract-information/swaps) in the API section for more details.

## Swap functions

The SDK provides the following functions to perform swaps between the base and quote asset.

### swapExactBaseForQuote

Use `swapExactBaseForQuote` to swap exact base amount for quote amount. The call returns a function that takes a `Transaction` object.

**Parameters**

- `params`: `SwapParams` object that represents the parameters for the swap.

```tsx
swapExactBaseForQuote({ params: SwapParams });
```

### swapExactQuoteForBase

Use `swapExactQuoteForBase` to swap exact quote amount for base amount. The call returns a function that takes a `Transaction` object.

**Parameters**

- `params`: `SwapParams` object that represents the parameters for the swap.

```tsx
swapExactQuoteForBase({ params: SwapParams });
```

### `swapExactQuantity`

Use `swapExactQuantity` to swap an exact quantity in either direction (base to quote or quote to base) without using a balance manager. The call returns a function that takes a `Transaction` object.

**Parameters**

- `params`: `SwapParams & { isBaseToCoin: boolean }` object containing:
  - `poolKey`: String that identifies the pool.
  - `amount`: Number representing the amount to swap.
  - `deepAmount`: Number representing the DEEP amount for fees.
  - `minOut`: Number representing minimum output amount.
  - `isBaseToCoin`: Boolean indicating swap direction (true = base to quote).
  - `baseCoin`: Optional `TransactionArgument` for base coin input.
  - `quoteCoin`: Optional `TransactionArgument` for quote coin input.
  - `deepCoin`: Optional `TransactionArgument` for DEEP coin input.

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

### swapExactQuantityWithManager

Use `swapExactQuantityWithManager` to swap an exact quantity using a balance manager. The call returns a function that takes a `Transaction` object.

**Parameters**

- `params`: `SwapWithManagerParams & { isBaseToCoin: boolean }` object containing:
  - `poolKey`: String that identifies the pool.
  - `balanceManagerKey`: String that identifies the balance manager.
  - `amount`: Number representing the amount to swap.
  - `minOut`: Number representing minimum output amount.
  - `isBaseToCoin`: Boolean indicating swap direction (true = base to quote).
  - `tradeCap`: Optional `TransactionArgument` for trade capability.
  - `depositCap`: Optional `TransactionArgument` for deposit capability.
  - `withdrawCap`: Optional `TransactionArgument` for withdraw capability.
  - `baseCoin`: Optional `TransactionArgument` for base coin input.
  - `quoteCoin`: Optional `TransactionArgument` for quote coin input.

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

### Examples

The following examples demonstrate custom swap functions that you can place into the `DeepBookMarketMaker` class. Base coin, quote coin, and deep coin are automatically determined by the coin available in the user address unless you explicitly pass one in as an argument. You can transfer the coin outputs to their address or execute other operations using the outputs.

```tsx
swapExactBaseForQuote = (tx: Transaction) => {
  const [baseOut, quoteOut, deepOut] = this.deepBook.swapExactBaseForQuote({
    poolKey: 'SUI_DBUSDC',
    amount: 1, // amount of SUI to swap
    deepAmount: 1, // amount of DEEP to pay as fees, excess is returned
    minOut: 0.1, // minimum amount of DBUSDC to receive or transaction fails
  })(tx);

  // Transfer received coins to own address
  tx.transferObjects([baseOut, quoteOut, deepOut], this.getActiveAddress());
};

swapExactQuoteForBase = (tx: Transaction) => {
  const [baseOut, quoteOut, deepOut] = this.deepBook.swapExactQuoteForBase({
    poolKey: 'SUI_DBUSDC',
    amount: 1, // amount of DBUSDC to swap
    deepAmount: 1, // amount of DEEP to pay as fees, excess is returned
    minOut: 0.1, // minimum amount of SUI to receive or transaction fails
  })(tx);

  // Transfer received coins to own address
  tx.transferObjects([baseOut, quoteOut, deepOut], this.getActiveAddress());
};
```
