Associated Token Account decoder

Decode Associated Token Account instructions in Rust with the carbon-associated-token-account-decoder crate.

On this page

carbon-associated-token-account-decoder decodes the instructions of the Associated Token Account program (ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL): 3 instructions. It decodes no accounts: AssociatedTokenAccountAccount has no variants. It is generated with Codama from the program's IDL; see decoders for how decoders plug into a pipeline.

Cratecarbon-associated-token-account-decoder 2.0.0
Installcargo add carbon-associated-token-account-decoder@2.0.0
Program IDATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL (carbon_associated_token_account_decoder::PROGRAM_ID)
CategoryCore & SPL
Decodercarbon_associated_token_account_decoder::AssociatedTokenAccountDecoder
Instruction enuminstructions::AssociatedTokenAccountInstruction
Account enumaccounts::AssociatedTokenAccountAccount (no variants)
Featuresserde, postgres, graphql (none enabled by default)
API docsdocs.rs/carbon-associated-token-account-decoder/2.0.0
Sourcedecoders/associated-token-account-decoder

Other core and SPL decoders: Address Lookup Table, SPL Memo, SPL Name Service, SPL Token, Stake Program, System Program, Token-2022. To decode several in one pipeline, see core and SPL program decoders.

The serde feature derives Serialize/Deserialize on every type, postgres adds tables and processors for Postgres, and graphql adds a schema for the GraphQL server.

Usage#

This program crawls recent Associated Token Account transactions from the public RPC and prints every decoded CreateIdempotent instruction. Add the dependencies:

Terminal
cargo add carbon-core@2.0.0 carbon-associated-token-account-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
use {
    carbon_core::{
        error::CarbonResult, instruction::InstructionProcessorInputType, pipeline::Pipeline,
        processor::Processor,
    },
    carbon_log_metrics::LogMetrics,
    carbon_associated_token_account_decoder::{
        instructions::AssociatedTokenAccountInstruction, AssociatedTokenAccountDecoder, 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(AssociatedTokenAccountDecoder, InstructionLogger)
        .build()?
        .run()
        .await
}

struct InstructionLogger;

impl Processor<InstructionProcessorInputType<'_, AssociatedTokenAccountInstruction>> for InstructionLogger {
    async fn process(
        &mut self,
        input: &InstructionProcessorInputType<'_, AssociatedTokenAccountInstruction>,
    ) -> CarbonResult<()> {
        let signature = input.metadata.transaction_metadata.signature;
        if let AssociatedTokenAccountInstruction::CreateIdempotent { data, accounts, .. } = input.decoded_instruction {
            println!("create_idempotent {signature}: {data:?} funder={}", accounts.funder);
        }
        Ok(())
    }
}
Terminal
RUST_LOG=info cargo run

Each CreateIdempotent instruction prints one line with the transaction signature and the decoded CreateIdempotent 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 for the crawler options.

Instructions#

Variants of AssociatedTokenAccountInstruction. Each is AssociatedTokenAccountInstruction::<Variant> { program_id, data, accounts }, where data and accounts are the types listed under the variant, exported from carbon_associated_token_account_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 ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL.
  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 AccountMetas. 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.

Create#

Discriminator 00 · data Create · accounts CreateInstructionAccounts.

No data fields.

Required accounts: 6; extras go to remaining.

Fields of CreateInstructionAccounts, in account order:

#FieldType
0funderPubkey
1associated_token_accountPubkey
2walletPubkey
3mintPubkey
4system_programPubkey
5token_programPubkey
6…remainingVec<AccountMeta>

CreateIdempotent#

Discriminator 01 · data CreateIdempotent · accounts CreateIdempotentInstructionAccounts.

No data fields.

Required accounts: 6; extras go to remaining.

Fields of CreateIdempotentInstructionAccounts, in account order:

#FieldType
0funderPubkey
1associated_token_accountPubkey
2walletPubkey
3mintPubkey
4system_programPubkey
5token_programPubkey
6…remainingVec<AccountMeta>

RecoverNested#

Discriminator 02 · data RecoverNested · accounts RecoverNestedInstructionAccounts.

No data fields.

Required accounts: 7 (8 listed); extras go to remaining.

Fields of RecoverNestedInstructionAccounts, in account order:

#FieldType
0nested_associated_token_accountPubkey
1nested_mintPubkey
2destination_associated_token_accountPubkey
3owner_associated_token_accountPubkey
4owner_mintPubkey
5walletPubkey
6owner_token_programPubkey
7nested_token_programOption<Pubkey>
8…remainingVec<AccountMeta>

Generated from the crate’s source at Carbon v2.0.0, so it always matches the published code. Spotted a problem? Report it; edits to this page itself are overwritten on the next release.

Your endpoints

Code samples use these instead of the placeholders, so you can copy and run them as they are. They’re stored only in this browser and never sent anywhere.