> ## Documentation Index
> Fetch the complete documentation index at: https://docs.exponent.finance/llms.txt
> Use this file to discover all available pages before exploring further.

# Exponent Orderbook Instructions

> Trade PT and YT on the Exponent Orderbook through policy-gated vault execution

The orderbook instructions module enables vault managers and allocators to trade PT (Principal Tokens) and YT (Yield Tokens) on [Exponent Orderbook](/developer-orderbook/overview). All interactions are executed as Squads sync transactions validated against onchain policies.

## Transaction Structure

Every orderbook interaction goes through `createVaultSyncTransaction`, which returns three parts:

```typescript theme={null}
const { preInstructions, instruction, postInstructions } =
  await createVaultSyncTransaction({
    instructions,   // Array of VaultInstruction descriptors
    owner,          // Vault PDA (the Squads smart account)
    connection,
    policyPda,      // Policy authorizing this execution
    vaultPda,       // Squads vault PDA
    signer,         // The allocator or manager wallet
    vaultAddress,   // ExponentStrategyVault PDA (auto-resolves hook accounts)
  });

// Send all three parts in order
const tx = new Transaction().add(...preInstructions, instruction, ...postInstructions);
await sendAndConfirmTransaction(connection, tx, [wallet]);
```

| Part               | What it contains                                               | Why it's separate                                 |
| ------------------ | -------------------------------------------------------------- | ------------------------------------------------- |
| `preInstructions`  | Any permissionless setup instructions                          | Must be top-level instructions                    |
| `instruction`      | Squads sync transaction wrapping all vault-signed instructions | Executes with the vault's smart account as signer |
| `postInstructions` | Any post-operation instructions                                | Must be top-level instructions                    |

## Trade Directions

The `OrderbookTradeDirection` enum specifies the side of the trade:

```typescript theme={null}
import { OrderbookTradeDirection } from "@exponent-labs/exponent-sdk";

OrderbookTradeDirection.BUY_PT   // Buy principal tokens
OrderbookTradeDirection.SELL_PT  // Sell principal tokens
OrderbookTradeDirection.BUY_YT   // Buy yield tokens
OrderbookTradeDirection.SELL_YT  // Sell yield tokens
```

***

## Post Offer

`orderbookAction.postOffer` posts a limit order on the orderbook at a specified APY price.

```typescript theme={null}
import {
  orderbookAction,
  createVaultSyncTransaction,
  OrderbookTradeDirection,
} from "@exponent-labs/exponent-sdk";

const { preInstructions, instruction, postInstructions } =
  await createVaultSyncTransaction({
    instructions: [
      orderbookAction.postOffer({
        orderbook: new PublicKey("..."),
        direction: OrderbookTradeDirection.BUY_PT,
        priceApy: 0.10,             // 10% APY
        amount: 1_000_000_000n,     // 1 token in native units
        offerIdx: 0,                // Unique offer index per trader
      }),
    ],
    owner: vaultPda,
    connection,
    policyPda,
    vaultPda,
    signer: wallet.publicKey,
    vaultAddress,
  });

const tx = new Transaction().add(...preInstructions, instruction, ...postInstructions);
await sendAndConfirmTransaction(connection, tx, [wallet]);
```

### Parameters

| Parameter       | Type                      | Description                                                                        |
| --------------- | ------------------------- | ---------------------------------------------------------------------------------- |
| `orderbook`     | `PublicKey`               | The orderbook account address                                                      |
| `direction`     | `OrderbookTradeDirection` | Trade direction (BUY\_PT, SELL\_PT, BUY\_YT, SELL\_YT)                             |
| `priceApy`      | `number`                  | Limit price expressed as an APY (e.g., `0.10` for 10%)                             |
| `amount`        | `bigint`                  | Amount in native token units                                                       |
| `offerIdx`      | `number`                  | Unique offer index per trader — used for position tracking                         |
| `offerOption`   | `OrderbookOfferOption`    | Optional. `"FillOrKill"` to require full fill or cancel                            |
| `virtualOffer`  | `boolean`                 | Optional. Use [virtual offers](/orderbook/virtual-offers) to share PT/YT liquidity |
| `expirySeconds` | `number`                  | Optional. Time-to-live in seconds before the offer auto-cancels                    |

A matching policy must exist before executing. See [Policy Builders](/vault-sdk/typescript/manager-operations/policy-builders#orderbook-policy).

***

## Market Offer

`orderbookAction.marketOffer` executes a market order against existing resting offers on the orderbook.

```typescript theme={null}
const { preInstructions, instruction, postInstructions } =
  await createVaultSyncTransaction({
    instructions: [
      orderbookAction.marketOffer({
        orderbook: new PublicKey("..."),
        direction: OrderbookTradeDirection.BUY_PT,
        maxPriceApy: 0.15,            // Will not pay more than 15% APY
        amount: 500_000_000n,
        minAmountOut: 480_000_000n,    // Slippage protection
      }),
    ],
    owner: vaultPda,
    connection,
    policyPda,
    vaultPda,
    signer: wallet.publicKey,
    vaultAddress,
  });

const tx = new Transaction().add(...preInstructions, instruction, ...postInstructions);
await sendAndConfirmTransaction(connection, tx, [wallet]);
```

### Parameters

| Parameter      | Type                      | Description                                               |
| -------------- | ------------------------- | --------------------------------------------------------- |
| `orderbook`    | `PublicKey`               | The orderbook account address                             |
| `direction`    | `OrderbookTradeDirection` | Trade direction                                           |
| `maxPriceApy`  | `number`                  | Maximum acceptable APY price                              |
| `amount`       | `bigint`                  | Amount to spend in native token units                     |
| `minAmountOut` | `bigint`                  | Minimum output amount — slippage protection               |
| `virtualOffer` | `boolean`                 | Optional. Use [virtual offers](/orderbook/virtual-offers) |

A matching policy must exist before executing. See [Policy Builders](/vault-sdk/typescript/manager-operations/policy-builders#orderbook-policy).

***

## Remove Offer

`orderbookAction.removeOffer` cancels an existing limit order on the orderbook.

```typescript theme={null}
const { preInstructions, instruction, postInstructions } =
  await createVaultSyncTransaction({
    instructions: [
      orderbookAction.removeOffer({
        orderbook: new PublicKey("..."),
        offerIdx: 0,
      }),
    ],
    owner: vaultPda,
    connection,
    policyPda,
    vaultPda,
    signer: wallet.publicKey,
    vaultAddress,
  });

const tx = new Transaction().add(...preInstructions, instruction, ...postInstructions);
await sendAndConfirmTransaction(connection, tx, [wallet]);
```

### Parameters

| Parameter   | Type        | Description                                              |
| ----------- | ----------- | -------------------------------------------------------- |
| `orderbook` | `PublicKey` | The orderbook account address                            |
| `offerIdx`  | `number`    | The offer index to cancel (same index used when posting) |

A matching policy must exist before executing. See [Policy Builders](/vault-sdk/typescript/manager-operations/policy-builders#orderbook-policy).

***

## Withdraw Funds

`orderbookAction.withdrawFunds` withdraws settled funds from the user escrow on the orderbook back to the vault's token accounts.

```typescript theme={null}
const { preInstructions, instruction, postInstructions } =
  await createVaultSyncTransaction({
    instructions: [
      orderbookAction.withdrawFunds({
        orderbook: new PublicKey("..."),
        ptAmount: null,    // null = withdraw max
        ytAmount: null,
        syAmount: null,
      }),
    ],
    owner: vaultPda,
    connection,
    policyPda,
    vaultPda,
    signer: wallet.publicKey,
    vaultAddress,
  });

const tx = new Transaction().add(...preInstructions, instruction, ...postInstructions);
await sendAndConfirmTransaction(connection, tx, [wallet]);
```

### Parameters

| Parameter   | Type        | Description                   |                                                                          |
| ----------- | ----------- | ----------------------------- | ------------------------------------------------------------------------ |
| `orderbook` | `PublicKey` | The orderbook account address |                                                                          |
| `ptAmount`  | \`bigint    | null\`                        | Optional. PT amount to withdraw — `null` withdraws the maximum available |
| `ytAmount`  | \`bigint    | null\`                        | Optional. YT amount to withdraw — `null` withdraws the maximum available |
| `syAmount`  | \`bigint    | null\`                        | Optional. SY amount to withdraw — `null` withdraws the maximum available |

A matching policy must exist before executing. See [Policy Builders](/vault-sdk/typescript/manager-operations/policy-builders#orderbook-policy).

***

## Full Flow Example

This example demonstrates a complete orderbook interaction: post a limit buy for PT, then cancel it and withdraw any settled funds.

### Step 1: Post a Limit Buy PT Order

```typescript theme={null}
import {
  orderbookAction,
  createVaultSyncTransaction,
  OrderbookTradeDirection,
} from "@exponent-labs/exponent-sdk";
import { Connection, PublicKey, Transaction, sendAndConfirmTransaction } from "@solana/web3.js";

const connection = new Connection("https://api.mainnet-beta.solana.com");

const ORDERBOOK = new PublicKey("...");

const { preInstructions, instruction, postInstructions } =
  await createVaultSyncTransaction({
    instructions: [
      orderbookAction.postOffer({
        orderbook: ORDERBOOK,
        direction: OrderbookTradeDirection.BUY_PT,
        priceApy: 0.08,            // 8% APY
        amount: 2_000_000_000n,    // 2 tokens
        offerIdx: 0,
      }),
    ],
    owner: vaultPda,
    connection,
    policyPda,
    vaultPda,
    signer: wallet.publicKey,
    vaultAddress,
  });

const tx = new Transaction().add(...preInstructions, instruction, ...postInstructions);
await sendAndConfirmTransaction(connection, tx, [wallet]);
```

### Step 2: Cancel the Order

```typescript theme={null}
const { preInstructions, instruction, postInstructions } =
  await createVaultSyncTransaction({
    instructions: [
      orderbookAction.removeOffer({
        orderbook: ORDERBOOK,
        offerIdx: 0,
      }),
    ],
    owner: vaultPda,
    connection,
    policyPda,
    vaultPda,
    signer: wallet.publicKey,
    vaultAddress,
  });

const tx = new Transaction().add(...preInstructions, instruction, ...postInstructions);
await sendAndConfirmTransaction(connection, tx, [wallet]);
```

### Step 3: Withdraw All Settled Funds

```typescript theme={null}
const { preInstructions, instruction, postInstructions } =
  await createVaultSyncTransaction({
    instructions: [
      orderbookAction.withdrawFunds({
        orderbook: ORDERBOOK,
        ptAmount: null,
        ytAmount: null,
        syAmount: null,
      }),
    ],
    owner: vaultPda,
    connection,
    policyPda,
    vaultPda,
    signer: wallet.publicKey,
    vaultAddress,
  });

const tx = new Transaction().add(...preInstructions, instruction, ...postInstructions);
await sendAndConfirmTransaction(connection, tx, [wallet]);
```
