Files
solana-streamer/examples/shred_example.rs
T
ysq 0c07c4d9ee refactor: enhance account event parser with more precise event classification
- Rename CommonAccountEvent to TokenAccountEvent for better semantic clarity
- Rename AccountNonce to NonceAccount for consistent naming convention
- Add MintInfoEvent type to support SPL Token Mint account parsing
- Enhance parse_token_account_event to automatically detect and parse Mint accounts
- Update all example code to use new event types and structures
- Improve event type semantics for better developer experience
2025-09-09 23:42:35 +08:00

253 lines
12 KiB
Rust

use solana_streamer_sdk::{
match_event,
streaming::{
event_parser::{
common::EventType,
core::account_event_parser::TokenAccountEvent,
protocols::{
bonk::{
BonkGlobalConfigAccountEvent, BonkMigrateToAmmEvent,
BonkMigrateToCpswapEvent, BonkPlatformConfigAccountEvent, BonkPoolCreateEvent,
BonkPoolStateAccountEvent, BonkTradeEvent,
},
pumpfun::{
PumpFunBondingCurveAccountEvent,
PumpFunCreateTokenEvent, PumpFunGlobalAccountEvent, PumpFunMigrateEvent,
PumpFunTradeEvent,
},
pumpswap::{
PumpSwapBuyEvent, PumpSwapCreatePoolEvent,
PumpSwapDepositEvent, PumpSwapGlobalConfigAccountEvent,
PumpSwapPoolAccountEvent, PumpSwapSellEvent, PumpSwapWithdrawEvent,
},
raydium_amm_v4::{
RaydiumAmmV4AmmInfoAccountEvent,
RaydiumAmmV4DepositEvent, RaydiumAmmV4Initialize2Event, RaydiumAmmV4SwapEvent,
RaydiumAmmV4WithdrawEvent, RaydiumAmmV4WithdrawPnlEvent,
},
raydium_clmm::{
RaydiumClmmAmmConfigAccountEvent,
RaydiumClmmClosePositionEvent, RaydiumClmmCreatePoolEvent,
RaydiumClmmDecreaseLiquidityV2Event, RaydiumClmmIncreaseLiquidityV2Event,
RaydiumClmmOpenPositionV2Event, RaydiumClmmOpenPositionWithToken22NftEvent,
RaydiumClmmPoolStateAccountEvent, RaydiumClmmSwapEvent, RaydiumClmmSwapV2Event,
RaydiumClmmTickArrayStateAccountEvent,
},
raydium_cpmm::{
RaydiumCpmmAmmConfigAccountEvent,
RaydiumCpmmDepositEvent, RaydiumCpmmInitializeEvent,
RaydiumCpmmPoolStateAccountEvent, RaydiumCpmmSwapEvent,
RaydiumCpmmWithdrawEvent,
},
BlockMetaEvent,
},
Protocol, UnifiedEvent,
},
shred::StreamClientConfig,
ShredStreamGrpc,
},
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Starting ShredStream Streamer...");
test_shreds().await?;
Ok(())
}
async fn test_shreds() -> Result<(), Box<dyn std::error::Error>> {
println!("Subscribing to ShredStream events...");
// Create low-latency configuration
let mut config = StreamClientConfig::low_latency();
// Enable performance monitoring, has performance overhead, disabled by default
config.enable_metrics = true;
let shred_stream =
ShredStreamGrpc::new_with_config("http://127.0.0.1:10800".to_string(), config).await?;
let callback = create_event_callback();
let protocols = vec![
Protocol::PumpFun,
Protocol::PumpSwap,
Protocol::Bonk,
Protocol::RaydiumCpmm,
Protocol::RaydiumClmm,
Protocol::RaydiumAmmV4,
];
// Event filtering
// No event filtering, includes all events
let event_type_filter = None;
// Only include PumpSwapBuy events and PumpSwapSell events
// let event_type_filter =
// EventTypeFilter { include: vec![EventType::PumpSwapBuy, EventType::PumpSwapSell] };
println!("Listening for events, press Ctrl+C to stop...");
shred_stream.shredstream_subscribe(protocols, None, event_type_filter, callback).await?;
// 支持 stop 方法,测试代码 - 异步1000秒之后停止
let shred_clone = shred_stream.clone();
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_secs(1000)).await;
shred_clone.stop().await;
});
println!("Waiting for Ctrl+C to stop...");
tokio::signal::ctrl_c().await?;
Ok(())
}
fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
|event: Box<dyn UnifiedEvent>| {
println!(
"🎉 Event received! Type: {:?}, transaction_index: {:?}",
event.event_type(),
event.transaction_index()
);
match_event!(event, {
// -------------------------- block meta -----------------------
BlockMetaEvent => |e: BlockMetaEvent| {
println!("BlockMetaEvent: {:?}", e.metadata.handle_us);
},
// -------------------------- bonk -----------------------
BonkPoolCreateEvent => |e: BonkPoolCreateEvent| {
// When using grpc, you can get block_time from each event
println!("block_time: {:?}, block_time_ms: {:?}", e.metadata.block_time, e.metadata.block_time_ms);
println!("BonkPoolCreateEvent: {:?}", e.base_mint_param.symbol);
},
BonkTradeEvent => |e: BonkTradeEvent| {
println!("BonkTradeEvent: {e:?}");
},
BonkMigrateToAmmEvent => |e: BonkMigrateToAmmEvent| {
println!("BonkMigrateToAmmEvent: {e:?}");
},
BonkMigrateToCpswapEvent => |e: BonkMigrateToCpswapEvent| {
println!("BonkMigrateToCpswapEvent: {e:?}");
},
// -------------------------- pumpfun -----------------------
PumpFunTradeEvent => |e: PumpFunTradeEvent| {
println!("PumpFunTradeEvent: {e:?}");
},
PumpFunMigrateEvent => |e: PumpFunMigrateEvent| {
println!("PumpFunMigrateEvent: {e:?}");
},
PumpFunCreateTokenEvent => |e: PumpFunCreateTokenEvent| {
println!("PumpFunCreateTokenEvent: {e:?}");
},
// -------------------------- pumpswap -----------------------
PumpSwapBuyEvent => |e: PumpSwapBuyEvent| {
println!("Buy event: {e:?}");
},
PumpSwapSellEvent => |e: PumpSwapSellEvent| {
println!("Sell event: {e:?}");
},
PumpSwapCreatePoolEvent => |e: PumpSwapCreatePoolEvent| {
println!("CreatePool event: {e:?}");
},
PumpSwapDepositEvent => |e: PumpSwapDepositEvent| {
println!("Deposit event: {e:?}");
},
PumpSwapWithdrawEvent => |e: PumpSwapWithdrawEvent| {
println!("Withdraw event: {e:?}");
},
// -------------------------- raydium_cpmm -----------------------
RaydiumCpmmSwapEvent => |e: RaydiumCpmmSwapEvent| {
println!("RaydiumCpmmSwapEvent: {e:?}");
},
RaydiumCpmmDepositEvent => |e: RaydiumCpmmDepositEvent| {
println!("RaydiumCpmmDepositEvent: {e:?}");
},
RaydiumCpmmInitializeEvent => |e: RaydiumCpmmInitializeEvent| {
println!("RaydiumCpmmInitializeEvent: {e:?}");
},
RaydiumCpmmWithdrawEvent => |e: RaydiumCpmmWithdrawEvent| {
println!("RaydiumCpmmWithdrawEvent: {e:?}");
},
// -------------------------- raydium_clmm -----------------------
RaydiumClmmSwapEvent => |e: RaydiumClmmSwapEvent| {
println!("RaydiumClmmSwapEvent: {e:?}");
},
RaydiumClmmSwapV2Event => |e: RaydiumClmmSwapV2Event| {
println!("RaydiumClmmSwapV2Event: {e:?}");
},
RaydiumClmmClosePositionEvent => |e: RaydiumClmmClosePositionEvent| {
println!("RaydiumClmmClosePositionEvent: {e:?}");
},
RaydiumClmmDecreaseLiquidityV2Event => |e: RaydiumClmmDecreaseLiquidityV2Event| {
println!("RaydiumClmmDecreaseLiquidityV2Event: {e:?}");
},
RaydiumClmmCreatePoolEvent => |e: RaydiumClmmCreatePoolEvent| {
println!("RaydiumClmmCreatePoolEvent: {e:?}");
},
RaydiumClmmIncreaseLiquidityV2Event => |e: RaydiumClmmIncreaseLiquidityV2Event| {
println!("RaydiumClmmIncreaseLiquidityV2Event: {e:?}");
},
RaydiumClmmOpenPositionWithToken22NftEvent => |e: RaydiumClmmOpenPositionWithToken22NftEvent| {
println!("RaydiumClmmOpenPositionWithToken22NftEvent: {e:?}");
},
RaydiumClmmOpenPositionV2Event => |e: RaydiumClmmOpenPositionV2Event| {
println!("RaydiumClmmOpenPositionV2Event: {e:?}");
},
// -------------------------- raydium_amm_v4 -----------------------
RaydiumAmmV4SwapEvent => |e: RaydiumAmmV4SwapEvent| {
println!("RaydiumAmmV4SwapEvent: {e:?}");
},
RaydiumAmmV4DepositEvent => |e: RaydiumAmmV4DepositEvent| {
println!("RaydiumAmmV4DepositEvent: {e:?}");
},
RaydiumAmmV4Initialize2Event => |e: RaydiumAmmV4Initialize2Event| {
println!("RaydiumAmmV4Initialize2Event: {e:?}");
},
RaydiumAmmV4WithdrawEvent => |e: RaydiumAmmV4WithdrawEvent| {
println!("RaydiumAmmV4WithdrawEvent: {e:?}");
},
RaydiumAmmV4WithdrawPnlEvent => |e: RaydiumAmmV4WithdrawPnlEvent| {
println!("RaydiumAmmV4WithdrawPnlEvent: {e:?}");
},
// -------------------------- account -----------------------
BonkPoolStateAccountEvent => |e: BonkPoolStateAccountEvent| {
println!("BonkPoolStateAccountEvent: {e:?}");
},
BonkGlobalConfigAccountEvent => |e: BonkGlobalConfigAccountEvent| {
println!("BonkGlobalConfigAccountEvent: {e:?}");
},
BonkPlatformConfigAccountEvent => |e: BonkPlatformConfigAccountEvent| {
println!("BonkPlatformConfigAccountEvent: {e:?}");
},
PumpSwapGlobalConfigAccountEvent => |e: PumpSwapGlobalConfigAccountEvent| {
println!("PumpSwapGlobalConfigAccountEvent: {e:?}");
},
PumpSwapPoolAccountEvent => |e: PumpSwapPoolAccountEvent| {
println!("PumpSwapPoolAccountEvent: {e:?}");
},
PumpFunBondingCurveAccountEvent => |e: PumpFunBondingCurveAccountEvent| {
println!("PumpFunBondingCurveAccountEvent: {e:?}");
},
PumpFunGlobalAccountEvent => |e: PumpFunGlobalAccountEvent| {
println!("PumpFunGlobalAccountEvent: {e:?}");
},
RaydiumAmmV4AmmInfoAccountEvent => |e: RaydiumAmmV4AmmInfoAccountEvent| {
println!("RaydiumAmmV4AmmInfoAccountEvent: {e:?}");
},
RaydiumClmmAmmConfigAccountEvent => |e: RaydiumClmmAmmConfigAccountEvent| {
println!("RaydiumClmmAmmConfigAccountEvent: {e:?}");
},
RaydiumClmmPoolStateAccountEvent => |e: RaydiumClmmPoolStateAccountEvent| {
println!("RaydiumClmmPoolStateAccountEvent: {e:?}");
},
RaydiumClmmTickArrayStateAccountEvent => |e: RaydiumClmmTickArrayStateAccountEvent| {
println!("RaydiumClmmTickArrayStateAccountEvent: {e:?}");
},
RaydiumCpmmAmmConfigAccountEvent => |e: RaydiumCpmmAmmConfigAccountEvent| {
println!("RaydiumCpmmAmmConfigAccountEvent: {e:?}");
},
RaydiumCpmmPoolStateAccountEvent => |e: RaydiumCpmmPoolStateAccountEvent| {
println!("RaydiumCpmmPoolStateAccountEvent: {e:?}");
},
TokenAccountEvent => |e: TokenAccountEvent| {
println!("TokenAccountEvent: {e:?}");
},
});
}
}