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

# Order Book SDK

> Get started with the Exponent Orderbook SDK

This page walks through the full orderbook trading lifecycle — loading state, getting quotes, posting and filling orders, collecting interest, and withdrawing funds.

## Installation

<CodeGroup>
  ```bash yarn theme={null}
  yarn add @exponent-labs/exponent-sdk
  ```

  ```bash npm theme={null}
  npm install @exponent-labs/exponent-sdk
  ```

  ```bash pnpm theme={null}
  pnpm add @exponent-labs/exponent-sdk
  ```
</CodeGroup>

## Setup

```typescript theme={null}
import { Orderbook, LOCAL_ENV } from "@exponent-labs/exponent-sdk";
import {
  Connection,
  PublicKey,
  Transaction,
  sendAndConfirmTransaction,
  Keypair,
} from "@solana/web3.js";
const connection = new Connection("https://api.mainnet-beta.solana.com");
const wallet = Keypair.fromSecretKey(/* your keypair */);
const orderbookAddress = new PublicKey("...");
const syMintAddress = new PublicKey("...");

const orderbook = await Orderbook.load(LOCAL_ENV, connection, orderbookAddress);
```

<Steps>
  <Step title="Get a Quote">
    `getQuote` simulates a trade and returns the expected output before you send a transaction.

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

    const clock = await connection.getClock();
    const unixNow = Number(clock.unixTimestamp);

    const quote = orderbook.getQuote({
      inAmount: 1_000_000_000,
      direction: QuoteDirection.SY_TO_YT,
      unixNow,
      syExchangeRate: 1.000009210084,
    });

    console.log("Expected output:", quote.outAmount);
    console.log("Maker fees:", quote.makerFees);
    console.log("Taker fees:", quote.takerFees);
    ```

    <Tip>
      Always use `connection.getClock()` for the `unixNow` parameter — `Date.now()` may drift from the onchain clock and produce inaccurate quotes.
    </Tip>
  </Step>

  <Step title="Post a Limit Order">
    `ixWrapperPostOffer` places a limit order on the orderbook. The wrapper handles token account creation for you.

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

    const { ix, setupIxs } = await orderbook.ixWrapperPostOffer({
      trader: wallet.publicKey,
      price: 5.0,
      amount: 1_000_000_000n,
      offerType: OfferType.SellYt,
      offerOption: offerOptions("FillOrKill", [false]),
      virtualOffer: false,
      expirySeconds: 3600,
      mintSy: syMintAddress,
    });

    const tx = new Transaction().add(...setupIxs, ix);
    await sendAndConfirmTransaction(connection, tx, [wallet]);
    ```

    <Note>
      The `setupIxs` array creates PT, YT, and SY token accounts if they do not exist. Always include them before `ix` in your transaction.
    </Note>
  </Step>

  <Step title="Execute a Market Order">
    `ixWrapperMarketOffer` fills against existing orders immediately.

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

    const { ix, setupIxs } = await orderbook.ixWrapperMarketOffer({
      trader: wallet.publicKey,
      maxPriceApy: 5.5,
      amount: 1_000_000_000n,
      offerType: OfferType.BuyYt,
      minAmountOut: 990_000_000n,
      virtualOffer: false,
      mintSy: syMintAddress,
    });

    const tx = new Transaction().add(...setupIxs, ix);
    await sendAndConfirmTransaction(connection, tx, [wallet]);
    ```

    <Tip>
      Set `minAmountOut` for slippage protection. The transaction reverts if the output falls below this threshold.
    </Tip>
  </Step>

  <Step title="Check Open Orders">
    Query your active orders to see what is currently on the book.

    ```typescript theme={null}
    const openOrders = orderbook.getUserOpenOrders(wallet.publicKey);

    for (const order of openOrders) {
      console.log("Index:", order.offerIndex);
      console.log("Type:", order.type);
      console.log("Price APY:", order.priceApy);
      console.log("Amount:", order.amount);
    }
    ```
  </Step>

  <Step title="Remove an Order">
    Cancel an open order using its `offerIdx` from `getUserOpenOrders`.

    ```typescript theme={null}
    const ix = await orderbook.ixWrapperRemoveOffer({
      trader: wallet.publicKey,
      offerIdx: openOrders[0].offerIndex,
      mintSy: syMintAddress,
    });

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

    <Note>
      Use `getUserOpenOrders` to find the `offerIdx` of the order you want to remove. Removing an order returns PT/YT back to your escrow balance.
    </Note>
  </Step>

  <Step title="Collect Interest">
    Collect yield that has accrued on YT held in your open or filled orders.

    ```typescript theme={null}
    const ix = await orderbook.ixWrapperCollectInterest({
      trader: wallet.publicKey,
      mintSy: syMintAddress,
    });

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

    <Tip>
      Interest accrues on YT held in open sell orders and from filled buy orders. You can collect it at any time without affecting your active orders.
    </Tip>
  </Step>

  <Step title="Withdraw Funds">
    Withdraw PT, YT, and SY from your orderbook escrow back to your wallet.

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

    const ix = await orderbook.ixWrapperWithdrawFunds({
      trader: wallet.publicKey,
      mintSy: syMintAddress,
      ptAmount: amount("All"),
      ytAmount: amount("All"),
      syAmount: amount("All"),
    });

    const tx = new Transaction().add(ix);
    await sendAndConfirmTransaction(connection, tx, [wallet]);
    ```
  </Step>
</Steps>

<Note>
  Use `amount("All")` to withdraw all available funds, or `amount("Some", [500_000_000n])` for a specific amount. The `amount` helper is imported from `@exponent-labs/exponent-sdk`.
</Note>

## Reading State

Read orderbook and user state without transactions:

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

// Check your escrow balances
const balances = orderbook.getUserBalances(wallet.publicKey);
console.log("PT:", balances.pt);
console.log("YT:", balances.yt);
console.log("SY:", balances.sy);
console.log("Staked YT:", balances.stakedYt);
console.log("Staged interest:", balances.staged);

// Get a quote for a potential trade
const clock = await connection.getClock();
const quote = orderbook.getQuote({
  inAmount: 1_000_000_000,
  direction: QuoteDirection.SY_TO_YT,
  unixNow: Number(clock.unixTimestamp),
  syExchangeRate: 1.0001,
});

// View open orders
const orders = orderbook.getUserOpenOrders(wallet.publicKey);
```

## Next Steps

<CardGroup cols={2}>
  <Card href="/developer-orderbook/concepts" title="Core Concepts">
    Understand escrows, fee decay, interest accrual, expiry behavior, and the account model behind the orderbook.
  </Card>

  <Card href="/developer-orderbook/virtual-offers" title="Virtual Offers">
    Learn how PT trades route through shared YT liquidity using strip and merge flows under the hood.
  </Card>

  <Card href="/developer-orderbook/typescript/instructions/overview" title="TypeScript Instructions">
    Explore the full set of instruction builders for posting offers, taking liquidity, removing orders, collecting interest, and withdrawing funds.
  </Card>

  <Card href="/developer-orderbook/typescript/read-functions/overview" title="Read Functions">
    Query quotes, balances, open orders, and other orderbook state without sending transactions.
  </Card>
</CardGroup>
