2025-10-10 18:19:44 +08:00
|
|
|
use solana_streamer_sdk::streaming::{
|
2025-10-12 22:00:15 +08:00
|
|
|
event_parser::{Protocol, DexEvent},
|
2025-10-10 18:19:44 +08:00
|
|
|
shred::StreamClientConfig,
|
|
|
|
|
ShredStreamGrpc,
|
2025-07-19 23:46:42 +08:00
|
|
|
};
|
2025-01-03 00:38:45 +08:00
|
|
|
|
2025-01-02 19:44:38 +08:00
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2025-09-09 18:21:26 +08:00
|
|
|
println!("Starting ShredStream Streamer...");
|
2025-08-31 22:18:18 +08:00
|
|
|
test_shreds().await?;
|
2025-07-19 23:46:42 +08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn test_shreds() -> Result<(), Box<dyn std::error::Error>> {
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("Subscribing to ShredStream events...");
|
|
|
|
|
|
2025-08-04 21:30:55 +08:00
|
|
|
// Create low-latency configuration
|
2025-10-12 22:38:50 +08:00
|
|
|
let mut config = StreamClientConfig::default();
|
2025-08-04 21:30:55 +08:00
|
|
|
// Enable performance monitoring, has performance overhead, disabled by default
|
|
|
|
|
config.enable_metrics = true;
|
2025-08-03 21:59:08 +08:00
|
|
|
let shred_stream =
|
2025-08-29 14:59:16 +08:00
|
|
|
ShredStreamGrpc::new_with_config("http://127.0.0.1:10800".to_string(), config).await?;
|
2025-05-31 14:45:20 +08:00
|
|
|
|
2025-07-19 23:46:42 +08:00
|
|
|
let callback = create_event_callback();
|
|
|
|
|
let protocols = vec![
|
|
|
|
|
Protocol::PumpFun,
|
|
|
|
|
Protocol::PumpSwap,
|
|
|
|
|
Protocol::Bonk,
|
|
|
|
|
Protocol::RaydiumCpmm,
|
|
|
|
|
Protocol::RaydiumClmm,
|
2025-08-11 22:02:09 +08:00
|
|
|
Protocol::RaydiumAmmV4,
|
2025-07-19 23:46:42 +08:00
|
|
|
];
|
|
|
|
|
|
2025-08-18 01:08:12 +08:00
|
|
|
// 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] };
|
|
|
|
|
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("Listening for events, press Ctrl+C to stop...");
|
2025-08-18 01:08:12 +08:00
|
|
|
shred_stream.shredstream_subscribe(protocols, None, event_type_filter, callback).await?;
|
2025-07-19 23:46:42 +08:00
|
|
|
|
2025-08-21 15:47:31 +08:00
|
|
|
// 支持 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?;
|
|
|
|
|
|
2025-07-19 23:46:42 +08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-12 22:00:15 +08:00
|
|
|
fn create_event_callback() -> impl Fn(DexEvent) {
|
|
|
|
|
|event: DexEvent| {
|
2025-08-31 22:18:18 +08:00
|
|
|
println!(
|
2026-03-07 12:35:57 +08:00
|
|
|
"🎉 Event received! Type: {:?}, tx_index: {:?}",
|
2025-10-10 18:19:44 +08:00
|
|
|
event.metadata().event_type,
|
2026-03-07 12:35:57 +08:00
|
|
|
event.metadata().tx_index
|
2025-08-31 22:18:18 +08:00
|
|
|
);
|
2025-10-10 18:19:44 +08:00
|
|
|
match event {
|
2025-10-12 22:00:15 +08:00
|
|
|
DexEvent::BlockMetaEvent(e) => {
|
2025-09-03 15:49:50 +08:00
|
|
|
println!("BlockMetaEvent: {:?}", e.metadata.handle_us);
|
2025-10-10 18:19:44 +08:00
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
2025-07-19 23:46:42 +08:00
|
|
|
}
|
2025-10-10 18:19:44 +08:00
|
|
|
}
|