2025-07-24 19:37:34 +08:00
|
|
|
use crate::{
|
|
|
|
|
common::AnyResult,
|
2025-09-02 17:27:27 +08:00
|
|
|
streaming::{grpc::pool::factory, grpc::EventPretty, yellowstone_grpc::YellowstoneGrpc},
|
2025-07-24 19:37:34 +08:00
|
|
|
};
|
2025-08-29 14:59:16 +08:00
|
|
|
use futures::{SinkExt, StreamExt};
|
2025-07-19 23:46:42 +08:00
|
|
|
use log::error;
|
2025-07-24 19:37:34 +08:00
|
|
|
use solana_program::pubkey;
|
|
|
|
|
use solana_sdk::{pubkey::Pubkey, transaction::VersionedTransaction};
|
2025-08-26 17:57:39 +08:00
|
|
|
use solana_transaction_status::TransactionWithStatusMeta;
|
2025-08-29 14:59:16 +08:00
|
|
|
use yellowstone_grpc_proto::geyser::{
|
|
|
|
|
subscribe_update::UpdateOneof, SubscribeRequest, SubscribeRequestPing,
|
|
|
|
|
};
|
2025-07-19 23:46:42 +08:00
|
|
|
|
|
|
|
|
const SYSTEM_PROGRAM_ID: Pubkey = pubkey!("11111111111111111111111111111111");
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum SystemEvent {
|
|
|
|
|
NewTransfer(TransferInfo),
|
|
|
|
|
Error(String),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
|
|
|
pub struct TransferInfo {
|
|
|
|
|
pub slot: u64,
|
|
|
|
|
pub signature: String,
|
|
|
|
|
pub tx: Option<VersionedTransaction>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl YellowstoneGrpc {
|
|
|
|
|
pub async fn subscribe_system<F>(
|
|
|
|
|
&self,
|
|
|
|
|
callback: F,
|
|
|
|
|
account_include: Option<Vec<String>>,
|
|
|
|
|
account_exclude: Option<Vec<String>>,
|
|
|
|
|
) -> AnyResult<()>
|
|
|
|
|
where
|
2025-08-29 14:59:16 +08:00
|
|
|
F: Fn(SystemEvent) + Send + Sync + Clone + 'static,
|
2025-07-19 23:46:42 +08:00
|
|
|
{
|
|
|
|
|
let addrs = vec![SYSTEM_PROGRAM_ID.to_string()];
|
|
|
|
|
let account_include = account_include.unwrap_or_default();
|
|
|
|
|
let account_exclude = account_exclude.unwrap_or_default();
|
2025-08-11 01:04:16 +08:00
|
|
|
let transactions = self.subscription_manager.get_subscribe_request_filter(
|
|
|
|
|
account_include,
|
|
|
|
|
account_exclude,
|
|
|
|
|
addrs,
|
2025-08-16 14:48:51 +08:00
|
|
|
None,
|
2025-08-11 01:04:16 +08:00
|
|
|
);
|
2025-08-25 17:09:14 -07:00
|
|
|
let (mut subscribe_tx, mut stream, _) = self
|
2025-08-16 14:48:51 +08:00
|
|
|
.subscription_manager
|
|
|
|
|
.subscribe_with_request(transactions, None, None, None)
|
|
|
|
|
.await?;
|
2025-07-19 23:46:42 +08:00
|
|
|
|
|
|
|
|
let callback = Box::new(callback);
|
|
|
|
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
while let Some(message) = stream.next().await {
|
|
|
|
|
match message {
|
|
|
|
|
Ok(msg) => {
|
2025-08-29 14:59:16 +08:00
|
|
|
let created_at = msg.created_at;
|
|
|
|
|
match msg.update_oneof {
|
|
|
|
|
Some(UpdateOneof::Transaction(sut)) => {
|
2025-09-02 17:27:27 +08:00
|
|
|
let transaction_pretty =
|
|
|
|
|
factory::create_transaction_pretty_pooled(sut, created_at);
|
2025-08-29 14:59:16 +08:00
|
|
|
let event_pretty = EventPretty::Transaction(transaction_pretty);
|
2025-09-02 17:27:27 +08:00
|
|
|
if let Err(e) =
|
|
|
|
|
Self::process_system_transaction(event_pretty, &*callback).await
|
2025-08-31 22:18:18 +08:00
|
|
|
{
|
|
|
|
|
error!("Error processing transaction: {e:?}");
|
|
|
|
|
}
|
2025-08-29 14:59:16 +08:00
|
|
|
}
|
|
|
|
|
Some(UpdateOneof::Ping(_)) => {
|
|
|
|
|
let _ = subscribe_tx
|
|
|
|
|
.send(SubscribeRequest {
|
|
|
|
|
ping: Some(SubscribeRequestPing { id: 1 }),
|
|
|
|
|
..Default::default()
|
|
|
|
|
})
|
|
|
|
|
.await;
|
|
|
|
|
}
|
|
|
|
|
Some(UpdateOneof::Pong(_)) => {
|
|
|
|
|
// Pong response, no action needed
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
// Other message types, ignore for system subscription
|
2025-08-27 21:31:31 +08:00
|
|
|
}
|
2025-07-19 23:46:42 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(error) => {
|
|
|
|
|
error!("Stream error: {error:?}");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 01:04:16 +08:00
|
|
|
async fn process_system_transaction<F>(event_pretty: EventPretty, callback: &F) -> AnyResult<()>
|
2025-07-19 23:46:42 +08:00
|
|
|
where
|
|
|
|
|
F: Fn(SystemEvent) + Send + Sync,
|
|
|
|
|
{
|
2025-08-11 01:04:16 +08:00
|
|
|
match event_pretty {
|
|
|
|
|
EventPretty::Transaction(transaction_pretty) => {
|
2025-09-03 15:29:57 +08:00
|
|
|
let tx = yellowstone_grpc_proto::convert_from::create_tx_with_meta(
|
|
|
|
|
transaction_pretty.grpc_tx,
|
|
|
|
|
);
|
|
|
|
|
if let Ok(tx) = tx {
|
|
|
|
|
let trade_raw: TransactionWithStatusMeta = tx;
|
|
|
|
|
let meta = trade_raw.get_status_meta();
|
|
|
|
|
if meta.is_none() {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
let transaction = trade_raw.get_transaction();
|
|
|
|
|
callback(SystemEvent::NewTransfer(TransferInfo {
|
|
|
|
|
slot: transaction_pretty.slot,
|
|
|
|
|
signature: transaction_pretty.signature.to_string(),
|
|
|
|
|
tx: Some(transaction),
|
|
|
|
|
}));
|
2025-08-11 01:04:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
2025-07-19 23:46:42 +08:00
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|