Files
solana-streamer/src/main.rs
T

157 lines
5.3 KiB
Rust
Raw Normal View History

use solana_streamer_sdk::{
match_event,
streaming::{
event_parser::{
protocols::{
bonk::{parser::BONK_PROGRAM_ID, BonkPoolCreateEvent, BonkTradeEvent},
pumpfun::{parser::PUMPFUN_PROGRAM_ID, PumpFunCreateTokenEvent, PumpFunTradeEvent},
pumpswap::{
parser::PUMPSWAP_PROGRAM_ID, PumpSwapBuyEvent, PumpSwapCreatePoolEvent,
PumpSwapDepositEvent, PumpSwapSellEvent, PumpSwapWithdrawEvent,
},
raydium_clmm::{
parser::RAYDIUM_CLMM_PROGRAM_ID, RaydiumClmmSwapEvent, RaydiumClmmSwapV2Event,
},
raydium_cpmm::{parser::RAYDIUM_CPMM_PROGRAM_ID, RaydiumCpmmSwapEvent},
},
Protocol, UnifiedEvent,
},
ShredStreamGrpc, YellowstoneGrpc,
},
};
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-08-03 05:37:04 +08:00
println!("Starting Solana Streamer...");
test_grpc().await?;
test_shreds().await?;
Ok(())
}
async fn test_grpc() -> Result<(), Box<dyn std::error::Error>> {
2025-08-03 05:37:04 +08:00
println!("Subscribing to Yellowstone gRPC events...");
2025-08-03 05:37:04 +08:00
// enable_metrics 为 true 时,会打印性能指标
let grpc = YellowstoneGrpc::new_with_config(
"https://solana-yellowstone-grpc.publicnode.com:443".to_string(),
2025-05-31 14:45:20 +08:00
None,
2025-08-03 05:37:04 +08:00
true,
2025-05-31 14:45:20 +08:00
)?;
2025-08-03 05:37:04 +08:00
println!("GRPC client created successfully");
let callback = create_event_callback();
// Will try to parse corresponding protocol events from transactions
let protocols = vec![
Protocol::PumpFun,
Protocol::PumpSwap,
Protocol::Bonk,
Protocol::RaydiumCpmm,
Protocol::RaydiumClmm,
];
2025-01-02 19:44:38 +08:00
2025-08-03 05:37:04 +08:00
println!("Protocols to monitor: {:?}", protocols);
// Filter accounts
let account_include = vec![
PUMPFUN_PROGRAM_ID.to_string(), // Listen to pumpfun program ID
PUMPSWAP_PROGRAM_ID.to_string(), // Listen to pumpswap program ID
BONK_PROGRAM_ID.to_string(), // Listen to bonk program ID
RAYDIUM_CPMM_PROGRAM_ID.to_string(), // Listen to raydium_cpmm program ID
RAYDIUM_CLMM_PROGRAM_ID.to_string(), // Listen to raydium_clmm program ID
];
let account_exclude = vec![];
let account_required = vec![];
println!("Starting to listen for events, press Ctrl+C to stop...");
2025-08-03 05:37:04 +08:00
println!("Monitoring programs: {:?}", account_include);
println!("Starting subscription...");
grpc.subscribe_events_v2(
protocols,
None,
account_include,
account_exclude,
account_required,
None,
callback,
)
.await?;
2025-05-31 14:45:20 +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...");
// enable_metrics 为 true 时,会打印性能指标
let shred_stream = ShredStreamGrpc::new_with_config(
"http://127.0.0.1:10800".to_string(),
true,
).await?;
let callback = create_event_callback();
let protocols = vec![
Protocol::PumpFun,
Protocol::PumpSwap,
Protocol::Bonk,
Protocol::RaydiumCpmm,
Protocol::RaydiumClmm,
];
2025-08-03 05:37:04 +08:00
println!("Listening for events, press Ctrl+C to stop...");
shred_stream
.shredstream_subscribe(protocols, None, callback)
.await?;
Ok(())
}
fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
|event: Box<dyn UnifiedEvent>| {
2025-08-03 05:37:04 +08:00
println!("🎉 Event received! Type: {:?}, ID: {}", event.event_type(), event.id());
match_event!(event, {
BonkPoolCreateEvent => |e: BonkPoolCreateEvent| {
// 使用grpc的时候,可以从每个事件中获取到block_time
println!("block_time: {:?}, block_time_ms: {:?}", e.metadata.block_time, e.metadata.block_time_ms);
println!("BonkPoolCreateEvent: {:?}", e.base_mint_param.symbol);
2025-05-31 14:45:20 +08:00
},
BonkTradeEvent => |e: BonkTradeEvent| {
2025-08-03 05:37:04 +08:00
println!("BonkTradeEvent: {e:?}");
2025-01-03 00:38:45 +08:00
},
PumpFunTradeEvent => |e: PumpFunTradeEvent| {
2025-08-03 05:37:04 +08:00
println!("PumpFunTradeEvent: {e:?}");
2025-01-03 00:38:45 +08:00
},
PumpFunCreateTokenEvent => |e: PumpFunCreateTokenEvent| {
2025-08-03 05:37:04 +08:00
println!("PumpFunCreateTokenEvent: {e:?}");
2025-05-31 14:45:20 +08:00
},
PumpSwapBuyEvent => |e: PumpSwapBuyEvent| {
2025-08-03 05:37:04 +08:00
println!("Buy event: {e:?}");
},
PumpSwapSellEvent => |e: PumpSwapSellEvent| {
2025-08-03 05:37:04 +08:00
println!("Sell event: {e:?}");
},
PumpSwapCreatePoolEvent => |e: PumpSwapCreatePoolEvent| {
2025-08-03 05:37:04 +08:00
println!("CreatePool event: {e:?}");
},
PumpSwapDepositEvent => |e: PumpSwapDepositEvent| {
2025-08-03 05:37:04 +08:00
println!("Deposit event: {e:?}");
},
PumpSwapWithdrawEvent => |e: PumpSwapWithdrawEvent| {
2025-08-03 05:37:04 +08:00
println!("Withdraw event: {e:?}");
},
RaydiumCpmmSwapEvent => |e: RaydiumCpmmSwapEvent| {
2025-08-03 05:37:04 +08:00
println!("RaydiumCpmmSwapEvent: {e:?}");
},
RaydiumClmmSwapEvent => |e: RaydiumClmmSwapEvent| {
2025-08-03 05:37:04 +08:00
println!("RaydiumClmmSwapEvent: {e:?}");
},
RaydiumClmmSwapV2Event => |e: RaydiumClmmSwapV2Event| {
2025-08-03 05:37:04 +08:00
println!("RaydiumClmmSwapV2Event: {e:?}");
2025-01-03 00:38:45 +08:00
}
});
}
}