2026-05-15 07:46:29 +08:00
|
|
|
//! Single RPC transaction parse example:
|
|
|
|
|
//! - Default: SDK-backed parsing through `parse_encoded_rpc_transaction_as_streamer_events`.
|
|
|
|
|
//! - Debug compare: set `STREAMER_LOCAL_IX=1` to run the local instruction path.
|
2026-05-15 05:09:29 +08:00
|
|
|
//!
|
2026-05-15 07:46:29 +08:00
|
|
|
//! You can also call `fetch_rpc_transaction_as_streamer_events_async` from the crate root
|
|
|
|
|
//! when you want the helper to fetch and parse by signature.
|
2026-05-15 05:09:29 +08:00
|
|
|
//!
|
2026-05-15 07:46:29 +08:00
|
|
|
//! Environment: `SOLANA_RPC_URL` (optional, defaults to the public mainnet RPC).
|
2026-05-15 05:09:29 +08:00
|
|
|
|
2025-08-29 14:59:16 +08:00
|
|
|
use anyhow::Result;
|
2025-09-26 23:59:06 +08:00
|
|
|
use solana_commitment_config::CommitmentConfig;
|
2026-05-15 05:09:29 +08:00
|
|
|
use solana_streamer_sdk::parse_encoded_rpc_transaction_as_streamer_events;
|
2025-09-14 01:13:11 +08:00
|
|
|
use solana_streamer_sdk::streaming::event_parser::core::event_parser::EventParser;
|
2025-10-12 22:00:15 +08:00
|
|
|
use solana_streamer_sdk::streaming::event_parser::DexEvent;
|
2026-05-15 05:09:29 +08:00
|
|
|
use solana_streamer_sdk::streaming::event_parser::Protocol;
|
2025-08-29 14:59:16 +08:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
use std::sync::Arc;
|
2026-05-15 05:09:29 +08:00
|
|
|
|
|
|
|
|
fn rpc_url_from_env() -> String {
|
|
|
|
|
std::env::var("SOLANA_RPC_URL")
|
|
|
|
|
.unwrap_or_else(|_| "https://api.mainnet-beta.solana.com".to_string())
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-15 07:46:29 +08:00
|
|
|
fn use_local_ix_path() -> bool {
|
2026-05-15 05:09:29 +08:00
|
|
|
matches!(
|
2026-05-15 07:46:29 +08:00
|
|
|
std::env::var("STREAMER_LOCAL_IX").as_deref(),
|
2026-05-15 05:09:29 +08:00
|
|
|
Ok("1") | Ok("true") | Ok("yes") | Ok("TRUE") | Ok("YES")
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-29 14:59:16 +08:00
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() -> Result<()> {
|
|
|
|
|
let signatures = vec![
|
2025-11-03 23:58:19 +08:00
|
|
|
"4PsHYajH87x2zJPEGZczZtd2ksibuMCFPonC24jk5mTGZ46hzvjpzM5UZuLz9sRv79MkCBbtDqwJapGPTSkCFKoL",
|
2025-08-29 14:59:16 +08:00
|
|
|
];
|
|
|
|
|
// Validate signature format
|
|
|
|
|
let mut valid_signatures = Vec::new();
|
|
|
|
|
for sig_str in &signatures {
|
|
|
|
|
match solana_sdk::signature::Signature::from_str(sig_str) {
|
|
|
|
|
Ok(_) => valid_signatures.push(*sig_str),
|
|
|
|
|
Err(e) => println!("Invalid signature format: {}", e),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if valid_signatures.is_empty() {
|
|
|
|
|
println!("No valid transaction signatures");
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
for signature in valid_signatures {
|
|
|
|
|
println!("Starting transaction parsing: {}", signature);
|
|
|
|
|
get_single_transaction_details(signature).await?;
|
|
|
|
|
println!("Transaction parsing completed: {}\n", signature);
|
|
|
|
|
println!("Visit link to compare data: \nhttps://solscan.io/tx/{}\n", signature);
|
|
|
|
|
println!("--------------------------------");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-15 07:46:29 +08:00
|
|
|
/// Local versioned-message + inner-instruction path, kept for parser debugging.
|
2026-05-15 05:09:29 +08:00
|
|
|
async fn parse_local_ix_path(
|
|
|
|
|
transaction: &solana_transaction_status::EncodedConfirmedTransactionWithStatusMeta,
|
|
|
|
|
signature: solana_sdk::signature::Signature,
|
|
|
|
|
recv_us: i64,
|
|
|
|
|
protocols: &[Protocol],
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
use prost_types::Timestamp;
|
|
|
|
|
use solana_sdk::{message::compiled_instruction::CompiledInstruction, pubkey::Pubkey};
|
|
|
|
|
use solana_transaction_status::{InnerInstruction, InnerInstructions, UiInstruction};
|
|
|
|
|
|
2026-05-15 07:46:29 +08:00
|
|
|
println!("\n--- Parsed events (STREAMER_LOCAL_IX=1, local instruction path) ---\n");
|
2026-05-15 05:09:29 +08:00
|
|
|
|
|
|
|
|
let versioned_tx = match transaction.transaction.transaction.decode() {
|
|
|
|
|
Some(tx) => tx,
|
|
|
|
|
None => {
|
|
|
|
|
println!("Failed to decode transaction");
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut inner_instructions_vec: Vec<InnerInstructions> = Vec::new();
|
|
|
|
|
if let Some(meta) = &transaction.transaction.meta {
|
|
|
|
|
if let solana_transaction_status::option_serializer::OptionSerializer::Some(
|
|
|
|
|
ui_inner_insts,
|
|
|
|
|
) = &meta.inner_instructions
|
|
|
|
|
{
|
|
|
|
|
for ui_inner in ui_inner_insts {
|
|
|
|
|
let mut converted_instructions = Vec::new();
|
|
|
|
|
|
|
|
|
|
for ui_instruction in &ui_inner.instructions {
|
|
|
|
|
if let UiInstruction::Compiled(ui_compiled) = ui_instruction {
|
|
|
|
|
if let Ok(data) = solana_sdk::bs58::decode(&ui_compiled.data).into_vec() {
|
|
|
|
|
let compiled_instruction = CompiledInstruction {
|
|
|
|
|
program_id_index: ui_compiled.program_id_index,
|
|
|
|
|
accounts: ui_compiled.accounts.to_vec(),
|
|
|
|
|
data,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let inner_instruction = InnerInstruction {
|
|
|
|
|
instruction: compiled_instruction,
|
|
|
|
|
stack_height: ui_compiled.stack_height,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
converted_instructions.push(inner_instruction);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let inner_instructions = InnerInstructions {
|
|
|
|
|
index: ui_inner.index,
|
|
|
|
|
instructions: converted_instructions,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
inner_instructions_vec.push(inner_instructions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let meta = transaction.transaction.meta.clone();
|
|
|
|
|
let mut address_table_lookups: Vec<Pubkey> = vec![];
|
|
|
|
|
if let Some(meta) = meta {
|
|
|
|
|
if let solana_transaction_status::option_serializer::OptionSerializer::Some(
|
|
|
|
|
loaded_addresses,
|
|
|
|
|
) = &meta.loaded_addresses
|
|
|
|
|
{
|
|
|
|
|
address_table_lookups
|
|
|
|
|
.reserve(loaded_addresses.writable.len() + loaded_addresses.readonly.len());
|
|
|
|
|
address_table_lookups.extend(
|
|
|
|
|
loaded_addresses.writable.iter().filter_map(|s| s.parse::<Pubkey>().ok()).chain(
|
|
|
|
|
loaded_addresses.readonly.iter().filter_map(|s| s.parse::<Pubkey>().ok()),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut accounts = Vec::with_capacity(
|
|
|
|
|
versioned_tx.message.static_account_keys().len() + address_table_lookups.len(),
|
|
|
|
|
);
|
|
|
|
|
accounts.extend_from_slice(versioned_tx.message.static_account_keys());
|
|
|
|
|
accounts.extend(address_table_lookups);
|
|
|
|
|
|
|
|
|
|
let slot = transaction.slot;
|
2026-06-01 20:20:01 +08:00
|
|
|
let block_time = transaction.block_time.map(|t| Timestamp { seconds: t, nanos: 0 });
|
2026-05-15 05:09:29 +08:00
|
|
|
let bot_wallet = None;
|
|
|
|
|
let tx_index = None;
|
|
|
|
|
|
|
|
|
|
let callback = Arc::new(move |event: DexEvent| {
|
|
|
|
|
println!("{:?}\n", event);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
EventParser::parse_instruction_events_from_versioned_transaction(
|
|
|
|
|
protocols,
|
|
|
|
|
None,
|
|
|
|
|
&versioned_tx,
|
|
|
|
|
signature,
|
|
|
|
|
Some(slot),
|
|
|
|
|
block_time,
|
|
|
|
|
recv_us,
|
|
|
|
|
&accounts,
|
|
|
|
|
&inner_instructions_vec,
|
|
|
|
|
bot_wallet,
|
|
|
|
|
tx_index,
|
|
|
|
|
callback,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-29 14:59:16 +08:00
|
|
|
/// Get details of a single transaction
|
|
|
|
|
async fn get_single_transaction_details(signature_str: &str) -> Result<()> {
|
2026-05-15 05:09:29 +08:00
|
|
|
use solana_sdk::signature::Signature;
|
|
|
|
|
use solana_transaction_status::UiTransactionEncoding;
|
2025-08-29 14:59:16 +08:00
|
|
|
|
|
|
|
|
let signature = Signature::from_str(signature_str)?;
|
|
|
|
|
|
2026-05-15 05:09:29 +08:00
|
|
|
let rpc_url = rpc_url_from_env();
|
2025-08-29 14:59:16 +08:00
|
|
|
println!("Connecting to Solana RPC: {}", rpc_url);
|
|
|
|
|
|
2026-05-15 05:09:29 +08:00
|
|
|
let client = solana_client::nonblocking::rpc_client::RpcClient::new(rpc_url);
|
2025-08-29 14:59:16 +08:00
|
|
|
|
|
|
|
|
match client
|
|
|
|
|
.get_transaction_with_config(
|
|
|
|
|
&signature,
|
|
|
|
|
solana_client::rpc_config::RpcTransactionConfig {
|
|
|
|
|
encoding: Some(UiTransactionEncoding::Base64),
|
|
|
|
|
commitment: Some(CommitmentConfig::confirmed()),
|
|
|
|
|
max_supported_transaction_version: Some(0),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(transaction) => {
|
|
|
|
|
println!("Transaction signature: {}", signature_str);
|
|
|
|
|
println!("Block slot: {}", transaction.slot);
|
|
|
|
|
|
|
|
|
|
if let Some(block_time) = transaction.block_time {
|
|
|
|
|
println!("Block time: {}", block_time);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(meta) = &transaction.transaction.meta {
|
|
|
|
|
println!("Transaction fee: {} lamports", meta.fee);
|
|
|
|
|
println!("Status: {}", if meta.err.is_none() { "Success" } else { "Failed" });
|
|
|
|
|
if let Some(err) = &meta.err {
|
|
|
|
|
println!("Error details: {:?}", err);
|
|
|
|
|
}
|
|
|
|
|
// Compute units consumed
|
|
|
|
|
if let solana_transaction_status::option_serializer::OptionSerializer::Some(units) =
|
|
|
|
|
&meta.compute_units_consumed
|
|
|
|
|
{
|
|
|
|
|
println!("Compute units consumed: {}", units);
|
|
|
|
|
}
|
|
|
|
|
// Display logs (all)
|
|
|
|
|
if let solana_transaction_status::option_serializer::OptionSerializer::Some(logs) =
|
|
|
|
|
&meta.log_messages
|
|
|
|
|
{
|
|
|
|
|
println!("Transaction logs (all {} entries):", logs.len());
|
|
|
|
|
for (i, log) in logs.iter().enumerate() {
|
|
|
|
|
println!(" [{}] {}", i + 1, log);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-04 22:49:40 +08:00
|
|
|
|
|
|
|
|
let recv_us = std::time::SystemTime::now()
|
|
|
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_micros() as i64;
|
|
|
|
|
|
2025-08-29 14:59:16 +08:00
|
|
|
let protocols = vec![
|
|
|
|
|
Protocol::Bonk,
|
|
|
|
|
Protocol::RaydiumClmm,
|
|
|
|
|
Protocol::PumpSwap,
|
|
|
|
|
Protocol::PumpFun,
|
|
|
|
|
Protocol::RaydiumCpmm,
|
|
|
|
|
Protocol::RaydiumAmmV4,
|
2025-11-10 00:38:23 +08:00
|
|
|
Protocol::MeteoraDammV2,
|
2026-05-15 05:09:29 +08:00
|
|
|
Protocol::OrcaWhirlpool,
|
|
|
|
|
Protocol::MeteoraPools,
|
|
|
|
|
Protocol::MeteoraDlmm,
|
2025-08-29 14:59:16 +08:00
|
|
|
];
|
2025-11-04 22:49:40 +08:00
|
|
|
|
2026-05-15 07:46:29 +08:00
|
|
|
if use_local_ix_path() {
|
|
|
|
|
parse_local_ix_path(&transaction, signature, recv_us, &protocols).await?;
|
|
|
|
|
} else {
|
|
|
|
|
println!("\n--- Parsed events (default SDK-backed RPC path) ---\n");
|
2026-05-15 05:09:29 +08:00
|
|
|
match parse_encoded_rpc_transaction_as_streamer_events(
|
|
|
|
|
&transaction,
|
|
|
|
|
recv_us,
|
|
|
|
|
&protocols,
|
|
|
|
|
None,
|
|
|
|
|
) {
|
|
|
|
|
Ok(events) => {
|
|
|
|
|
println!("Total {} streamer DexEvent(s):\n", events.len());
|
|
|
|
|
for ev in events {
|
|
|
|
|
println!("{:?}\n", ev);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-15 07:46:29 +08:00
|
|
|
Err(e) => println!("SDK-backed parse error: {}", e),
|
2026-05-15 05:09:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-29 14:59:16 +08:00
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
println!("Failed to get transaction: {}", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|