forked from 0xfnzero/solana-streamer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshred_example.rs
More file actions
71 lines (61 loc) · 2.21 KB
/
shred_example.rs
File metadata and controls
71 lines (61 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use solana_streamer_sdk::streaming::{
event_parser::{Protocol, DexEvent},
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::default();
// 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(DexEvent) {
|event: DexEvent| {
println!(
"🎉 Event received! Type: {:?}, transaction_index: {:?}",
event.metadata().event_type,
event.metadata().transaction_index
);
match event {
DexEvent::BlockMetaEvent(e) => {
println!("BlockMetaEvent: {:?}", e.metadata.handle_us);
}
_ => {}
}
}
}