2025-07-19 23:55:37 +08:00
|
|
|
use solana_streamer_sdk::{
|
2025-07-19 23:46:42 +08:00
|
|
|
match_event,
|
|
|
|
|
streaming::{
|
|
|
|
|
event_parser::{
|
|
|
|
|
protocols::{
|
2025-07-26 17:11:28 +08:00
|
|
|
bonk::{parser::BONK_PROGRAM_ID, BonkPoolCreateEvent, BonkTradeEvent},
|
|
|
|
|
pumpfun::{parser::PUMPFUN_PROGRAM_ID, PumpFunCreateTokenEvent, PumpFunTradeEvent},
|
2025-07-19 23:46:42 +08:00
|
|
|
pumpswap::{
|
2025-07-26 17:11:28 +08:00
|
|
|
parser::PUMPSWAP_PROGRAM_ID, PumpSwapBuyEvent, PumpSwapCreatePoolEvent,
|
|
|
|
|
PumpSwapDepositEvent, PumpSwapSellEvent, PumpSwapWithdrawEvent,
|
2025-07-19 23:46:42 +08:00
|
|
|
},
|
2025-07-26 17:11:28 +08:00
|
|
|
raydium_clmm::{
|
|
|
|
|
parser::RAYDIUM_CLMM_PROGRAM_ID, RaydiumClmmSwapEvent, RaydiumClmmSwapV2Event,
|
|
|
|
|
},
|
|
|
|
|
raydium_cpmm::{parser::RAYDIUM_CPMM_PROGRAM_ID, RaydiumCpmmSwapEvent},
|
2025-07-19 23:46:42 +08:00
|
|
|
},
|
|
|
|
|
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...");
|
2025-07-19 23:46:42 +08:00
|
|
|
test_grpc().await?;
|
2025-07-25 16:43:06 +08:00
|
|
|
test_shreds().await?;
|
2025-07-19 23:46:42 +08:00
|
|
|
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-07-19 23:46:42 +08:00
|
|
|
|
2025-08-03 05:37:04 +08:00
|
|
|
// enable_metrics 为 true 时,会打印性能指标
|
|
|
|
|
let grpc = YellowstoneGrpc::new_with_config(
|
2025-07-19 23:46:42 +08:00
|
|
|
"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");
|
|
|
|
|
|
2025-07-19 23:46:42 +08:00
|
|
|
let callback = create_event_callback();
|
2025-07-26 17:11:28 +08:00
|
|
|
|
|
|
|
|
// Will try to parse corresponding protocol events from transactions
|
2025-07-19 23:46:42 +08:00
|
|
|
let protocols = vec![
|
2025-07-25 16:43:06 +08:00
|
|
|
Protocol::PumpFun,
|
|
|
|
|
Protocol::PumpSwap,
|
2025-07-19 23:46:42 +08:00
|
|
|
Protocol::Bonk,
|
2025-07-25 16:43:06 +08:00
|
|
|
Protocol::RaydiumCpmm,
|
|
|
|
|
Protocol::RaydiumClmm,
|
2025-07-19 23:46:42 +08:00
|
|
|
];
|
2025-01-02 19:44:38 +08:00
|
|
|
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("Protocols to monitor: {:?}", protocols);
|
|
|
|
|
|
2025-07-26 17:11:28 +08:00
|
|
|
// 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...");
|
|
|
|
|
|
2025-07-26 17:11:28 +08:00
|
|
|
grpc.subscribe_events_v2(
|
|
|
|
|
protocols,
|
|
|
|
|
None,
|
|
|
|
|
account_include,
|
|
|
|
|
account_exclude,
|
|
|
|
|
account_required,
|
|
|
|
|
None,
|
|
|
|
|
callback,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
2025-05-31 14:45:20 +08:00
|
|
|
|
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...");
|
|
|
|
|
|
|
|
|
|
// enable_metrics 为 true 时,会打印性能指标
|
|
|
|
|
let shred_stream = ShredStreamGrpc::new_with_config(
|
|
|
|
|
"http://127.0.0.1:10800".to_string(),
|
|
|
|
|
true,
|
|
|
|
|
).await?;
|
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-03 05:37:04 +08:00
|
|
|
println!("Listening for events, press Ctrl+C to stop...");
|
2025-07-19 23:46:42 +08:00
|
|
|
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());
|
2025-07-19 23:46:42 +08:00
|
|
|
match_event!(event, {
|
|
|
|
|
BonkPoolCreateEvent => |e: BonkPoolCreateEvent| {
|
2025-07-25 16:43:06 +08:00
|
|
|
// 使用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
|
|
|
},
|
2025-07-19 23:46:42 +08:00
|
|
|
BonkTradeEvent => |e: BonkTradeEvent| {
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("BonkTradeEvent: {e:?}");
|
2025-01-03 00:38:45 +08:00
|
|
|
},
|
2025-07-19 23:46:42 +08:00
|
|
|
PumpFunTradeEvent => |e: PumpFunTradeEvent| {
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("PumpFunTradeEvent: {e:?}");
|
2025-01-03 00:38:45 +08:00
|
|
|
},
|
2025-07-19 23:46:42 +08:00
|
|
|
PumpFunCreateTokenEvent => |e: PumpFunCreateTokenEvent| {
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("PumpFunCreateTokenEvent: {e:?}");
|
2025-05-31 14:45:20 +08:00
|
|
|
},
|
2025-07-19 23:46:42 +08:00
|
|
|
PumpSwapBuyEvent => |e: PumpSwapBuyEvent| {
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("Buy event: {e:?}");
|
2025-07-19 23:46:42 +08:00
|
|
|
},
|
|
|
|
|
PumpSwapSellEvent => |e: PumpSwapSellEvent| {
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("Sell event: {e:?}");
|
2025-07-19 23:46:42 +08:00
|
|
|
},
|
|
|
|
|
PumpSwapCreatePoolEvent => |e: PumpSwapCreatePoolEvent| {
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("CreatePool event: {e:?}");
|
2025-07-19 23:46:42 +08:00
|
|
|
},
|
|
|
|
|
PumpSwapDepositEvent => |e: PumpSwapDepositEvent| {
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("Deposit event: {e:?}");
|
2025-07-19 23:46:42 +08:00
|
|
|
},
|
|
|
|
|
PumpSwapWithdrawEvent => |e: PumpSwapWithdrawEvent| {
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("Withdraw event: {e:?}");
|
2025-07-19 23:46:42 +08:00
|
|
|
},
|
|
|
|
|
RaydiumCpmmSwapEvent => |e: RaydiumCpmmSwapEvent| {
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("RaydiumCpmmSwapEvent: {e:?}");
|
2025-07-19 23:46:42 +08:00
|
|
|
},
|
|
|
|
|
RaydiumClmmSwapEvent => |e: RaydiumClmmSwapEvent| {
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("RaydiumClmmSwapEvent: {e:?}");
|
2025-07-19 23:46:42 +08:00
|
|
|
},
|
|
|
|
|
RaydiumClmmSwapV2Event => |e: RaydiumClmmSwapV2Event| {
|
2025-08-03 05:37:04 +08:00
|
|
|
println!("RaydiumClmmSwapV2Event: {e:?}");
|
2025-01-03 00:38:45 +08:00
|
|
|
}
|
2025-07-19 23:46:42 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|