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

# PumpSwap recipe: pools and swaps after migration

PumpSwap is where a pump.fun token trades once its bonding curve completes. This recipe finds the pool from the migration, decodes pool creations and every swap variant with the published IDL, and was checked against the balances and vaults of real mainnet transactions.

For a PumpSwap swap decode, match each top-level instruction to the PumpSwap program ID and decode it with the IDL that pump.fun publishes. A buy fixes either the tokens received or the quote spent, and a sell fixes the tokens given up. Before any of that, the pump.fun migrate instruction names the pool a graduating token will trade in.

## Program IDs and IDLs

| What | Value |
| --- | --- |
| PumpSwap program ID | `pAMMBay6oceH9fJKBRHGP5D4bD4sWpmSwMn52FMfXEA` |
| pump.fun program ID | `6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P` |
| IDLs | `pump_amm.json` and `pump.json` from pump-fun/pump-public-docs, commit 639b87f |
| Decoder | `anchor.ts` from the [Anchor decoding guide](https://solanashreds.xyz/docs/decode/anchor-instructions), plus the two files below |
| Runtime | Node 24, no dependencies |

Both IDLs carry an `address` field equal to the program ID, and both programs were executable on mainnet when we checked. The [program IDs page](https://solanashreds.xyz/learn/program-ids) lists them with the other DEX programs.

## From bonding curve to pool

A pump.fun token trades on its [bonding curve](https://solanashreds.xyz/learn/pump-fun-bonding-curve) until the curve completes. Then a top-level pump.fun `migrate` instruction moves the liquidity into a PumpSwap pool. Its account list, in IDL order, includes the token as `mint` (position 3) and the new pool as `pool` (position 10), so a decoder sees the pool address in the same transaction that creates it, before the pool has traded at all.

The pool itself is created by a PumpSwap `create_pool` that `migrate` calls internally. That call is a [cross-program invocation](https://solanashreds.xyz/learn/inner-instructions-and-cpi): it is not in the transaction bytes, so a shred decoder never sees it directly. It appears only in the inner instructions that RPC returns after execution. In the migration we decoded at slot 470,939,696, that inner `create_pool` put 206,900,000,000,000 raw units of the token and 150,941,143 lamports of wrapped SOL into the pool.

Do not treat every successful `migrate` as a new pool. The other migration in our sample, at slot 470,939,762, succeeded but did nothing: its log reads "Bonding curve already migrated", and it has no inner instructions. Its pool already existed. In both cases the pool's earliest swaps we found were in the same slot as the migration, so a decoder should expect trades on a new pool immediately.

PumpSwap pools can also be created directly, by a top-level `create_pool`. All three direct creations in our sample were signed by the account named as the coin creator.

## Swap instructions and what they fix

| Instruction | User fixes | Amount field | Limit field | Tokens leave | Tokens arrive |
| --- | --- | --- | --- | --- | --- |
| `buy` | tokens out | `base_amount_out` | `max_quote_amount_in` | `user_quote_token_account` | `user_base_token_account` |
| `buy_exact_quote_in` | quote in | `spendable_quote_in` | `min_base_amount_out` | `user_quote_token_account` | `user_base_token_account` |
| `sell` | tokens in | `base_amount_in` | `min_quote_amount_out` | `user_base_token_account` | `user_quote_token_account` |
| `create_pool` | deposit | `base_amount_in`, `quote_amount_in` | none | creator | the new pool |

The IDL also has `_v2` forms of `buy`, `buy_exact_quote_in` and `sell`; the recipe maps them the same way, and one `sell_v2` appeared in our sample. The user is the `user` account and must sign.

## Steps

1. Keep the transactions whose top-level instructions call PumpSwap or pump.fun. Resolve [lookup table](https://solanashreds.xyz/docs/decode/resolve-lookup-tables) addresses first if the transaction is v0 or v1.
2. For pump.fun `migrate`, record the mint and pool from its accounts as a candidate pool.
3. For PumpSwap, decode with `SwapDecoder` and `PoolDecoder` below. Each returns one plain object or `null`.
4. Join swaps to pools on the pool address, and order them by slot and position in the block.
5. After confirmation, use RPC to settle what the instructions could not say: whether the transaction succeeded, whether a `migrate` actually created a pool, and the amounts that really moved.

## src/swaps.ts

One module covers the swaps of six DEX programs; the PumpSwap mapping is the first entry in `MAPS`.

```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)));
}
```

Run it with the IDL folder, the transaction in base64, and any addresses loaded from lookup tables:

```bash
node src/decode-dex.ts fixtures/idls "$TX_BASE64" "$LOADED_WRITABLE" "$LOADED_READONLY"
```

## A real pool and a real swap, decoded

| Field | Pool creation, slot 470,928,022 | Swap, slot 470,928,476 |
| --- | --- | --- |
| `instruction` | `create_pool` | `buy_exact_quote_in` |
| `pool` | `EiCNb6JWdg7H2UENoi5ZMDG69bFoNYkF39rdXusK5a8Z` | `FVPeKS3rfrpF7ZzfXvo3PRGQ6XKpXc47ocyLhPpmN5vz` |
| creator or user | `3kkqFc66Qp4K6NqK3opuM93PSYCBeGoWdo8z8mxUtLbK` | `6MGz9PUXfW9GEKj1DsMiit2wv1txC54rx2qQTNegyHW9` |
| amounts | base 86,990,000,000, quote 199,002,201,000,000 | exact in: 2,188,715 |
| limit | none | at least 3,197,003,676 out |
| mints | base `So11111111111111111111111111111111111111112`, quote `EcygPriCFGis8k9w7fNMPnr8qvLjcSMq5jpmCxrK929J` | not in the swap's own fields |

The pool creation has wrapped SOL as its base mint: the program accepts any pair, so never assume SOL is the quote side. Amounts are raw units; divide by each mint's decimals before showing them.

## How this code was tested

| Check | Result |
| --- | --- |
| PumpSwap swaps decoded (successful transactions) | `buy` 9, `buy_exact_quote_in` 63, `sell` 74, `sell_v2` 1 |
| The user signed the transaction | every one |
| Exact side equals the user's token balance change, where no other instruction touched that account | 2/2 `buy`, 36/36 `buy_exact_quote_in`, 19/19 `sell`, 1/1 `sell_v2` |
| Limit respected on the other side, same condition | 7/7, 24/24, 56/56 |
| Direct pool creations: decoded pool equals the pool the sample was found by; deposit equals the new vault balances | 3/3, 3/3 |
| Migrations: `migrate` names the pool; inner `create_pool` has that pool and the token as base, or the log says already migrated; swaps on the pool follow | 2/2 |

The balance checks skip token accounts that another instruction in the same transaction also touched, such as a second swap in a route, because then the change is not the swap's alone. The tests are `test-swaps.ts`, `test-pools.ts` and `test-migrations.ts`, and they run in the examples' `test-all.sh`.

## What the instructions cannot tell you

The instruction gives the side the user fixed and the worst price they accepted, not the price they got: for the other side you need the token balance changes or the program's event after execution. A swap does not name the mints, only the user's token accounts and the pool, so join to the pool creation or read the pool account. A `migrate` names a pool even when it creates nothing. And swaps that a router or a bot program calls internally are inner instructions, invisible in shreds.

## Earlier and later in a token's life

Before migration, the token trades on pump.fun: the [pump.fun recipe](https://solanashreds.xyz/docs/programs/pump-fun) decodes creates, buys and sells on the curve. For swaps that pass through PumpSwap inside an aggregator, see the [Jupiter recipe](https://solanashreds.xyz/docs/programs/jupiter). To receive these transactions as the leader sends them, [compare the plans](https://solanashreds.xyz/pricing).
