Building and Executing Trades on DeepBook Protocol

·

This section explains how to construct and execute trades using the DeepBook decentralized exchange protocol.


1. Creating a Liquidity Pool

To create a pool on DeepBook, you must specify:

A transaction fee (paid in SUI tokens) is required for pool creation.

Move Function Signature

public fun create_pool (
  registry: &mut Registry,
  tick_size: u64,
  lot_size: u64,
  coin: Coin,
  ctx: &mut TxContext
) -> AccountCap

TypeScript SDK Implementation

public createPool(
  tickSize: number,
  lotSize: number,
  token1: string,
  token2: string,
  overrides: Overrides = new Overrides()
): TransactionBlock {
  const [coin] = overrides.txb.splitCoins(overrides.txb.gas, [overrides.txb.pure(200000000)]);
  overrides.txb.moveCall({
    typeArguments: [token1, token2, '0x2::sui::SUI'],
    target: `${this.configs.swapPackageId}::clob::create_pool`,
    arguments: [
      overrides.txb.object(this.configs.registryId),
      overrides.txb.pure(tickSize),
      overrides.txb.pure(lotSize),
      coin
    ]
  });
  return overrides.txb;
}

2. Order Placement

DeepBook supports two order types:

2.1 Limit Orders

Order Restrictions

| Type | Code | Description |
|--------------------|------|-------------|
| NO_RESTRICTION | 0 | Fill as much as possible, rest becomes limit order |
| IMMEDIATE_OR_CANCEL | 1 | Fill immediately or cancel remainder |
| FILL_OR_KILL | 2 | Execute fully or cancel entirely |
| POST_OR_ABORT | 3 | Post entire order or abort |

Custodian Account Setup

Before placing orders, traders must:

  1. Create an account via create_account to receive an AccountCap.
  2. Deposit assets using deposit_base or deposit_quote.

Example Move Call:

public fun place_limit_order (
  pool: &mut Pool,
  price: u64,
  quantity: u64,
  is_bid: bool,
  expire_timestamp: u64,
  restriction: u8,
  account_cap: &AccountCap
) -> (u64, u64, bool, u64)

👉 Learn advanced order strategies


2.2 Market Orders

Execute immediately at best available price. Unfilled portions are canceled.

TypeScript Example:

public async placeMarketOrder(
  tokenIn: string,
  amountIn: number,
  currentAddress: string
): Promise<TransactionBlock> {
  const [baseCoin, quoteCoin] = await txb.moveCall({
    target: `${packageId}::clob::place_market_order`,
    arguments: [poolId, amountIn, isBid, clockObject]
  });
  txb.transferObjects([baseCoin, quoteCoin], currentAddress);
  return txb;
}

3. Order Cancellation

3.1 Single Order Cancellation

public fun cancel_order (
  pool: &mut Pool,
  order_id: u64,
  account_cap: &AccountCap
)

3.2 Bulk Cancellation

Group orders by price level for gas efficiency:

public batchCancelOrder(
  orderIds: number[],
  accountCap: string
): TransactionBlock {
  txb.moveCall({
    target: `${packageId}::clob::batch_cancel_order`,
    arguments: [poolId, orderIds, accountCap]
  });
}

FAQ

Q: How are fees calculated for limit orders?

A: Fees depend on pool parameters and are deducted from the deposited SUI tokens.

Q: Can I modify an existing limit order?

A: No—orders must be canceled and re-placed.

Q: What’s the maximum order duration?

A: Orders expire at the specified expire_timestamp (in milliseconds).

👉 Explore DeepBook’s full API


This guide covers core functionalities for trading on DeepBook. For real-world examples, refer to the protocol’s official documentation.