Canonical: https://solanashreds.xyz/docs/programs/raydium-amm

# Raydium AMM v4 recipe: pool creation and swaps

Raydium AMM v4 publishes no IDL, so this recipe decodes it by hand from Raydium's own source: new pools from initialize2, in both of its account layouts, and every swap variant. Written in Rust and checked against what the vaults and users actually held afterwards.

For Raydium new pool detection, watch for top-level AMM v4 instructions with tag 1, `initialize2`. Its data holds the opening time and the two deposit amounts; its accounts name the pool, both mints and both vaults. AMM v4 has no IDL, so the layout comes from Raydium's source, which accepts two account lists for this one instruction.

## Program ID and source

| What | Value |
| --- | --- |
| Program ID | `675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8` |
| Format | not Anchor: one tag byte, then fixed little-endian fields |
| Layout source | `program/src/instruction.rs` and `program/src/processor.rs` in raydium-io/raydium-amm, commit 9c54069 |
| Tags used here | 1 `initialize2`, 9 `swap_base_in`, 11 `swap_base_out`, 16 `swap_base_in_v2`, 17 `swap_base_out_v2` |

## The layouts, from the source

| Instruction | Data after the tag | Accounts that matter |
| --- | --- | --- |
| `initialize2`, 21 accounts | `u8` nonce, `u64` open time, `u64` init pc amount, `u64` init coin amount | pool 4, open orders 6, lp mint 7, coin mint 8, pc mint 9, coin vault 10, pc vault 11, creator 17 |
| `initialize2`, 19 accounts | the same | pool 4, lp mint 6, coin mint 7, pc mint 8, coin vault 9, pc vault 10, creator 15 |
| `swap_base_in` / `swap_base_out` | two `u64`: amount in and minimum out, or maximum in and amount out | pool 1; user source, destination and wallet are the last three of 17 or 18 |
| `swap_base_in_v2` / `swap_base_out_v2` | the same two `u64` | pool 1, source 5, destination 6, wallet 7 |

Two traps hide in that table. The pc amount comes before the coin amount in `initialize2`'s data, the reverse of the account order. And the 19-account form, which the processor calls the recommended one, leaves out two accounts the program never uses, open orders and the market program: positions 6 to 14 are one lower, and the creator's wallet is two lower, at 15. The instruction file's comment lists only the 21-account form; the processor shows both. In our sample of four real pool creations, one used 19 accounts.

## Steps

1. Keep top-level instructions whose program is AMM v4.
2. Read the first byte. Ignore tags this recipe does not handle; deposits, withdrawals and admin calls use other tags.
3. For tag 1, read the three numbers, then pick the account positions by the account count, 19 or 21.
4. For the classic swaps, take the pool from position 1 and the user's accounts from the end of the list, because one optional account in the middle may be missing.
5. For the v2 swaps, take the eight fixed positions.

## src/lib.rs

```rust file="src/lib.rs"
//! Raydium AMM v4 instruction decoding by hand, from raydium-io/raydium-amm program/src/instruction.rs
//! (commit 9c54069): a one-byte tag, then fixed little-endian fields. Accounts are positional.

pub const RAYDIUM_AMM_V4: &str = "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8";

#[derive(Debug, PartialEq)]
pub enum RaydiumEvent {
    /// A new pool. `init_*_amount` are what the creator deposits; the vaults should hold exactly that afterwards.
    NewPool {
        pool: String,
        coin_mint: String,
        pc_mint: String,
        coin_vault: String,
        pc_vault: String,
        lp_mint: String,
        creator: String,
        open_time: u64,
        init_coin_amount: u64,
        init_pc_amount: u64,
    },
    /// Exact input: the user gives `amount_in` and accepts no less than `minimum_amount_out`.
    SwapBaseIn {
        pool: String,
        user: String,
        source: String,
        destination: String,
        amount_in: u64,
        minimum_amount_out: u64,
    },
    /// Exact output: the user wants `amount_out` and pays no more than `max_amount_in`.
    SwapBaseOut {
        pool: String,
        user: String,
        source: String,
        destination: String,
        max_amount_in: u64,
        amount_out: u64,
    },
}

fn u64_at(d: &[u8], o: usize) -> Option<u64> {
    Some(u64::from_le_bytes(d.get(o..o + 8)?.try_into().ok()?))
}

/// `accounts` are the instruction's account addresses in order (lookup tables already resolved).
pub fn decode(data: &[u8], accounts: &[String]) -> Option<RaydiumEvent> {
    let a = |i: usize| accounts.get(i).cloned();
    match *data.first()? {
        // initialize2: nonce u8, open_time u64, init_pc_amount u64, init_coin_amount u64 (note: pc before coin).
        // Two account layouts (processor.rs process_initialize2). With 21 accounts: amm 4, authority 5, open orders 6,
        // lp mint 7, coin mint 8, pc mint 9, coin vault 10, pc vault 11, ..., user wallet 17. The 19-account form,
        // which the program calls recommended, leaves out two accounts it never uses, open orders (6) and the market
        // program (15): positions 6 to 14 are one lower (lp mint 6, coin mint 7, pc mint 8, coin vault 9, pc vault 10)
        // and the user wallet is two lower, at 15.
        1 if data.len() >= 26 && (accounts.len() == 19 || accounts.len() >= 21) => {
            let s = if accounts.len() == 19 { 1 } else { 0 };
            Some(RaydiumEvent::NewPool {
                pool: a(4)?,
                lp_mint: a(7 - s)?,
                coin_mint: a(8 - s)?,
                pc_mint: a(9 - s)?,
                coin_vault: a(10 - s)?,
                pc_vault: a(11 - s)?,
                creator: a(17 - 2 * s)?,
                open_time: u64_at(data, 2)?,
                init_pc_amount: u64_at(data, 10)?,
                init_coin_amount: u64_at(data, 18)?,
            })
        }
        // swap_base_in (9) and swap_base_out (11): 18 accounts, or 17 when the optional target-orders account
        // (position 4) is left out, so the user's accounts are read from the end of the list.
        9 | 11 if data.len() >= 17 && accounts.len() >= 17 => {
            let n = accounts.len();
            let (pool, source, destination, user) = (a(1)?, a(n - 3)?, a(n - 2)?, a(n - 1)?);
            let (x, y) = (u64_at(data, 1)?, u64_at(data, 9)?);
            Some(if data[0] == 9 {
                RaydiumEvent::SwapBaseIn {
                    pool,
                    user,
                    source,
                    destination,
                    amount_in: x,
                    minimum_amount_out: y,
                }
            } else {
                RaydiumEvent::SwapBaseOut {
                    pool,
                    user,
                    source,
                    destination,
                    max_amount_in: x,
                    amount_out: y,
                }
            })
        }
        // swap_base_in_v2 (16) and swap_base_out_v2 (17): no order book accounts, 8 fixed positions.
        16 | 17 if data.len() >= 17 && accounts.len() >= 8 => {
            let (pool, source, destination, user) = (a(1)?, a(5)?, a(6)?, a(7)?);
            let (x, y) = (u64_at(data, 1)?, u64_at(data, 9)?);
            Some(if data[0] == 16 {
                RaydiumEvent::SwapBaseIn {
                    pool,
                    user,
                    source,
                    destination,
                    amount_in: x,
                    minimum_amount_out: y,
                }
            } else {
                RaydiumEvent::SwapBaseOut {
                    pool,
                    user,
                    source,
                    destination,
                    max_amount_in: x,
                    amount_out: y,
                }
            })
        }
        _ => None,
    }
}
```

It needs no dependencies. Call `decode(&ix.data, &accounts)` with each instruction's resolved account addresses, for example from the transactions the [Rust decoder](https://solanashreds.xyz/docs/decode/rust) prints, after [resolving lookup tables](https://solanashreds.xyz/docs/decode/resolve-lookup-tables) for v0 transactions.

## A real new pool, decoded

This `initialize2` from mainnet slot 470,834,211 used the 21-account layout. Its 26 data bytes, `01 fe b37f066b00000000 00e1f50500000000 00406352bfc60100`, decode as:

| Field | Bytes | Value |
| --- | --- | --- |
| Tag | `01` | `initialize2` |
| Nonce | `fe` | 254 |
| Open time | `b37f066b00000000` | 1,795,588,019, a Unix time: 2026-11-25 06:26:59 UTC |
| Init pc amount | `00e1f50500000000` | 100,000,000 base units of the pc mint, wrapped SOL here: 0.1 SOL |
| Init coin amount | `00406352bfc60100` | 500,000,000,000,000 base units of the new token |

After the transaction, the coin vault at position 10 held exactly 500,000,000,000,000 of the coin mint and the pc vault at position 11 held exactly 100,000,000 of wrapped SOL. The open time sits a week after the transaction: a pool can be created before trading on it is allowed.

## How this code was tested

Every successful top-level AMM v4 instruction in the fixtures, checked against the transaction's own token balances:

| Check | Result |
| --- | --- |
| New pools: each vault holds exactly its init amount, of the mint at the matching position | 4 of 4 (three 21-account, one 19-account) |
| New pools: the creator position is a signer of the transaction | 4 of 4 |
| Swaps: the exact side moved the user's token account by exactly that amount | 3 of 3 checkable (the fourth account was touched by another instruction too) |

Fixtures: four pool creations found through Raydium's pool-creation fee account and four swaps from recent blocks, all finalized mainnet transactions fetched on 18 November 2026. Built with Rust 1.98.1.

## Reading a layout from source when there is no IDL

Without an IDL, the program's instruction parser is the specification. Look for the function that reads the tag byte and unpacks each variant, note each field's width and order, then read the processor to learn which accounts it takes and in what order. Doc comments are a guide, not a contract: here the comment documents one account list while the code accepts two. Test against real transactions whose effects you can see, such as vault balances, before you trust a layout. [Reading an IDL](https://solanashreds.xyz/learn/reading-an-idl) covers the case where one exists, and [the Borsh rules](https://solanashreds.xyz/learn/borsh-encoding) explain why a hand layout like this one is not Borsh.

## What the instruction cannot tell you

A swap instruction carries the user's limit, not the fill: `swap_base_in` names what goes in and the least that must come out. The amount received and whether the swap succeeded come from execution. Swaps routed through an aggregator reach AMM v4 as inner instructions, which raw transactions do not contain, so most routed volume never shows up here; [reading a DEX swap](https://solanashreds.xyz/learn/how-swaps-look-as-instructions) and [DEX volume before confirmation](https://solanashreds.xyz/blog/dex-volume-before-confirmation) go into that gap.

## Other Raydium programs

Raydium's newer pools run on separate programs with published IDLs; the [CPMM and CLMM recipe](https://solanashreds.xyz/docs/programs/raydium-cpmm-and-clmm) covers them. For other ways to spot new tokens and pools, see [token launch data sources](https://solanashreds.xyz/compare/new-token-data-sources), and to run this on your own feed, [see pricing](https://solanashreds.xyz/pricing).
