Expand description
§anchor-parser
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.1.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:
| Module | Contents |
|---|---|
accounts | Account structs with AccountDeserialize and from_account_data |
events | Event structs with from_logs and from_cpi_logs |
instructions | Builder functions returning solana_sdk::instruction::Instruction |
types | Shared structs, enums, and type aliases from the IDL |
constants | Program constants with doc comments |
utils | Event / 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§
- Account
Deserialize - Trait implemented by all generated account types.