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.
| Crate | carbon-associated-token-account-decoder 2.0.0 |
| Install | cargo add carbon-associated-token-account-decoder@2. |
| Program ID | ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL (carbon_) |
| Category | Core & SPL |
| Decoder | carbon_ |
| Instruction enum | instructions:: |
| Account enum | accounts:: (no variants) |
| Features | serde, postgres, graphql (none enabled by default) |
| API docs | docs.rs/carbon-associated-token-account-decoder/2.0.0 |
| Source | decoders/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:
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/fulluse {
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(())
}
}RUST_LOG=info cargo runEach 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::, where data and accounts are the types listed under the variant, exported from carbon_. 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:
- The instruction's program ID is
ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL. - The data starts with the variant's discriminator, and the rest deserializes into its data type.
- 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_ too. Accounts after the listed ones are in accounts. as AccountMetas. The decoder doesn't record whether an account signed or was writable: read that from input. and ., 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:
| # | Field | Type |
|---|---|---|
| 0 | funder | Pubkey |
| 1 | associated_ | Pubkey |
| 2 | wallet | Pubkey |
| 3 | mint | Pubkey |
| 4 | system_ | Pubkey |
| 5 | token_ | Pubkey |
| 6… | remaining | Vec< |
CreateIdempotent#
Discriminator 01 · data CreateIdempotent · accounts CreateIdempotentInstructionAccounts.
No data fields.
Required accounts: 6; extras go to remaining.
Fields of CreateIdempotentInstructionAccounts, in account order:
| # | Field | Type |
|---|---|---|
| 0 | funder | Pubkey |
| 1 | associated_ | Pubkey |
| 2 | wallet | Pubkey |
| 3 | mint | Pubkey |
| 4 | system_ | Pubkey |
| 5 | token_ | Pubkey |
| 6… | remaining | Vec< |
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:
| # | Field | Type |
|---|---|---|
| 0 | nested_ | Pubkey |
| 1 | nested_mint | Pubkey |
| 2 | destination_ | Pubkey |
| 3 | owner_ | Pubkey |
| 4 | owner_mint | Pubkey |
| 5 | wallet | Pubkey |
| 6 | owner_ | Pubkey |
| 7 | nested_ | Option< |
| 8… | remaining | Vec< |