mirror of
https://github.com/0xfnzero/solana-streamer.git
synced 2026-08-24 06:18:06 +00:00
feat: bridge streamer to sol-parser-sdk
This commit is contained in:
+159
-113
@@ -1,11 +1,32 @@
|
||||
//! RPC 单笔解析示例:
|
||||
//! - **默认**:本地 ix 路径(便于打印完整日志等)。
|
||||
//! - **对齐 sol-parser-sdk**:`SOL_PARSER_SDK_RPC=1` 时对同一笔 RPC 响应使用 `parse_encoded_rpc_transaction_as_streamer_events`。
|
||||
//!
|
||||
//! 亦可直接使用 crate 根的 `fetch_rpc_transaction_as_streamer_events_async`(单独 RPC 拉取 + 对齐解析)。
|
||||
//!
|
||||
//! 环境变量:`SOLANA_RPC_URL`(可选,默认 mainnet 公共 RPC)。
|
||||
|
||||
use anyhow::Result;
|
||||
use solana_commitment_config::CommitmentConfig;
|
||||
use solana_streamer_sdk::parse_encoded_rpc_transaction_as_streamer_events;
|
||||
use solana_streamer_sdk::streaming::event_parser::core::event_parser::EventParser;
|
||||
use solana_streamer_sdk::streaming::event_parser::Protocol;
|
||||
use solana_streamer_sdk::streaming::event_parser::DexEvent;
|
||||
use solana_streamer_sdk::streaming::event_parser::Protocol;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
/// Get transaction data based on transaction signature
|
||||
|
||||
fn rpc_url_from_env() -> String {
|
||||
std::env::var("SOLANA_RPC_URL")
|
||||
.unwrap_or_else(|_| "https://api.mainnet-beta.solana.com".to_string())
|
||||
}
|
||||
|
||||
fn use_sol_parser_sdk_rpc_path() -> bool {
|
||||
matches!(
|
||||
std::env::var("SOL_PARSER_SDK_RPC").as_deref(),
|
||||
Ok("1") | Ok("true") | Ok("yes") | Ok("TRUE") | Ok("YES")
|
||||
)
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let signatures = vec![
|
||||
@@ -34,19 +55,127 @@ async fn main() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 本地版本化消息 + inner ix 路径(与订阅管线中的「无 sdk」解析相近)。
|
||||
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};
|
||||
|
||||
println!("\n--- Parsed events (本地 ix 路径) ---\n");
|
||||
|
||||
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;
|
||||
let block_time = transaction.block_time.map(|t| Timestamp { seconds: t as i64, nanos: 0 });
|
||||
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(())
|
||||
}
|
||||
|
||||
/// Get details of a single transaction
|
||||
async fn get_single_transaction_details(signature_str: &str) -> Result<()> {
|
||||
use solana_sdk::{signature::Signature, pubkey::Pubkey, message::compiled_instruction::CompiledInstruction};
|
||||
use solana_transaction_status::{UiTransactionEncoding, InnerInstruction, InnerInstructions, UiInstruction};
|
||||
use prost_types::Timestamp;
|
||||
use solana_sdk::signature::Signature;
|
||||
use solana_transaction_status::UiTransactionEncoding;
|
||||
|
||||
let signature = Signature::from_str(signature_str)?;
|
||||
|
||||
// Create Solana RPC client
|
||||
let rpc_url = "https://api.mainnet-beta.solana.com";
|
||||
let rpc_url = rpc_url_from_env();
|
||||
println!("Connecting to Solana RPC: {}", rpc_url);
|
||||
|
||||
let client = solana_client::nonblocking::rpc_client::RpcClient::new(rpc_url.to_string());
|
||||
let client = solana_client::nonblocking::rpc_client::RpcClient::new(rpc_url);
|
||||
|
||||
match client
|
||||
.get_transaction_with_config(
|
||||
@@ -90,94 +219,10 @@ async fn get_single_transaction_details(signature_str: &str) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the transaction and extract necessary data
|
||||
let versioned_tx = match transaction.transaction.transaction.decode() {
|
||||
Some(tx) => tx,
|
||||
None => {
|
||||
println!("Failed to decode transaction");
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
// Convert inner instructions from meta
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract address table lookups
|
||||
let meta = transaction.transaction.meta;
|
||||
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()),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Build complete accounts list
|
||||
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;
|
||||
let block_time = transaction.block_time.map(|t| Timestamp { seconds: t as i64, nanos: 0 });
|
||||
let recv_us = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_micros() as i64;
|
||||
let bot_wallet = None;
|
||||
let tx_index = None;
|
||||
|
||||
let protocols = vec![
|
||||
Protocol::Bonk,
|
||||
@@ -187,29 +232,30 @@ async fn get_single_transaction_details(signature_str: &str) -> Result<()> {
|
||||
Protocol::RaydiumCpmm,
|
||||
Protocol::RaydiumAmmV4,
|
||||
Protocol::MeteoraDammV2,
|
||||
Protocol::OrcaWhirlpool,
|
||||
Protocol::MeteoraPools,
|
||||
Protocol::MeteoraDlmm,
|
||||
];
|
||||
|
||||
// Create callback
|
||||
let callback = Arc::new(move |event: DexEvent| {
|
||||
println!("{:?}\n", event);
|
||||
});
|
||||
|
||||
// Call parse_instruction_events_from_versioned_transaction
|
||||
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?;
|
||||
if use_sol_parser_sdk_rpc_path() {
|
||||
println!("\n--- Parsed events (SOL_PARSER_SDK_RPC=1, sol-parser-sdk 对齐) ---\n");
|
||||
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);
|
||||
}
|
||||
}
|
||||
Err(e) => println!("SDK-aligned parse error: {}", e),
|
||||
}
|
||||
} else {
|
||||
parse_local_ix_path(&transaction, signature, recv_us, &protocols).await?;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Failed to get transaction: {}", e);
|
||||
|
||||
Reference in New Issue
Block a user