Canonical: https://solanashreds.xyz/docs/programs/meteora

# Meteora recipe: DLMM and DAMM pools

Meteora runs several pool programs side by side, so the first job is telling them apart by program ID. This recipe decodes DLMM swaps and DAMM v2 swaps and pool creations with Meteora's published IDLs, follows bonding-curve graduations into DAMM v2, and was tested on real mainnet transactions.

For a Meteora DLMM swap decode, first confirm the program ID, since Meteora runs several pool programs with different layouts. DLMM swaps carry an input and minimum output, or an exact output and maximum input, and name the bin arrays they may cross. DAMM v2 swaps decode with their own IDL, including a mode byte.

## The Meteora programs and their IDs

| Program | Program ID | Covered here |
| --- | --- | --- |
| DLMM | `LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo` | yes, IDL `dlmm.json` from dlmm-sdk 1532e54 |
| DAMM v2 | `cpamdpZCGKUy5JxQXB4dcpGPiikHawvSWAd6mEn1sGG` | yes, IDL `cp_amm.json` from damm-v2-sdk eab5e8a |
| Dynamic Bonding Curve | `dbcij3LWUppWqq96dh6gJWwBifmcGfLSB5D4DuSMaqN` | its graduation into DAMM v2, IDL read from chain |
| DLMM Pro | `dPro1D5wiqnGMH8vMDG23jh35btTHoWdyqXjqkqxe8n` | no: a separate, tick-based program with its own IDL |

The names are close and the layouts are not. A DLMM decoder pointed at DLMM Pro, or a DAMM v2 decoder pointed at the older DAMM pools, will misread every field, so key everything on the program ID first. All four were executable on mainnet when we checked, and the [program IDs page](https://solanashreds.xyz/learn/program-ids) lists the first three with their sources.

## Swap and pool instructions

| Program, instruction | User fixes | Amount | Limit | User accounts |
| --- | --- | --- | --- | --- |
| DLMM `swap`, `swap2` | input | `amount_in` | `min_amount_out` | `user_token_in`, `user_token_out`, signer `user` |
| DLMM `swap_exact_out`, `swap_exact_out2` | output | `out_amount` | `max_in_amount` | same |
| DAMM v2 `swap` | input | `amount_in` | `minimum_amount_out` | `input_token_account`, `output_token_account`, signer `payer` |
| DAMM v2 `swap2` | set by `swap_mode` | `amount_0` | `amount_1` | same |
| DLMM `initialize_lb_pair2` and the customizable variants | starting bin | `active_id` | none | `funder`, `token_mint_x`, `token_mint_y`, `lb_pair` |
| DAMM v2 `initialize_pool`, `initialize_customizable_pool`, `initialize_pool_with_dynamic_config` | starting price | `sqrt_price` | none | `creator`, `token_a_mint`, `token_b_mint`, `pool` |

The DLMM `2` variants add a `memo_program` account and a `remaining_accounts_info` argument; the amount and limit fields are the same. Neither DLMM nor DAMM v2 swaps name the mints directly.

## DAMM v2 swap modes and DLMM bins

DAMM v2's `swap2` packs its arguments into one parameter struct with a `swap_mode` byte. Meteora's source defines 0 as exact in, 1 as partial fill and 2 as exact out. For exact in, `amount_0` is the input and `amount_1` the minimum output; for exact out, `amount_0` is the output and `amount_1` the maximum input. Partial fill accepts up to `amount_0` of input and may use less, so `swaps.ts` returns it as exact in with `partial: true`. None of the 61 `swap2` calls in our sample used partial fill.

DLMM keeps liquidity in bins, each a single price, grouped 70 to a bin array. A swap lists the bin arrays it may cross after its named accounts: 3 or 5 in the `swap2` calls we decoded. The price of bin `i` is (1 + bin step / 10,000) to the power `i`, and the bin step is a pool setting that the creation instruction does not carry: `initialize_lb_pair2` takes it from a preset parameter account. [Concentrated liquidity on Solana](https://solanashreds.xyz/learn/concentrated-liquidity-on-solana) compares bins with ticks.

## Bonding-curve graduations into DAMM v2

One of the three DAMM v2 pool creations in our sample was not a top-level call. A top-level Dynamic Bonding Curve `migration_damm_v2` created it by calling DAMM v2's `initialize_pool_with_dynamic_config` internally. As with other launchpads, the inner call is invisible in shreds, but the outer instruction names the new pool in its own `pool` account, together with `base_mint` and `quote_mint`, and in our sample that account matched the pool created. The other two creations were top-level `initialize_customizable_pool` calls, both over the full price range the program allows.

## Steps

1. Keep transactions with a top-level call to DLMM, DAMM v2 or the Dynamic Bonding Curve, resolving [lookup tables](https://solanashreds.xyz/docs/decode/resolve-lookup-tables) first for v0 and v1.
2. Decode DLMM and DAMM v2 calls with `SwapDecoder` and `PoolDecoder` below.
3. For a Dynamic Bonding Curve `migration_damm_v2`, decode it with that program's IDL through `AnchorDecoder` and record `pool` with its mints as a new DAMM v2 pool.
4. Read `partial` on DAMM v2 swaps before you treat the amount as exact.
5. After confirmation, take outcomes and real amounts from the token balance changes.

## src/swaps.ts

The Meteora DLMM and DAMM v2 entries in `MAPS` are the fifth and sixth.

```typescript file="src/swaps.ts"
// One shape for swaps on PumpSwap, Raydium CPMM and CLMM, Orca Whirlpool and Meteora DLMM and DAMM v2, decoded with
// each program's published IDL (fixtures/idls/SOURCES.tsv) through anchor.ts. Names on the right are the IDLs' own.
import { AnchorDecoder } from "./anchor.ts";

export type Swap = {
  program: string; instruction: string;
  pool: string; user: string;                    // the pool traded against and the signer that owns the user accounts
  source: string; destination: string;           // the user's token accounts: tokens leave source, arrive in destination
  exact: "in" | "out";                           // which side the user fixed
  amount: bigint;                                // the fixed side: exact input, or exact output
  limit: bigint;                                 // exact in: least output accepted; exact out: most input accepted
  partial: boolean;                              // true only for DAMM v2 PartialFill: up to `amount` in may be spent
};

export const DEX = {
  pumpswap: "pAMMBay6oceH9fJKBRHGP5D4bD4sWpmSwMn52FMfXEA",
  raydiumCpmm: "CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C",
  raydiumClmm: "CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK",
  orcaWhirlpool: "whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc",
  meteoraDlmm: "LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo",
  meteoraDammV2: "cpamdpZCGKUy5JxQXB4dcpGPiikHawvSWAd6mEn1sGG",
} as const;

type Map = (name: string, x: Record<string, any>, a: Record<string, string | null>) => Omit<Swap, "program" | "instruction" | "partial"> | null;

const MAPS: Record<string, Map> = {
  // PumpSwap: buy fixes the base tokens out; buy_exact_quote_in fixes the quote in; sell fixes the base tokens in.
  [DEX.pumpswap]: (n, x, a) => {
    const base = { pool: a.pool!, user: a.user! };
    if (n === "buy" || n === "buy_v2") return { ...base, source: a.user_quote_token_account!, destination: a.user_base_token_account!, exact: "out", amount: x.base_amount_out, limit: x.max_quote_amount_in };
    if (n.startsWith("buy_exact_quote_in")) return { ...base, source: a.user_quote_token_account!, destination: a.user_base_token_account!, exact: "in", amount: x.spendable_quote_in, limit: x.min_base_amount_out };
    if (n === "sell" || n === "sell_v2") return { ...base, source: a.user_base_token_account!, destination: a.user_quote_token_account!, exact: "in", amount: x.base_amount_in, limit: x.min_quote_amount_out };
    return null;
  },
  // Raydium CPMM: one instruction per direction of exactness.
  [DEX.raydiumCpmm]: (n, x, a) => {
    const base = { pool: a.pool_state!, user: a.payer!, source: a.input_token_account!, destination: a.output_token_account! };
    if (n === "swap_base_input") return { ...base, exact: "in", amount: x.amount_in, limit: x.minimum_amount_out };
    if (n === "swap_base_output") return { ...base, exact: "out", amount: x.amount_out, limit: x.max_amount_in };
    return null;
  },
  // Raydium CLMM: a flag says which side `amount` is; `other_amount_threshold` is the limit on the other side.
  [DEX.raydiumClmm]: (n, x, a) => n === "swap" || n === "swap_v2"
    ? { pool: a.pool_state!, user: a.payer!, source: a.input_token_account!, destination: a.output_token_account!,
        exact: x.is_base_input ? "in" : "out", amount: x.amount, limit: x.other_amount_threshold } : null,
  // Orca Whirlpool: accounts are named by token A and B, so direction comes from `a_to_b`.
  [DEX.orcaWhirlpool]: (n, x, a) => n === "swap" || n === "swap_v2"
    ? { pool: a.whirlpool!, user: a.token_authority!,
        source: (x.a_to_b ? a.token_owner_account_a : a.token_owner_account_b)!, destination: (x.a_to_b ? a.token_owner_account_b : a.token_owner_account_a)!,
        exact: x.amount_specified_is_input ? "in" : "out", amount: x.amount, limit: x.other_amount_threshold } : null,
  // Meteora DLMM: separate exact-in and exact-out instructions, each with a "2" variant that adds remaining accounts.
  [DEX.meteoraDlmm]: (n, x, a) => {
    const base = { pool: a.lb_pair!, user: a.user!, source: a.user_token_in!, destination: a.user_token_out! };
    if (n === "swap" || n === "swap2") return { ...base, exact: "in", amount: x.amount_in, limit: x.min_amount_out };
    if (n === "swap_exact_out" || n === "swap_exact_out2") return { ...base, exact: "out", amount: x.out_amount, limit: x.max_in_amount };
    return null;
  },
  // Meteora DAMM v2: swap is exact in; swap2 carries a mode byte (damm-v2 ix_swap.rs SwapMode: 0 ExactIn, 1 PartialFill, 2 ExactOut).
  [DEX.meteoraDammV2]: (n, x, a) => {
    const base = { pool: a.pool!, user: a.payer!, source: a.input_token_account!, destination: a.output_token_account! };
    const p = x._params;
    if (n === "swap") return { ...base, exact: "in", amount: p.amount_in, limit: p.minimum_amount_out };
    if (n === "swap2") return p.swap_mode === 2 ? { ...base, exact: "out", amount: p.amount_0, limit: p.amount_1 }
      : { ...base, exact: "in", amount: p.amount_0, limit: p.amount_1 }; // PartialFill fills up to amount_0 in
    return null;
  },
};

export class SwapDecoder {
  private decoders = new Map<string, AnchorDecoder>();
  /** idls: program id -> that program's IDL */
  constructor(idls: Record<string, object>) { for (const [p, idl] of Object.entries(idls)) this.decoders.set(p, new AnchorDecoder(idl as any)); }

  decode(programId: string, data: Uint8Array, accounts: string[]): Swap | null {
    const dec = this.decoders.get(programId), map = MAPS[programId];
    if (!dec || !map) return null;
    const d = dec.decodeWithAccounts(data, accounts, programId);
    if (!d) return null;
    const s = map(d.name, d.args as Record<string, any>, d.accounts);
    if (!s) return null;
    const partial = programId === DEX.meteoraDammV2 && d.name === "swap2" && (d.args as any)._params.swap_mode === 1;
    return { program: programId, instruction: d.name, ...s, partial };
  }
}
```

## src/pools.ts

```typescript file="src/pools.ts"
// One shape for new pools on PumpSwap, Raydium CPMM and CLMM, Orca Whirlpool and Meteora DLMM and DAMM v2, decoded
// with each program's published IDL (fixtures/idls/SOURCES.tsv) through anchor.ts. Names on the right are the IDLs' own.
import { AnchorDecoder } from "./anchor.ts";
import { DEX } from "./swaps.ts";

export type PoolCreated = {
  program: string; instruction: string;
  pool: string; creator: string;                 // the new pool account and the account that pays for it
  mintA: string; mintB: string;                  // in the program's own order (base/quote, 0/1, A/B or X/Y)
  amountA: bigint | null; amountB: bigint | null; // initial deposit, when the creation instruction carries one
  sqrtPriceX64: bigint | null;                   // starting price for the sqrt-price designs (Q64.64)
  activeBinId: number | null;                    // starting bin for DLMM
};

type Map = (n: string, x: Record<string, any>, a: Record<string, string | null>) => Omit<PoolCreated, "program" | "instruction"> | null;
const none = { amountA: null, amountB: null, sqrtPriceX64: null, activeBinId: null };

const MAPS: Record<string, Map> = {
  [DEX.pumpswap]: (n, x, a) => n === "create_pool"
    ? { ...none, pool: a.pool!, creator: a.creator!, mintA: a.base_mint!, mintB: a.quote_mint!, amountA: x.base_amount_in, amountB: x.quote_amount_in } : null,
  [DEX.raydiumCpmm]: (n, x, a) => n === "initialize" || n === "initialize_with_permission"
    ? { ...none, pool: a.pool_state!, creator: a.creator!, mintA: a.token_0_mint!, mintB: a.token_1_mint!, amountA: x.init_amount_0, amountB: x.init_amount_1 } : null,
  [DEX.raydiumClmm]: (n, x, a) => n === "create_pool"
    ? { ...none, pool: a.pool_state!, creator: a.pool_creator!, mintA: a.token_mint_0!, mintB: a.token_mint_1!, sqrtPriceX64: x.sqrt_price_x64 } : null,
  [DEX.orcaWhirlpool]: (n, x, a) => n.startsWith("initialize_pool")
    ? { ...none, pool: a.whirlpool!, creator: a.funder!, mintA: a.token_mint_a!, mintB: a.token_mint_b!, sqrtPriceX64: x.initial_sqrt_price } : null,
  [DEX.meteoraDlmm]: (n, x, a) => n === "initialize_lb_pair" || n === "initialize_lb_pair2" || n.startsWith("initialize_customizable_permissionless_lb_pair")
    ? { ...none, pool: a.lb_pair!, creator: a.funder!, mintA: a.token_mint_x!, mintB: a.token_mint_y!, activeBinId: x.active_id ?? x.params.active_id } : null,
  [DEX.meteoraDammV2]: (n, x, a) => n === "initialize_pool" || n === "initialize_customizable_pool" || n === "initialize_pool_with_dynamic_config"
    ? { ...none, pool: a.pool!, creator: a.creator!, mintA: a.token_a_mint!, mintB: a.token_b_mint!, sqrtPriceX64: x.params.sqrt_price } : null,
};

export class PoolDecoder {
  private decoders = new Map<string, AnchorDecoder>();
  /** idls: program id -> that program's IDL */
  constructor(idls: Record<string, object>) { for (const [p, idl] of Object.entries(idls)) this.decoders.set(p, new AnchorDecoder(idl as any)); }

  decode(programId: string, data: Uint8Array, accounts: string[]): PoolCreated | null {
    const dec = this.decoders.get(programId), map = MAPS[programId];
    if (!dec || !map) return null;
    const d = dec.decodeWithAccounts(data, accounts, programId);
    if (!d) return null;
    const p = map(d.name, d.args as Record<string, any>, d.accounts);
    if (!p) return null;
    return { program: programId, instruction: d.name, pool: p.pool, creator: p.creator, mintA: p.mintA, mintB: p.mintB,
      amountA: p.amountA, amountB: p.amountB, sqrtPriceX64: p.sqrtPriceX64, activeBinId: p.activeBinId };
  }
}
```

## src/decode-dex.ts: one transaction

```typescript file="src/decode-dex.ts"
// Print swaps and new pools for one transaction, for the six DEX programs in swaps.ts and pools.ts.
//   node src/decode-dex.ts <idl-dir> <transaction-base64> [loaded-writable,...] [loaded-readonly,...]
import fs from "node:fs";
import { decodeTx } from "./tx.ts";
import { SwapDecoder, DEX } from "./swaps.ts";
import { PoolDecoder } from "./pools.ts";

const [idlDir, txBase64, loadedW = "", loadedR = ""] = process.argv.slice(2);
const idl = (f: string) => JSON.parse(fs.readFileSync(`${idlDir}/${f}`, "utf8"));
const idls = { [DEX.pumpswap]: idl("pump_amm.json"), [DEX.raydiumCpmm]: idl("raydium_cp_swap.json"), [DEX.raydiumClmm]: idl("raydium_clmm.json"),
  [DEX.orcaWhirlpool]: idl("whirlpool.json"), [DEX.meteoraDlmm]: idl("dlmm.json"), [DEX.meteoraDammV2]: idl("cp_amm.json") };
const swaps = new SwapDecoder(idls), pools = new PoolDecoder(idls);
const tx = decodeTx(Buffer.from(txBase64, "base64"));
const keys = [...tx.staticKeys, ...loadedW.split(",").filter(Boolean), ...loadedR.split(",").filter(Boolean)];

for (const ix of tx.instructions) { // top-level calls only: a DEX called by a router or launchpad is a CPI
  const program = keys[ix.programIndex], accounts = ix.accounts.map((i) => keys[i]);
  const event = swaps.decode(program, ix.data, accounts) ?? pools.decode(program, ix.data, accounts);
  if (event) console.log(JSON.stringify({ signature: tx.signatures[0], ...event }, (_k, v) => (typeof v === "bigint" ? v.toString() : v)));
}
```

## Real swaps and a real pool, decoded

| Field | DLMM swap, slot 470,930,436 | DAMM v2 swap, slot 470,931,142 | DAMM v2 pool from a graduation, slot 470,930,588 |
| --- | --- | --- | --- |
| `instruction` | `swap_exact_out2` | `swap2`, mode exact in | `initialize_pool_with_dynamic_config`, inner |
| `pool` | `EXXfbVutbuexDZtPw8pkSvWxLFoUMBHd544CAGkmtPZm` | `AiA5MSW5yKYKrmHY2zqRVxiS9QFtJFPTx6dkVqZp2o3x` | `AvxGG1QTtHHenc7jsxxDNKtvhuxRra5NSmHSLhr3wufE` |
| user or creator | `GuWe3yb1kWa5t53HKudLos6JitzKWcQ1FPJDvr2uXAhK` | `Gyp9FdQTb6nUKDRejtkg4FjGkdBoWmvjpfNsPPeZx324` | `FhVo3mqL8PW5pH5U2CN4XE33DokiyZnUwuGpH2hmHLuM` |
| fixed amount | exact out: 146,678,859 | exact in: 90,972 | none |
| limit or price | at most 5,032,854 in | at least 216,730,104 out | `sqrt_price` 412,481,737,123,559,485 |
| mints | not in the swap's fields | not in the swap's fields | A `9pAJKnkArqWCZo8u6Thp4wGjr7S9Zk3NiwcSjEYHhfdw`, B `So11111111111111111111111111111111111111112` |

## How this code was tested

| Check | Result |
| --- | --- |
| Swaps decoded (successful transactions, top level) | DLMM `swap2` 1, `swap_exact_out2` 1; DAMM v2 `swap` 24, `swap2` 61 |
| The user signed | every one |
| Exact side equals the user's balance change, where no other instruction touched that account | DLMM 2/2; DAMM v2 15/15 `swap`, 30/30 `swap2` |
| Limit respected, same condition | DLMM 2/2; DAMM v2 5/5, 15/15 |
| Pool creations: decoded pool equals the pool the sample was found by; both mints have vaults | DLMM `initialize_lb_pair2` 2/2; DAMM v2 3/3 |
| Dynamic Bonding Curve `migration_damm_v2` names the pool it creates | 1/1 |
| Not seen in our sample, so mapped from the IDL but not tested on real data | DLMM `swap` and `swap_exact_out` (v1) at top level; DAMM v2 partial fill and exact out |

DLMM swaps were rare at top level in our sample: most DLMM swaps we saw came from routers, as inner instructions. The tests are `test-swaps.ts` and `test-pools.ts` in the examples' `test-all.sh`.

## What the instructions cannot tell you

Swaps name the user's token accounts and the pool, not the mints, the price paid or the fee. DLMM prices need the bin step from the pool or its preset, and the active bin at execution time. Graduations and router swaps are inner instructions, so shreds show only the outer call. And our real-data coverage of DLMM top-level swaps is two transactions; check your own traffic before relying on the less common forms.

## Other DEX recipes

The [Orca Whirlpool recipe](https://solanashreds.xyz/docs/programs/orca-whirlpool) and the [Raydium CPMM and CLMM recipe](https://solanashreds.xyz/docs/programs/raydium-cpmm-and-clmm) use the same two files, and the [PumpSwap recipe](https://solanashreds.xyz/docs/programs/pumpswap) covers pump.fun's pools. For DLMM swaps inside routes, see the [Jupiter recipe](https://solanashreds.xyz/docs/programs/jupiter). To watch Meteora pools as blocks are produced, [see the plans](https://solanashreds.xyz/pricing).
