# Lifinity AMM v2 decoder (Carbon 2.0)

> Decode Lifinity AMM v2 instructions and accounts in Rust with carbon-lifinity-amm-v2-decoder 2.0.0. Program ID 2wT8Yq49kHgDzXuPxZSaeLaH1qbmGXtEyPy64bL7aD3c.

`carbon-lifinity-amm-v2-decoder` decodes the instructions and accounts of the Lifinity AMM v2 program (`2wT8Yq49kHgDzXuPxZSaeLaH1qbmGXtEyPy64bL7aD3c`): 3 instructions, 1 account type. 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-lifinity-amm-v2-decoder`](https://crates.io/crates/carbon-lifinity-amm-v2-decoder/2.0.0) 2.0.0                 |
| Install          | `cargo add carbon-lifinity-amm-v2-decoder@2.0.0`                                                                        |
| Program ID       | `2wT8Yq49kHgDzXuPxZSaeLaH1qbmGXtEyPy64bL7aD3c` (`carbon_lifinity_amm_v2_decoder::PROGRAM_ID`)                           |
| Category         | [DEX & AMM](https://carbon.sevenlabs.org/decoders/dex-and-amm.md)                                                       |
| Decoder          | `carbon_lifinity_amm_v2_decoder::LifinityAmmV2Decoder`                                                                  |
| Instruction enum | `instructions::LifinityAmmV2Instruction`                                                                                |
| Account enum     | `accounts::LifinityAmmV2Account`                                                                                        |
| Features         | `serde`, `postgres`, `graphql` (none enabled by default)                                                                |
| API docs         | [docs.rs/carbon-lifinity-amm-v2-decoder/2.0.0](https://docs.rs/carbon-lifinity-amm-v2-decoder/2.0.0)                    |
| Source           | [decoders/lifinity-amm-v2-decoder](https://github.com/sevenlabs-hq/carbon/tree/v2.0.0/decoders/lifinity-amm-v2-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), [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 AMM v4](https://carbon.sevenlabs.org/decoders/raydium-amm-v4.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 Lifinity AMM v2 transactions from the public RPC and prints every decoded `Swap` instruction. Add the dependencies:

```sh
cargo add carbon-core@2.0.0 carbon-lifinity-amm-v2-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_lifinity_amm_v2_decoder::{
        instructions::LifinityAmmV2Instruction, LifinityAmmV2Decoder, 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(LifinityAmmV2Decoder, InstructionLogger)
        .build()?
        .run()
        .await
}

struct InstructionLogger;

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

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

Each `Swap` instruction prints one line with the transaction signature and the decoded `Swap` 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 `Amm` 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_lifinity_amm_v2_decoder::{
        accounts::LifinityAmmV2Account, LifinityAmmV2Decoder, 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(LifinityAmmV2Decoder, AccountLogger)
        .build()?
        .run()
        .await
}

struct AccountLogger;

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

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

Each update to a `Amm` 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 `LifinityAmmV2Instruction`. Each is `LifinityAmmV2Instruction::<Variant> { program_id, data, accounts }`, where `data` and `accounts` are the types listed under the variant, exported from `carbon_lifinity_amm_v2_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 `2wT8Yq49kHgDzXuPxZSaeLaH1qbmGXtEyPy64bL7aD3c`.
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.

### `DepositAllTokenTypes`

Discriminator `205f453c4b4fcdee` · data `DepositAllTokenTypes` · accounts `DepositAllTokenTypesInstructionAccounts`.

| Field                    | Type  |
| ------------------------ | ----- |
| `pool_token_amount`      | `u64` |
| `maximum_token_a_amount` | `u64` |
| `maximum_token_b_amount` | `u64` |

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

Fields of `DepositAllTokenTypesInstructionAccounts`, in account order:

| #   | Field                          | Type               |
| --- | ------------------------------ | ------------------ |
| 0   | `amm`                          | `Pubkey`           |
| 1   | `authority`                    | `Pubkey`           |
| 2   | `user_transfer_authority_info` | `Pubkey`           |
| 3   | `source_a_info`                | `Pubkey`           |
| 4   | `source_b_info`                | `Pubkey`           |
| 5   | `token_a`                      | `Pubkey`           |
| 6   | `token_b`                      | `Pubkey`           |
| 7   | `pool_mint`                    | `Pubkey`           |
| 8   | `destination`                  | `Pubkey`           |
| 9   | `token_program`                | `Pubkey`           |
| 10… | `remaining`                    | `Vec<AccountMeta>` |

### `Swap`

Discriminator `f8c69e91e17587c8` · data `Swap` · accounts `SwapInstructionAccounts`.

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

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

Fields of `SwapInstructionAccounts`, in account order:

| #   | Field                     | Type               |
| --- | ------------------------- | ------------------ |
| 0   | `authority`               | `Pubkey`           |
| 1   | `amm`                     | `Pubkey`           |
| 2   | `user_transfer_authority` | `Pubkey`           |
| 3   | `source_info`             | `Pubkey`           |
| 4   | `destination_info`        | `Pubkey`           |
| 5   | `swap_source`             | `Pubkey`           |
| 6   | `swap_destination`        | `Pubkey`           |
| 7   | `pool_mint`               | `Pubkey`           |
| 8   | `fee_account`             | `Pubkey`           |
| 9   | `token_program`           | `Pubkey`           |
| 10  | `oracle_main_account`     | `Pubkey`           |
| 11  | `oracle_sub_account`      | `Pubkey`           |
| 12  | `oracle_pc_account`       | `Pubkey`           |
| 13… | `remaining`               | `Vec<AccountMeta>` |

### `WithdrawAllTokenTypes`

Discriminator `bdfe9caed209a4d8` · data `WithdrawAllTokenTypes` · accounts `WithdrawAllTokenTypesInstructionAccounts`.

| Field                    | Type  |
| ------------------------ | ----- |
| `pool_token_amount`      | `u64` |
| `minimum_token_a_amount` | `u64` |
| `minimum_token_b_amount` | `u64` |

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

Fields of `WithdrawAllTokenTypesInstructionAccounts`, in account order:

| #   | Field                          | Type               |
| --- | ------------------------------ | ------------------ |
| 0   | `amm`                          | `Pubkey`           |
| 1   | `authority`                    | `Pubkey`           |
| 2   | `user_transfer_authority_info` | `Pubkey`           |
| 3   | `source_info`                  | `Pubkey`           |
| 4   | `token_a`                      | `Pubkey`           |
| 5   | `token_b`                      | `Pubkey`           |
| 6   | `pool_mint`                    | `Pubkey`           |
| 7   | `dest_token_a_info`            | `Pubkey`           |
| 8   | `dest_token_b_info`            | `Pubkey`           |
| 9   | `token_program`                | `Pubkey`           |
| 10… | `remaining`                    | `Vec<AccountMeta>` |

## Accounts

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

### `Amm`

Discriminator `8ff5c8114ad6c487` · `accounts::amm::Amm`.

| Field                               | Type                      | Description                                                |
| ----------------------------------- | ------------------------- | ---------------------------------------------------------- |
| `initializer_key`                   | `Pubkey`                  |                                                            |
| `initializer_deposit_token_account` | `Pubkey`                  |                                                            |
| `initializer_receive_token_account` | `Pubkey`                  |                                                            |
| `initializer_amount`                | `u64`                     |                                                            |
| `taker_amount`                      | `u64`                     |                                                            |
| `is_initialized`                    | `bool`                    | Is the swap initialized, with data written to it           |
| `bump_seed`                         | `u8`                      | Bump seed used to generate the program address / authority |
| `freeze_trade`                      | `u8`                      | Freeze swap                                                |
| `freeze_deposit`                    | `u8`                      | Freeze deposit                                             |
| `freeze_withdraw`                   | `u8`                      | Freeze withdraw                                            |
| `base_decimals`                     | `u8`                      | Base decimals account                                      |
| `token_program_id`                  | `Pubkey`                  | Token program ID associated with the swap                  |
| `token_a_account`                   | `Pubkey`                  | Address of token A liquidity account                       |
| `token_b_account`                   | `Pubkey`                  | Address of token B liquidity account                       |
| `pool_mint`                         | `Pubkey`                  | Address of pool token mint                                 |
| `token_a_mint`                      | `Pubkey`                  | Address of token A mint                                    |
| `token_b_mint`                      | `Pubkey`                  | Address of token B mint                                    |
| `fee_account`                       | `Pubkey`                  | Address of pool fee account                                |
| `oracle_main_account`               | `Pubkey`                  | Oracle Main account                                        |
| `oracle_sub_account`                | `Pubkey`                  | Oracle Sub account                                         |
| `oracle_pc_account`                 | `Pubkey`                  | oracle Pc account                                          |
| `fees`                              | [`AmmFees`](#ammfees)     | Fees associated with swap                                  |
| `curve`                             | [`AmmCurve`](#ammcurve)   | Curve associated with swap                                 |
| `config`                            | [`AmmConfig`](#ammconfig) | Curve associated with swap                                 |
| `amm_p_temp1`                       | `Pubkey`                  | Temp area                                                  |
| `amm_p_temp2`                       | `Pubkey`                  | Temp area                                                  |
| `amm_p_temp3`                       | `Pubkey`                  | Temp area                                                  |
| `amm_p_temp4`                       | `Pubkey`                  | Temp area                                                  |
| `amm_p_temp5`                       | `Pubkey`                  | Temp area                                                  |

## Types

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

### `AmmConfig`

Struct `types::AmmConfig`.

| Field                         | Type  |
| ----------------------------- | ----- |
| `last_price`                  | `u64` |
| `last_balanced_price`         | `u64` |
| `config_denominator`          | `u64` |
| `volume_x`                    | `u64` |
| `volume_y`                    | `u64` |
| `volume_x_in_y`               | `u64` |
| `deposit_cap`                 | `u64` |
| `regression_target`           | `u64` |
| `oracle_type`                 | `u64` |
| `oracle_status`               | `u64` |
| `oracle_main_slot_limit`      | `u64` |
| `oracle_sub_confidence_limit` | `u64` |
| `oracle_sub_slot_limit`       | `u64` |
| `oracle_pc_confidence_limit`  | `u64` |
| `oracle_pc_slot_limit`        | `u64` |
| `std_spread`                  | `u64` |
| `std_spread_buffer`           | `u64` |
| `spread_coefficient`          | `u64` |
| `price_buffer_coin`           | `i64` |
| `price_buffer_pc`             | `i64` |
| `rebalance_ratio`             | `u64` |
| `fee_trade`                   | `u64` |
| `fee_platform`                | `u64` |
| `oracle_main_slot_buffer`     | `u64` |
| `config_temp4`                | `u64` |
| `config_temp5`                | `u64` |
| `config_temp6`                | `u64` |
| `config_temp7`                | `u64` |
| `config_temp8`                | `u64` |

### `AmmCurve`

Struct `types::AmmCurve`.

| Field              | Type  |
| ------------------ | ----- |
| `curve_type`       | `u8`  |
| `curve_parameters` | `u64` |

### `AmmFees`

> _From the program's IDL:_
>
> Encapsulates all fee information and calculations for swap operations

Struct `types::AmmFees`.

| Field                            | Type  |
| -------------------------------- | ----- |
| `trade_fee_numerator`            | `u64` |
| `trade_fee_denominator`          | `u64` |
| `owner_trade_fee_numerator`      | `u64` |
| `owner_trade_fee_denominator`    | `u64` |
| `owner_withdraw_fee_numerator`   | `u64` |
| `owner_withdraw_fee_denominator` | `u64` |
| `host_fee_numerator`             | `u64` |
| `host_fee_denominator`           | `u64` |

### `CurveType`

> _From the program's IDL:_
>
> Curve types supported by the token-swap program.

Enum `types::CurveType`.

| Variant           |
| ----------------- |
| `Standard`        |
| `ConstantProduct` |

---

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