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

# Trade PT Exact Out

> Low-level instruction to execute an exact-out swap for PT or SY tokens

# createTradePtExactOutInstruction

Builds a raw instruction to execute an exact-out swap to receive a specific amount of PT or SY tokens. Unlike the exact-in variant, this instruction guarantees the output amount and calculates the required input. An optional price limit provides anti-sandwich protection.

## Usage

```typescript theme={null}
import { createTradePtExactOutInstruction } from "@exponent-labs/exponent-sdk/client/clmm";
import { PublicKey, SystemProgram } from "@solana/web3.js";

const ix = createTradePtExactOutInstruction({
  trader: traderKeypair.publicKey,
  market: marketAddress,
  ticks: ticksAccount,
  tokenSyTrader: traderSyTokenAccount,
  tokenPtTrader: traderPtTokenAccount,
  tokenSyEscrow: marketSyEscrow,
  tokenPtEscrow: marketPtEscrow,
  addressLookupTable: lookupTableAddress,
  tokenProgram: tokenProgramId,
  syProgram: syProgramId,
  tokenFeeTreasurySy: feeTreasurySyAccount,
  tokenFeeTreasuryPt: feeTreasuryPtAccount,
  eventAuthority: eventAuthorityPda,
  program: clmmProgramId,
  amountOut: 1_000_000_000n,
  swapDirection: { buyPt: {} },
  amountInConstraint: 1_100_000_000n,
  priceSpotLimit: 1.05,
});
```

## Accounts

| Name                 | Type        | Signer | Writable | Description                   |
| -------------------- | ----------- | ------ | -------- | ----------------------------- |
| `trader`             | `PublicKey` | Yes    | Yes      | Wallet executing the trade    |
| `market`             | `PublicKey` | No     | Yes      | The CLMM market account       |
| `ticks`              | `PublicKey` | No     | Yes      | The market tick array account |
| `tokenSyTrader`      | `PublicKey` | No     | Yes      | Trader's SY token account     |
| `tokenPtTrader`      | `PublicKey` | No     | Yes      | Trader's PT token account     |
| `tokenSyEscrow`      | `PublicKey` | No     | Yes      | Market SY escrow account      |
| `tokenPtEscrow`      | `PublicKey` | No     | Yes      | Market PT escrow account      |
| `addressLookupTable` | `PublicKey` | No     | No       | Market address lookup table   |
| `tokenProgram`       | `PublicKey` | No     | No       | SPL Token program             |
| `syProgram`          | `PublicKey` | No     | No       | SY program                    |
| `tokenFeeTreasurySy` | `PublicKey` | No     | Yes      | SY fee treasury account       |
| `tokenFeeTreasuryPt` | `PublicKey` | No     | Yes      | PT fee treasury account       |
| `eventAuthority`     | `PublicKey` | No     | No       | Event authority PDA           |
| `program`            | `PublicKey` | No     | No       | CLMM program ID               |

## Args

| Name                 | Type             | Description                                |
| -------------------- | ---------------- | ------------------------------------------ |
| `amountOut`          | `u64`            | Exact amount of output tokens desired      |
| `swapDirection`      | `SwapDirection`  | Direction of the swap: `BuyPt` or `SellPt` |
| `amountInConstraint` | `u64` (optional) | Maximum amount of input tokens to spend    |
| `priceSpotLimit`     | `f64` (optional) | Price limit for anti-sandwich protection   |

## Returns

`TransactionInstruction` — a transaction instruction ready to be added to a transaction.
