# Raydium AMM v4 decoder (Carbon 2.0)

> Decode Raydium AMM v4 instructions and accounts in Rust with carbon-raydium-amm-v4-decoder 2.0.0. Program ID 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8.

`carbon-raydium-amm-v4-decoder` decodes the instructions and accounts of the Raydium AMM v4 program (`675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8`): 18 instructions, 3 account types. It is generated with Codama from the program's IDL; see [decoders](https://carbon.sevenlabs.org/concepts/decoders.md) for how decoders plug into a pipeline.

|                  |                                                                                                                       |
| ---------------- | --------------------------------------------------------------------------------------------------------------------- |
| Crate            | [`carbon-raydium-amm-v4-decoder`](https://crates.io/crates/carbon-raydium-amm-v4-decoder/2.0.0) 2.0.0                 |
| Install          | `cargo add carbon-raydium-amm-v4-decoder@2.0.0`                                                                       |
| Program ID       | `675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8` (`carbon_raydium_amm_v4_decoder::PROGRAM_ID`)                          |
| Category         | [DEX & AMM](https://carbon.sevenlabs.org/decoders/dex-and-amm.md)                                                     |
| Decoder          | `carbon_raydium_amm_v4_decoder::RaydiumAmmV4Decoder`                                                                  |
| Instruction enum | `instructions::RaydiumAmmV4Instruction`                                                                               |
| Account enum     | `accounts::RaydiumAmmV4Account`                                                                                       |
| Features         | `serde`, `postgres`, `graphql` (none enabled by default)                                                              |
| API docs         | [docs.rs/carbon-raydium-amm-v4-decoder/2.0.0](https://docs.rs/carbon-raydium-amm-v4-decoder/2.0.0)                    |
| Source           | [decoders/raydium-amm-v4-decoder](https://github.com/sevenlabs-hq/carbon/tree/v2.0.0/decoders/raydium-amm-v4-decoder) |

Other DEX and AMM decoders: [BonkSwap](https://carbon.sevenlabs.org/decoders/bonkswap.md), [FluxBeam](https://carbon.sevenlabs.org/decoders/fluxbeam.md), [Gavel](https://carbon.sevenlabs.org/decoders/gavel.md), [Heaven](https://carbon.sevenlabs.org/decoders/heaven.md), [Jupiter Limit Order](https://carbon.sevenlabs.org/decoders/jupiter-limit-order.md), [Jupiter Limit Order v2](https://carbon.sevenlabs.org/decoders/jupiter-limit-order-2.md), [Kamino Limit Order](https://carbon.sevenlabs.org/decoders/kamino-limit-order.md), [Lifinity AMM v2](https://carbon.sevenlabs.org/decoders/lifinity-amm-v2.md), [Meteora DAMM v2](https://carbon.sevenlabs.org/decoders/meteora-damm-v2.md), [Meteora DLMM](https://carbon.sevenlabs.org/decoders/meteora-dlmm.md), [Meteora Pools](https://carbon.sevenlabs.org/decoders/meteora-pools.md), [OpenBook v2](https://carbon.sevenlabs.org/decoders/openbook-v2.md), [Orca Whirlpools](https://carbon.sevenlabs.org/decoders/orca-whirlpool.md), [PancakeSwap](https://carbon.sevenlabs.org/decoders/pancake-swap.md), [Phoenix v1](https://carbon.sevenlabs.org/decoders/phoenix-v1.md), [PumpSwap](https://carbon.sevenlabs.org/decoders/pump-swap.md), [Raydium CLMM](https://carbon.sevenlabs.org/decoders/raydium-clmm.md), [Raydium CPMM](https://carbon.sevenlabs.org/decoders/raydium-cpmm.md), [Raydium Stable Swap](https://carbon.sevenlabs.org/decoders/raydium-stable-swap.md), [Stabble Stable Swap](https://carbon.sevenlabs.org/decoders/stabble-stable-swap.md), [Stabble Weighted Swap](https://carbon.sevenlabs.org/decoders/stabble-weighted-swap.md). To decode several in one pipeline, see [dEX and AMM decoders](https://carbon.sevenlabs.org/decoders/dex-and-amm.md).

The `serde` feature derives `Serialize`/`Deserialize` on every type, `postgres` adds tables and processors for [Postgres](https://carbon.sevenlabs.org/guides/postgres.md), and `graphql` adds a schema for the [GraphQL server](https://carbon.sevenlabs.org/guides/graphql.md).

## Usage

This program crawls recent Raydium AMM v4 transactions from the public RPC and prints every decoded `SwapBaseIn` instruction. Add the dependencies:

```sh
cargo add carbon-core@2.0.0 carbon-raydium-amm-v4-decoder@2.0.0 carbon-rpc-transaction-crawler-datasource@2.0.0 carbon-log-metrics@2.0.0 env_logger tokio --features tokio/full
```

`src/main.rs`:

```rust
use {
    carbon_core::{
        error::CarbonResult, instruction::InstructionProcessorInputType, pipeline::Pipeline,
        processor::Processor,
    },
    carbon_log_metrics::LogMetrics,
    carbon_raydium_amm_v4_decoder::{
        instructions::RaydiumAmmV4Instruction, RaydiumAmmV4Decoder, PROGRAM_ID,
    },
    carbon_rpc_transaction_crawler_datasource::{ConnectionConfig, Filters, RpcTransactionCrawler},
    std::sync::Arc,
};

#[tokio::main]
async fn main() -> CarbonResult<()> {
    env_logger::init();

    // Fetches the program's recent transactions over JSON-RPC, newest first.
    let datasource = RpcTransactionCrawler::new(
        "https://api.mainnet-beta.solana.com".to_string(),
        PROGRAM_ID,
        ConnectionConfig::default(),
        Filters::new(None, None, None),
        None,
    );

    Pipeline::builder()
        .datasource(datasource)
        .metrics(Arc::new(LogMetrics::new()))
        .instruction(RaydiumAmmV4Decoder, InstructionLogger)
        .build()?
        .run()
        .await
}

struct InstructionLogger;

impl Processor<InstructionProcessorInputType<'_, RaydiumAmmV4Instruction>> for InstructionLogger {
    async fn process(
        &mut self,
        input: &InstructionProcessorInputType<'_, RaydiumAmmV4Instruction>,
    ) -> CarbonResult<()> {
        let signature = input.metadata.transaction_metadata.signature;
        if let RaydiumAmmV4Instruction::SwapBaseIn { data, accounts, .. } = input.decoded_instruction {
            println!("swap_base_in {signature}: {data:?} token_program={}", accounts.token_program);
        }
        Ok(())
    }
}
```

```sh
RUST_LOG=info cargo run
```

Each `SwapBaseIn` instruction prints one line with the transaction signature and the decoded `SwapBaseIn` data. The public endpoint rate-limits requests, so the first lines can take a while; use your own RPC URL for anything beyond a test. See [stream with RPC](https://carbon.sevenlabs.org/guides/stream-with-rpc.md) for the crawler options.

### Decode accounts

The transaction crawler emits transactions only. To decode accounts, use a datasource that emits account updates. This program subscribes to the program's accounts with `programSubscribe` on the public WebSocket endpoint and prints every decoded `AmmInfo` account as it changes. Add the datasource and the Solana RPC types it takes:

```sh
cargo add carbon-rpc-program-subscribe-datasource@2.0.0 solana-client@4.2.2
```

`src/main.rs`:

```rust
use {
    carbon_core::{
        account::AccountProcessorInputType, error::CarbonResult, pipeline::Pipeline,
        processor::Processor,
    },
    carbon_log_metrics::LogMetrics,
    carbon_raydium_amm_v4_decoder::{accounts::RaydiumAmmV4Account, RaydiumAmmV4Decoder, PROGRAM_ID},
    carbon_rpc_program_subscribe_datasource::{Filters, RpcProgramSubscribe},
    solana_client::rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig, UiAccountEncoding},
    std::sync::Arc,
};

#[tokio::main]
async fn main() -> CarbonResult<()> {
    env_logger::init();

    // Streams every change to an account owned by the program (programSubscribe).
    let config = RpcProgramAccountsConfig {
        account_config: RpcAccountInfoConfig {
            encoding: Some(UiAccountEncoding::Base64),
            ..Default::default()
        },
        ..Default::default()
    };
    let datasource = RpcProgramSubscribe::new(
        "wss://api.mainnet-beta.solana.com".to_string(),
        Filters::new(PROGRAM_ID, Some(config)),
    );

    Pipeline::builder()
        .datasource(datasource)
        .metrics(Arc::new(LogMetrics::new()))
        .account(RaydiumAmmV4Decoder, AccountLogger)
        .build()?
        .run()
        .await
}

struct AccountLogger;

impl Processor<AccountProcessorInputType<'_, RaydiumAmmV4Account>> for AccountLogger {
    async fn process(
        &mut self,
        input: &AccountProcessorInputType<'_, RaydiumAmmV4Account>,
    ) -> CarbonResult<()> {
        if let RaydiumAmmV4Account::AmmInfo(amm_info) = &input.decoded_account.data {
            println!("amm_info {}: {amm_info:?}", input.metadata.pubkey);
        }
        Ok(())
    }
}
```

```sh
RUST_LOG=info cargo run
```

Each update to a `AmmInfo` account prints its address and decoded fields; the program runs until you stop it. Cargo resolves `solana-client` to 4.2.2, the exact version the datasource pins, so both use the same `RpcProgramAccountsConfig` type. To load every existing account once instead of streaming changes, see [load account state](https://carbon.sevenlabs.org/guides/load-account-state.md).

## Instructions

Variants of `RaydiumAmmV4Instruction`. Each is `RaydiumAmmV4Instruction::<Variant> { program_id, data, accounts }`, where `data` and `accounts` are the types listed under the variant, exported from `carbon_raydium_amm_v4_decoder::instructions`. Discriminators are the leading bytes of the instruction data, in hex.

**How an instruction decodes.** The decoder returns a variant only when all of these hold; otherwise the instruction is skipped and no processor is called:

1. The instruction's program ID is `675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8`.
2. The data starts with the variant's discriminator, and the rest deserializes into its data type.
3. The instruction has at least as many accounts as the variant requires (the "Required accounts" count under each variant).

**Accounts.** They are matched by position, in the order listed. An account marked _optional_ is `None` only when the instruction ends before it; Anchor programs pass the program ID as a placeholder for an omitted optional account, so check for `Some(PROGRAM_ID)` too. Accounts after the listed ones are in `accounts.remaining` as `AccountMeta`s. The decoder doesn't record whether an account signed or was writable: read that from `input.raw_instruction.accounts[i].is_signer` and `.is_writable`, which follow the same order. It doesn't check signer, writable, owner or PDA constraints either; the program enforced those when the transaction executed, so a decoded instruction from a successful transaction already satisfied them.

### `AdminCancelOrders`

> _From the program's IDL:_
>
> Admin: cancel up to `limit` of the pool's open orders.

Discriminator `0d` · data `AdminCancelOrders` · accounts `AdminCancelOrdersInstructionAccounts`.

| Field   | Type  |
| ------- | ----- |
| `limit` | `u16` |

Required accounts: 17 (18 listed); extras go to `remaining`.

Fields of `AdminCancelOrdersInstructionAccounts`, in account order:

| #   | Field                 | Type               |
| --- | --------------------- | ------------------ |
| 0   | `token_program`       | `Pubkey`           |
| 1   | `amm`                 | `Pubkey`           |
| 2   | `amm_authority`       | `Pubkey`           |
| 3   | `amm_open_orders`     | `Pubkey`           |
| 4   | `amm_target_orders`   | `Pubkey`           |
| 5   | `amm_coin_vault`      | `Pubkey`           |
| 6   | `amm_pc_vault`        | `Pubkey`           |
| 7   | `amm_config`          | `Pubkey`           |
| 8   | `admin`               | `Pubkey`           |
| 9   | `market_program`      | `Pubkey`           |
| 10  | `market`              | `Pubkey`           |
| 11  | `market_coin_vault`   | `Pubkey`           |
| 12  | `market_pc_vault`     | `Pubkey`           |
| 13  | `market_vault_signer` | `Pubkey`           |
| 14  | `market_event_queue`  | `Pubkey`           |
| 15  | `market_bids`         | `Pubkey`           |
| 16  | `market_asks`         | `Pubkey`           |
| 17  | `referrer_pc_account` | `Option<Pubkey>`   |
| 18… | `remaining`           | `Vec<AccountMeta>` |

### `CreateConfigAccount`

> _From the program's IDL:_
>
> Admin: create the singleton AmmConfig account.

Discriminator `0e` · data `CreateConfigAccount` · accounts `CreateConfigAccountInstructionAccounts`.

No data fields.

Required accounts: 5; extras go to `remaining`.

Fields of `CreateConfigAccountInstructionAccounts`, in account order:

| #  | Field            | Type               |
| -- | ---------------- | ------------------ |
| 0  | `admin`          | `Pubkey`           |
| 1  | `amm_config`     | `Pubkey`           |
| 2  | `owner`          | `Pubkey`           |
| 3  | `system_program` | `Pubkey`           |
| 4  | `rent`           | `Pubkey`           |
| 5… | `remaining`      | `Vec<AccountMeta>` |

### `Deposit`

> _From the program's IDL:_
>
> Deposit tokens into the pool. Mints LP tokens to the user.

Discriminator `03` · data `Deposit` · accounts `DepositInstructionAccounts`.

| Field              | Type          | Description                                             |
| ------------------ | ------------- | ------------------------------------------------------- |
| `max_coin_amount`  | `u64`         |                                                         |
| `max_pc_amount`    | `u64`         |                                                         |
| `base_side`        | `u64`         |                                                         |
| `other_amount_min` | `Option<u64>` | Optional trailing slippage bound; absent in legacy txs. |

Required accounts: 14; extras go to `remaining`.

Fields of `DepositInstructionAccounts`, in account order:

| #   | Field                | Type               |
| --- | -------------------- | ------------------ |
| 0   | `token_program`      | `Pubkey`           |
| 1   | `amm`                | `Pubkey`           |
| 2   | `amm_authority`      | `Pubkey`           |
| 3   | `amm_open_orders`    | `Pubkey`           |
| 4   | `amm_target_orders`  | `Pubkey`           |
| 5   | `amm_lp_mint`        | `Pubkey`           |
| 6   | `amm_coin_vault`     | `Pubkey`           |
| 7   | `amm_pc_vault`       | `Pubkey`           |
| 8   | `market`             | `Pubkey`           |
| 9   | `user_token_coin`    | `Pubkey`           |
| 10  | `user_token_pc`      | `Pubkey`           |
| 11  | `user_token_lp`      | `Pubkey`           |
| 12  | `user_owner`         | `Pubkey`           |
| 13  | `market_event_queue` | `Pubkey`           |
| 14… | `remaining`          | `Vec<AccountMeta>` |

### `Initialize`

> _From the program's IDL:_
>
> Deprecated. Use `initialize2`.

Discriminator `00` · data `Initialize` · accounts `InitializeInstructionAccounts`.

| Field       | Type  |
| ----------- | ----- |
| `nonce`     | `u8`  |
| `open_time` | `u64` |

Required accounts: 16; extras go to `remaining`.

Fields of `InitializeInstructionAccounts`, in account order:

| #   | Field               | Type               |
| --- | ------------------- | ------------------ |
| 0   | `token_program`     | `Pubkey`           |
| 1   | `system_program`    | `Pubkey`           |
| 2   | `rent`              | `Pubkey`           |
| 3   | `amm`               | `Pubkey`           |
| 4   | `amm_authority`     | `Pubkey`           |
| 5   | `amm_open_orders`   | `Pubkey`           |
| 6   | `amm_lp_mint`       | `Pubkey`           |
| 7   | `amm_coin_mint`     | `Pubkey`           |
| 8   | `amm_pc_mint`       | `Pubkey`           |
| 9   | `amm_coin_vault`    | `Pubkey`           |
| 10  | `amm_pc_vault`      | `Pubkey`           |
| 11  | `amm_target_orders` | `Pubkey`           |
| 12  | `pool_temp_lp`      | `Pubkey`           |
| 13  | `market_program`    | `Pubkey`           |
| 14  | `market`            | `Pubkey`           |
| 15  | `user_wallet`       | `Pubkey`           |
| 16… | `remaining`         | `Vec<AccountMeta>` |

### `Initialize2`

> _From the program's IDL:_
>
> Initializes a new AMM pool.

Discriminator `01` · data `Initialize2` · accounts `Initialize2InstructionAccounts`.

| Field              | Type  |
| ------------------ | ----- |
| `nonce`            | `u8`  |
| `open_time`        | `u64` |
| `init_pc_amount`   | `u64` |
| `init_coin_amount` | `u64` |

Required accounts: 21; extras go to `remaining`.

Fields of `Initialize2InstructionAccounts`, in account order:

| #   | Field                      | Type               |
| --- | -------------------------- | ------------------ |
| 0   | `token_program`            | `Pubkey`           |
| 1   | `associated_token_program` | `Pubkey`           |
| 2   | `system_program`           | `Pubkey`           |
| 3   | `rent`                     | `Pubkey`           |
| 4   | `amm`                      | `Pubkey`           |
| 5   | `amm_authority`            | `Pubkey`           |
| 6   | `amm_open_orders`          | `Pubkey`           |
| 7   | `amm_lp_mint`              | `Pubkey`           |
| 8   | `amm_coin_mint`            | `Pubkey`           |
| 9   | `amm_pc_mint`              | `Pubkey`           |
| 10  | `amm_coin_vault`           | `Pubkey`           |
| 11  | `amm_pc_vault`             | `Pubkey`           |
| 12  | `amm_target_orders`        | `Pubkey`           |
| 13  | `amm_config`               | `Pubkey`           |
| 14  | `create_fee_destination`   | `Pubkey`           |
| 15  | `market_program`           | `Pubkey`           |
| 16  | `market`                   | `Pubkey`           |
| 17  | `user_wallet`              | `Pubkey`           |
| 18  | `user_token_coin`          | `Pubkey`           |
| 19  | `user_token_pc`            | `Pubkey`           |
| 20  | `user_token_lp`            | `Pubkey`           |
| 21… | `remaining`                | `Vec<AccountMeta>` |

### `MigrateToOpenBook`

> _From the program's IDL:_
>
> Migrate the associated market from Serum to OpenBook.

Discriminator `05` · data `MigrateToOpenBook` · accounts `MigrateToOpenBookInstructionAccounts`.

No data fields.

Required accounts: 21; extras go to `remaining`.

Fields of `MigrateToOpenBookInstructionAccounts`, in account order:

| #   | Field                 | Type               |
| --- | --------------------- | ------------------ |
| 0   | `token_program`       | `Pubkey`           |
| 1   | `system_program`      | `Pubkey`           |
| 2   | `rent`                | `Pubkey`           |
| 3   | `amm`                 | `Pubkey`           |
| 4   | `amm_authority`       | `Pubkey`           |
| 5   | `amm_open_orders`     | `Pubkey`           |
| 6   | `amm_coin_vault`      | `Pubkey`           |
| 7   | `amm_pc_vault`        | `Pubkey`           |
| 8   | `amm_target_orders`   | `Pubkey`           |
| 9   | `market_program`      | `Pubkey`           |
| 10  | `market`              | `Pubkey`           |
| 11  | `market_bids`         | `Pubkey`           |
| 12  | `market_asks`         | `Pubkey`           |
| 13  | `market_event_queue`  | `Pubkey`           |
| 14  | `market_coin_vault`   | `Pubkey`           |
| 15  | `market_pc_vault`     | `Pubkey`           |
| 16  | `market_vault_signer` | `Pubkey`           |
| 17  | `new_amm_open_orders` | `Pubkey`           |
| 18  | `new_market_program`  | `Pubkey`           |
| 19  | `new_market`          | `Pubkey`           |
| 20  | `admin`               | `Pubkey`           |
| 21… | `remaining`           | `Vec<AccountMeta>` |

### `MonitorStep`

> _From the program's IDL:_
>
> Step the AMM order-book state machine.

Discriminator `02` · data `MonitorStep` · accounts `MonitorStepInstructionAccounts`.

| Field                | Type  |
| -------------------- | ----- |
| `plan_order_limit`   | `u16` |
| `place_order_limit`  | `u16` |
| `cancel_order_limit` | `u16` |

Required accounts: 18 (20 listed); extras go to `remaining`.

Fields of `MonitorStepInstructionAccounts`, in account order:

| #   | Field                  | Type               |
| --- | ---------------------- | ------------------ |
| 0   | `token_program`        | `Pubkey`           |
| 1   | `rent`                 | `Pubkey`           |
| 2   | `clock`                | `Pubkey`           |
| 3   | `amm`                  | `Pubkey`           |
| 4   | `amm_authority`        | `Pubkey`           |
| 5   | `amm_open_orders`      | `Pubkey`           |
| 6   | `amm_target_orders`    | `Pubkey`           |
| 7   | `amm_coin_vault`       | `Pubkey`           |
| 8   | `amm_pc_vault`         | `Pubkey`           |
| 9   | `market_program`       | `Pubkey`           |
| 10  | `market`               | `Pubkey`           |
| 11  | `market_coin_vault`    | `Pubkey`           |
| 12  | `market_pc_vault`      | `Pubkey`           |
| 13  | `market_vault_signer`  | `Pubkey`           |
| 14  | `market_request_queue` | `Pubkey`           |
| 15  | `market_event_queue`   | `Pubkey`           |
| 16  | `market_bids`          | `Pubkey`           |
| 17  | `market_asks`          | `Pubkey`           |
| 18  | `srm_account`          | `Option<Pubkey>`   |
| 19  | `referrer_pc_account`  | `Option<Pubkey>`   |
| 20… | `remaining`            | `Vec<AccountMeta>` |

### `PreInitialize`

> _From the program's IDL:_
>
> Deprecated. Use `initialize2`.

Discriminator `0a` · data `PreInitialize` · accounts `PreInitializeInstructionAccounts`.

| Field   | Type |
| ------- | ---- |
| `nonce` | `u8` |

Required accounts: 14; extras go to `remaining`.

Fields of `PreInitializeInstructionAccounts`, in account order:

| #   | Field                        | Type               |
| --- | ---------------------------- | ------------------ |
| 0   | `token_program`              | `Pubkey`           |
| 1   | `system_program`             | `Pubkey`           |
| 2   | `rent`                       | `Pubkey`           |
| 3   | `amm_target_orders`          | `Pubkey`           |
| 4   | `pool_withdraw_queue`        | `Pubkey`           |
| 5   | `amm_authority`              | `Pubkey`           |
| 6   | `lp_mint_address`            | `Pubkey`           |
| 7   | `coin_mint_address`          | `Pubkey`           |
| 8   | `pc_mint_address`            | `Pubkey`           |
| 9   | `pool_coin_token_account`    | `Pubkey`           |
| 10  | `pool_pc_token_account`      | `Pubkey`           |
| 11  | `pool_temp_lp_token_account` | `Pubkey`           |
| 12  | `serum_market`               | `Pubkey`           |
| 13  | `user_wallet`                | `Pubkey`           |
| 14… | `remaining`                  | `Vec<AccountMeta>` |

### `SetParams`

> _From the program's IDL:_
>
> Admin: set an AMM parameter. The payload after `param` depends on the variant:
>
> - ammOwner: 32-byte Pubkey
> - fees: 64-byte Fees struct
> - lastOrderDistance: numerator(u64) + denominator(u64)
> - all other variants: value(u64)
>
> Only `value`, `newPubkey`, `fees`, or `lastOrderDistance` is populated based on `param`.

Discriminator `06` · data `SetParams` · accounts `SetParamsInstructionAccounts`.

| Field                 | Type                                              |
| --------------------- | ------------------------------------------------- |
| `param`               | [`AmmParams`](#ammparams)                         |
| `value`               | `Option<u64>`                                     |
| `new_pubkey`          | `Option<Pubkey>`                                  |
| `fees`                | [`Option<Fees>`](#fees)                           |
| `last_order_distance` | [`Option<LastOrderDistance>`](#lastorderdistance) |

Required accounts: 16 (17 listed); extras go to `remaining`.

Fields of `SetParamsInstructionAccounts`, in account order:

| #   | Field                 | Type               |
| --- | --------------------- | ------------------ |
| 0   | `token_program`       | `Pubkey`           |
| 1   | `amm`                 | `Pubkey`           |
| 2   | `amm_authority`       | `Pubkey`           |
| 3   | `amm_open_orders`     | `Pubkey`           |
| 4   | `amm_target_orders`   | `Pubkey`           |
| 5   | `amm_coin_vault`      | `Pubkey`           |
| 6   | `amm_pc_vault`        | `Pubkey`           |
| 7   | `market_program`      | `Pubkey`           |
| 8   | `market`              | `Pubkey`           |
| 9   | `market_coin_vault`   | `Pubkey`           |
| 10  | `market_pc_vault`     | `Pubkey`           |
| 11  | `market_vault_signer` | `Pubkey`           |
| 12  | `market_event_queue`  | `Pubkey`           |
| 13  | `market_bids`         | `Pubkey`           |
| 14  | `market_asks`         | `Pubkey`           |
| 15  | `admin`               | `Pubkey`           |
| 16  | `new_amm_open_orders` | `Option<Pubkey>`   |
| 17… | `remaining`           | `Vec<AccountMeta>` |

### `SimulateInfo`

> _From the program's IDL:_
>
> Simulation-only instruction (used via simulateTransaction).\
> Payload after `param` depends on the variant:
>
> - poolInfo / runCrankInfo: no extra payload
> - swapBaseInInfo: amountIn(u64) + minimumAmountOut(u64)
> - swapBaseOutInfo: maxAmountIn(u64) + amountOut(u64)

Discriminator `0c` · data `SimulateInfo` · accounts `SimulateInfoInstructionAccounts`.

| Field                 | Type                                                        |
| --------------------- | ----------------------------------------------------------- |
| `param`               | [`SimulateParams`](#simulateparams)                         |
| `swap_base_in_value`  | [`Option<SwapInstructionBaseIn>`](#swapinstructionbasein)   |
| `swap_base_out_value` | [`Option<SwapInstructionBaseOut>`](#swapinstructionbaseout) |

Required accounts: 8; extras go to `remaining`.

Fields of `SimulateInfoInstructionAccounts`, in account order:

| #  | Field                | Type               |
| -- | -------------------- | ------------------ |
| 0  | `amm`                | `Pubkey`           |
| 1  | `amm_authority`      | `Pubkey`           |
| 2  | `amm_open_orders`    | `Pubkey`           |
| 3  | `amm_coin_vault`     | `Pubkey`           |
| 4  | `amm_pc_vault`       | `Pubkey`           |
| 5  | `amm_lp_mint`        | `Pubkey`           |
| 6  | `market`             | `Pubkey`           |
| 7  | `market_event_queue` | `Pubkey`           |
| 8… | `remaining`          | `Vec<AccountMeta>` |

### `SwapBaseIn`

> _From the program's IDL:_
>
> Swap a fixed input amount with a slippage-bounded output.

Discriminator `09` · data `SwapBaseIn` · accounts `SwapBaseInInstructionAccounts`.

| Field                | Type  |
| -------------------- | ----- |
| `amount_in`          | `u64` |
| `minimum_amount_out` | `u64` |

Required accounts: 18; extras go to `remaining`.

Fields of `SwapBaseInInstructionAccounts`, in account order:

| #   | Field                            | Type               |
| --- | -------------------------------- | ------------------ |
| 0   | `token_program`                  | `Pubkey`           |
| 1   | `amm`                            | `Pubkey`           |
| 2   | `amm_authority`                  | `Pubkey`           |
| 3   | `amm_open_orders`                | `Pubkey`           |
| 4   | `amm_target_orders`              | `Option<Pubkey>`   |
| 5   | `amm_coin_vault`                 | `Pubkey`           |
| 6   | `amm_pc_vault`                   | `Pubkey`           |
| 7   | `market_program`                 | `Pubkey`           |
| 8   | `market`                         | `Pubkey`           |
| 9   | `market_bids`                    | `Pubkey`           |
| 10  | `market_asks`                    | `Pubkey`           |
| 11  | `market_event_queue`             | `Pubkey`           |
| 12  | `market_coin_vault`              | `Pubkey`           |
| 13  | `market_pc_vault`                | `Pubkey`           |
| 14  | `market_vault_signer`            | `Pubkey`           |
| 15  | `user_source_token_account`      | `Pubkey`           |
| 16  | `user_destination_token_account` | `Pubkey`           |
| 17  | `user_source_owner`              | `Pubkey`           |
| 18… | `remaining`                      | `Vec<AccountMeta>` |

### `SwapBaseInV2`

> _From the program's IDL:_
>
> Order-book-disabled swap with fixed input amount.

Discriminator `10` · data `SwapBaseInV2` · accounts `SwapBaseInV2InstructionAccounts`.

| Field                | Type  |
| -------------------- | ----- |
| `amount_in`          | `u64` |
| `minimum_amount_out` | `u64` |

Required accounts: 8; extras go to `remaining`.

Fields of `SwapBaseInV2InstructionAccounts`, in account order:

| #  | Field                            | Type               |
| -- | -------------------------------- | ------------------ |
| 0  | `token_program`                  | `Pubkey`           |
| 1  | `amm`                            | `Pubkey`           |
| 2  | `amm_authority`                  | `Pubkey`           |
| 3  | `amm_coin_vault`                 | `Pubkey`           |
| 4  | `amm_pc_vault`                   | `Pubkey`           |
| 5  | `user_source_token_account`      | `Pubkey`           |
| 6  | `user_destination_token_account` | `Pubkey`           |
| 7  | `user_source_owner`              | `Pubkey`           |
| 8… | `remaining`                      | `Vec<AccountMeta>` |

### `SwapBaseOut`

> _From the program's IDL:_
>
> Swap to receive a fixed output amount with a slippage-bounded input.

Discriminator `0b` · data `SwapBaseOut` · accounts `SwapBaseOutInstructionAccounts`.

| Field           | Type  |
| --------------- | ----- |
| `max_amount_in` | `u64` |
| `amount_out`    | `u64` |

Required accounts: 18; extras go to `remaining`.

Fields of `SwapBaseOutInstructionAccounts`, in account order:

| #   | Field                            | Type               |
| --- | -------------------------------- | ------------------ |
| 0   | `token_program`                  | `Pubkey`           |
| 1   | `amm`                            | `Pubkey`           |
| 2   | `amm_authority`                  | `Pubkey`           |
| 3   | `amm_open_orders`                | `Pubkey`           |
| 4   | `amm_target_orders`              | `Option<Pubkey>`   |
| 5   | `amm_coin_vault`                 | `Pubkey`           |
| 6   | `amm_pc_vault`                   | `Pubkey`           |
| 7   | `market_program`                 | `Pubkey`           |
| 8   | `market`                         | `Pubkey`           |
| 9   | `market_bids`                    | `Pubkey`           |
| 10  | `market_asks`                    | `Pubkey`           |
| 11  | `market_event_queue`             | `Pubkey`           |
| 12  | `market_coin_vault`              | `Pubkey`           |
| 13  | `market_pc_vault`                | `Pubkey`           |
| 14  | `market_vault_signer`            | `Pubkey`           |
| 15  | `user_source_token_account`      | `Pubkey`           |
| 16  | `user_destination_token_account` | `Pubkey`           |
| 17  | `user_source_owner`              | `Pubkey`           |
| 18… | `remaining`                      | `Vec<AccountMeta>` |

### `SwapBaseOutV2`

> _From the program's IDL:_
>
> Order-book-disabled swap with fixed output amount.

Discriminator `11` · data `SwapBaseOutV2` · accounts `SwapBaseOutV2InstructionAccounts`.

| Field           | Type  |
| --------------- | ----- |
| `max_amount_in` | `u64` |
| `amount_out`    | `u64` |

Required accounts: 8; extras go to `remaining`.

Fields of `SwapBaseOutV2InstructionAccounts`, in account order:

| #  | Field                            | Type               |
| -- | -------------------------------- | ------------------ |
| 0  | `token_program`                  | `Pubkey`           |
| 1  | `amm`                            | `Pubkey`           |
| 2  | `amm_authority`                  | `Pubkey`           |
| 3  | `amm_coin_vault`                 | `Pubkey`           |
| 4  | `amm_pc_vault`                   | `Pubkey`           |
| 5  | `user_source_token_account`      | `Pubkey`           |
| 6  | `user_destination_token_account` | `Pubkey`           |
| 7  | `user_source_owner`              | `Pubkey`           |
| 8… | `remaining`                      | `Vec<AccountMeta>` |

### `UpdateConfigAccount`

> _From the program's IDL:_
>
> Admin: update the AmmConfig. Payload after `param` depends on the variant:
>
> - pnlOwner / cancelOrderOwner: 32-byte Pubkey
> - createPoolFee: u64

Discriminator `0f` · data `UpdateConfigAccount` · accounts `UpdateConfigAccountInstructionAccounts`.

| Field             | Type                          |
| ----------------- | ----------------------------- |
| `param`           | [`ConfigParam`](#configparam) |
| `owner`           | `Option<Pubkey>`              |
| `create_pool_fee` | `Option<u64>`                 |

Required accounts: 2; extras go to `remaining`.

Fields of `UpdateConfigAccountInstructionAccounts`, in account order:

| #  | Field        | Type               |
| -- | ------------ | ------------------ |
| 0  | `admin`      | `Pubkey`           |
| 1  | `amm_config` | `Pubkey`           |
| 2… | `remaining`  | `Vec<AccountMeta>` |

### `Withdraw`

> _From the program's IDL:_
>
> Burn LP tokens and withdraw the underlying assets.

Discriminator `04` · data `Withdraw` · accounts `WithdrawInstructionAccounts`.

| Field             | Type          | Description                                                                   |
| ----------------- | ------------- | ----------------------------------------------------------------------------- |
| `amount`          | `u64`         |                                                                               |
| `min_coin_amount` | `Option<u64>` | Optional trailing slippage bound; both min fields are present or both absent. |
| `min_pc_amount`   | `Option<u64>` |                                                                               |

Required accounts: 20; extras go to `remaining`.

Fields of `WithdrawInstructionAccounts`, in account order:

| #   | Field                 | Type               |
| --- | --------------------- | ------------------ |
| 0   | `token_program`       | `Pubkey`           |
| 1   | `amm`                 | `Pubkey`           |
| 2   | `amm_authority`       | `Pubkey`           |
| 3   | `amm_open_orders`     | `Pubkey`           |
| 4   | `amm_target_orders`   | `Pubkey`           |
| 5   | `amm_lp_mint`         | `Pubkey`           |
| 6   | `amm_coin_vault`      | `Pubkey`           |
| 7   | `amm_pc_vault`        | `Pubkey`           |
| 8   | `market_program`      | `Pubkey`           |
| 9   | `market`              | `Pubkey`           |
| 10  | `market_coin_vault`   | `Pubkey`           |
| 11  | `market_pc_vault`     | `Pubkey`           |
| 12  | `market_vault_signer` | `Pubkey`           |
| 13  | `user_token_lp`       | `Pubkey`           |
| 14  | `user_token_coin`     | `Pubkey`           |
| 15  | `user_token_pc`       | `Pubkey`           |
| 16  | `user_owner`          | `Pubkey`           |
| 17  | `market_event_queue`  | `Pubkey`           |
| 18  | `market_bids`         | `Pubkey`           |
| 19  | `market_asks`         | `Pubkey`           |
| 20… | `remaining`           | `Vec<AccountMeta>` |

### `WithdrawPnl`

> _From the program's IDL:_
>
> Protocol: withdraw accrued PnL from the pool.

Discriminator `07` · data `WithdrawPnl` · accounts `WithdrawPnlInstructionAccounts`.

No data fields.

Required accounts: 17 (18 listed); extras go to `remaining`.

Fields of `WithdrawPnlInstructionAccounts`, in account order:

| #   | Field                     | Type               |
| --- | ------------------------- | ------------------ |
| 0   | `token_program`           | `Pubkey`           |
| 1   | `amm`                     | `Pubkey`           |
| 2   | `amm_config`              | `Pubkey`           |
| 3   | `amm_authority`           | `Pubkey`           |
| 4   | `amm_open_orders`         | `Pubkey`           |
| 5   | `amm_coin_vault`          | `Pubkey`           |
| 6   | `amm_pc_vault`            | `Pubkey`           |
| 7   | `user_coin_token_account` | `Pubkey`           |
| 8   | `user_pc_token_account`   | `Pubkey`           |
| 9   | `user_owner`              | `Pubkey`           |
| 10  | `amm_target_orders`       | `Pubkey`           |
| 11  | `market_program`          | `Pubkey`           |
| 12  | `market`                  | `Pubkey`           |
| 13  | `market_event_queue`      | `Pubkey`           |
| 14  | `market_coin_vault`       | `Pubkey`           |
| 15  | `market_pc_vault`         | `Pubkey`           |
| 16  | `market_vault_signer`     | `Pubkey`           |
| 17  | `referrer_pc_account`     | `Option<Pubkey>`   |
| 18… | `remaining`               | `Vec<AccountMeta>` |

### `WithdrawSrm`

> _From the program's IDL:_
>
> Admin: withdraw (M)SRM held for fee discounts.

Discriminator `08` · data `WithdrawSrm` · accounts `WithdrawSrmInstructionAccounts`.

| Field    | Type  |
| -------- | ----- |
| `amount` | `u64` |

Required accounts: 6; extras go to `remaining`.

Fields of `WithdrawSrmInstructionAccounts`, in account order:

| #  | Field           | Type               |
| -- | --------------- | ------------------ |
| 0  | `token_program` | `Pubkey`           |
| 1  | `amm`           | `Pubkey`           |
| 2  | `admin`         | `Pubkey`           |
| 3  | `amm_authority` | `Pubkey`           |
| 4  | `srm_from`      | `Pubkey`           |
| 5  | `srm_to`        | `Pubkey`           |
| 6… | `remaining`     | `Vec<AccountMeta>` |

## Accounts

Variants of `RaydiumAmmV4Account`. Each wraps a boxed struct from `carbon_raydium_amm_v4_decoder::accounts::<module>`. `decode_account` only considers accounts owned by `PROGRAM_ID` and returns the first variant that matches.

### `AmmConfig`

> _From the program's IDL:_
>
> Global program config account.

Matched by data length (544 bytes) · `accounts::amm_config::AmmConfig`.

| Field             | Type        |
| ----------------- | ----------- |
| `pnl_owner`       | `Pubkey`    |
| `cancel_owner`    | `Pubkey`    |
| `pending1`        | `[u64; 28]` |
| `pending2`        | `[u64; 31]` |
| `create_pool_fee` | `u64`       |

### `AmmInfo`

> _From the program's IDL:_
>
> On-chain state of a single AMM pool.

Matched by data length (752 bytes) · `accounts::amm_info::AmmInfo`.

| Field                  | Type                      |
| ---------------------- | ------------------------- |
| `status`               | `u64`                     |
| `nonce`                | `u64`                     |
| `order_num`            | `u64`                     |
| `depth`                | `u64`                     |
| `coin_decimals`        | `u64`                     |
| `pc_decimals`          | `u64`                     |
| `state`                | `u64`                     |
| `reset_flag`           | `u64`                     |
| `min_size`             | `u64`                     |
| `vol_max_cut_ratio`    | `u64`                     |
| `amount_wave`          | `u64`                     |
| `coin_lot_size`        | `u64`                     |
| `pc_lot_size`          | `u64`                     |
| `min_price_multiplier` | `u64`                     |
| `max_price_multiplier` | `u64`                     |
| `sys_decimal_value`    | `u64`                     |
| `fees`                 | [`Fees`](#fees)           |
| `state_data`           | [`StateData`](#statedata) |
| `coin_vault`           | `Pubkey`                  |
| `pc_vault`             | `Pubkey`                  |
| `coin_vault_mint`      | `Pubkey`                  |
| `pc_vault_mint`        | `Pubkey`                  |
| `lp_mint`              | `Pubkey`                  |
| `open_orders`          | `Pubkey`                  |
| `market`               | `Pubkey`                  |
| `market_program`       | `Pubkey`                  |
| `target_orders`        | `Pubkey`                  |
| `padding1`             | `[u64; 8]`                |
| `amm_owner`            | `Pubkey`                  |
| `lp_amount`            | `u64`                     |
| `client_order_id`      | `u64`                     |
| `recent_epoch`         | `u64`                     |
| `padding2`             | `u64`                     |

### `TargetOrders`

> _From the program's IDL:_
>
> Per-pool target orders book.

No discriminator (matched by Borsh layout) · `accounts::target_orders::TargetOrders`.

| Field                    | Type                                |
| ------------------------ | ----------------------------------- |
| `owner`                  | `[u64; 4]`                          |
| `buy_orders`             | [`[TargetOrder; 50]`](#targetorder) |
| `padding1`               | `[u64; 8]`                          |
| `target_x`               | `u128`                              |
| `target_y`               | `u128`                              |
| `plan_x_buy`             | `u128`                              |
| `plan_y_buy`             | `u128`                              |
| `plan_x_sell`            | `u128`                              |
| `plan_y_sell`            | `u128`                              |
| `placed_x`               | `u128`                              |
| `placed_y`               | `u128`                              |
| `calc_pnl_x`             | `u128`                              |
| `calc_pnl_y`             | `u128`                              |
| `sell_orders`            | [`[TargetOrder; 50]`](#targetorder) |
| `padding2`               | `[u64; 6]`                          |
| `replace_buy_client_id`  | `[u64; 10]`                         |
| `replace_sell_client_id` | `[u64; 10]`                         |
| `last_order_numerator`   | `u64`                               |
| `last_order_denominator` | `u64`                               |
| `plan_orders_cur`        | `u64`                               |
| `place_orders_cur`       | `u64`                               |
| `valid_buy_order_num`    | `u64`                               |
| `valid_sell_order_num`   | `u64`                               |
| `padding3`               | `[u64; 10]`                         |
| `free_slot_bits`         | `u128`                              |

## Types

Types used by the instructions, accounts and events above, exported from `carbon_raydium_amm_v4_decoder::types`.

### `AmmParams`

> _From the program's IDL:_
>
> SetParams.param values.

Enum `types::AmmParams`.

| Variant              |
| -------------------- |
| `Status`             |
| `State`              |
| `OrderNum`           |
| `Depth`              |
| `AmountWave`         |
| `MinPriceMultiplier` |
| `MaxPriceMultiplier` |
| `MinSize`            |
| `VolMaxCutRatio`     |
| `Fees`               |
| `AmmOwner`           |
| `SetOpenTime`        |
| `LastOrderDistance`  |
| `InitOrderDepth`     |
| `SetSwitchTime`      |
| `ClearOpenTime`      |
| `Seek`               |
| `EnableOrderBook`    |
| `DisableOrderBook`   |

### `AmmResetFlag`

Enum `types::AmmResetFlag`.

| Variant    |
| ---------- |
| `ResetYes` |
| `ResetNo`  |

### `AmmState`

> _From the program's IDL:_
>
> AmmInfo.state machine (stored as u64).

Enum `types::AmmState`.

| Variant                |
| ---------------------- |
| `IdleState`            |
| `CancelAllOrdersState` |
| `PlanOrdersState`      |
| `CancelOrderState`     |
| `PlaceOrdersState`     |
| `PurgeOrderState`      |

### `AmmStatus`

> _From the program's IDL:_
>
> AmmInfo.status discriminator (stored as u64).

Enum `types::AmmStatus`.

| Variant         |
| --------------- |
| `Uninitialized` |
| `Initialized`   |
| `Disabled`      |
| `WithdrawOnly`  |
| `LiquidityOnly` |
| `OrderBookOnly` |
| `SwapOnly`      |
| `WaitingTrade`  |

### `ConfigParam`

> _From the program's IDL:_
>
> UpdateConfigAccount.param values.

Enum `types::ConfigParam`.

| Variant            |
| ------------------ |
| `PnlOwner`         |
| `CancelOrderOwner` |
| `CreatePoolFee`    |

### `Fees`

> _From the program's IDL:_
>
> Fee parameters for a pool.

Struct `types::Fees`.

| Field                      | Type  |
| -------------------------- | ----- |
| `min_separate_numerator`   | `u64` |
| `min_separate_denominator` | `u64` |
| `trade_fee_numerator`      | `u64` |
| `trade_fee_denominator`    | `u64` |
| `pnl_numerator`            | `u64` |
| `pnl_denominator`          | `u64` |
| `swap_fee_numerator`       | `u64` |
| `swap_fee_denominator`     | `u64` |

### `LastOrderDistance`

Struct `types::LastOrderDistance`.

| Field                    | Type  |
| ------------------------ | ----- |
| `last_order_numerator`   | `u64` |
| `last_order_denominator` | `u64` |

### `SimulateParams`

> _From the program's IDL:_
>
> SimulateInfo.param values.

Enum `types::SimulateParams`.

| Variant           |
| ----------------- |
| `PoolInfo`        |
| `SwapBaseInInfo`  |
| `SwapBaseOutInfo` |
| `RunCrankInfo`    |

### `StateData`

> _From the program's IDL:_
>
> Mutable pool statistics embedded in AmmInfo.

Struct `types::StateData`.

| Field                    | Type       |
| ------------------------ | ---------- |
| `need_take_pnl_coin`     | `u64`      |
| `need_take_pnl_pc`       | `u64`      |
| `total_pnl_pc`           | `u64`      |
| `total_pnl_coin`         | `u64`      |
| `pool_open_time`         | `u64`      |
| `padding`                | `[u64; 2]` |
| `orderbook_to_init_time` | `u64`      |
| `swap_coin_in_amount`    | `u128`     |
| `swap_pc_out_amount`     | `u128`     |
| `swap_acc_pc_fee`        | `u64`      |
| `swap_pc_in_amount`      | `u128`     |
| `swap_coin_out_amount`   | `u128`     |
| `swap_acc_coin_fee`      | `u64`      |

### `SwapInstructionBaseIn`

Struct `types::SwapInstructionBaseIn`.

| Field                | Type  |
| -------------------- | ----- |
| `amount_in`          | `u64` |
| `minimum_amount_out` | `u64` |

### `SwapInstructionBaseOut`

Struct `types::SwapInstructionBaseOut`.

| Field           | Type  |
| --------------- | ----- |
| `max_amount_in` | `u64` |
| `amount_out`    | `u64` |

### `TargetOrder`

Struct `types::TargetOrder`.

| Field   | Type  |
| ------- | ----- |
| `price` | `u64` |
| `vol`   | `u64` |

---

Carbon 2.0.0 documentation · Web: https://carbon.sevenlabs.org/decoders/raydium-amm-v4 · Last updated 2026-11-09 · Index of all pages: https://carbon.sevenlabs.org/llms.txt
