Skip to main content

Crate anchor_parser

Crate anchor_parser 

Source
Expand description

§anchor-parser

Crates.io docs.rs MIT License

Generate Rust types and helpers from Anchor IDL JSON files using [solana-sdk] types directly — no anchor-lang dependency required.

§Quick start

[dependencies]
anchor-parser = "2.0.0"

Place an Anchor IDL JSON file at <crate-root>/idls/<name>.json, then:

anchor_parser::declare_program!(my_program);

§Generated modules

declare_program! generates a module with the following sub-modules:

ModuleContents
accountsAccount structs with AccountDeserialize and from_account_data
eventsEvent structs with from_logs and from_cpi_logs
instructionsBuilder functions returning solana_sdk::instruction::Instruction
typesShared structs, enums, and type aliases from the IDL
constantsProgram constants with doc comments
utilsEvent / Account wrapper enums for generic parsing

§Examples

§Deserializing accounts

use my_program::accounts::MyAccount;

// Deserialize from raw account data (discriminator + payload)
let account = MyAccount::from_account_data(&raw_bytes)?;

// Check discriminator
assert_eq!(MyAccount::DISCRIMINATOR, [/* 8 bytes */]);

§Parsing events

use my_program::events::SwapEvent;
use my_program::utils::Event;

// Parse events from emit! log lines
let events = SwapEvent::from_logs(&log_messages);
let all_events = Event::from_logs(&log_messages);

// Parse events from emit_cpi! inner instruction data (bs58-encoded)
let events = SwapEvent::from_cpi_logs(&inner_ix_data_strings);
let all_events = Event::from_cpi_logs(&inner_ix_data_strings);

§Building instructions

use my_program::instructions;

let ix = instructions::swap(
    &my_program::ID,
    &instructions::SwapAccounts { /* ... */ },
    amount,
    min_out,
)?;

Macros§

declare_program
Generates a module from an Anchor IDL JSON file.

Traits§

AccountDeserialize
Trait implemented by all generated account types.