#![allow(deprecated)]
use std::sync::Arc;
use std::time::Duration;
use crossbeam_queue::ArrayQueue;
use futures::StreamExt;
use solana_entry::entry::Entry as SolanaEntry;
use solana_sdk::message::VersionedMessage;
use tokio::sync::Mutex;
use tokio::task::JoinHandle;
use tonic::transport::{Channel, Endpoint};
use crate::core::now_micros;
use crate::grpc::types::EventTypeFilter;
use crate::shredstream::config::ShredStreamConfig;
use crate::shredstream::proto::{Entry, ShredstreamProxyClient, SubscribeEntriesRequest};
use crate::DexEvent;
#[derive(Clone)]
pub struct ShredStreamClient {
endpoint: String,
config: ShredStreamConfig,
subscription_handle: Arc<Mutex<Option<JoinHandle<()>>>>,
}
impl ShredStreamClient {
pub async fn new(endpoint: impl Into<String>) -> crate::common::AnyResult<Self> {
Self::new_with_config(endpoint, ShredStreamConfig::default()).await
}
pub async fn new_with_config(
endpoint: impl Into<String>,
config: ShredStreamConfig,
) -> crate::common::AnyResult<Self> {
let endpoint = endpoint.into();
let _ = Self::connect_client(&endpoint, &config).await?;
Ok(Self { endpoint, config, subscription_handle: Arc::new(Mutex::new(None)) })
}
pub async fn subscribe(&self) -> crate::common::AnyResult<Arc<ArrayQueue<DexEvent>>> {
self.subscribe_with_filter(None).await
}
pub async fn subscribe_with_filter(
&self,
event_type_filter: Option<EventTypeFilter>,
) -> crate::common::AnyResult<Arc<ArrayQueue<DexEvent>>> {
self.stop().await;
let queue = Arc::new(ArrayQueue::new(100_000));
let queue_clone = Arc::clone(&queue);
let endpoint = self.endpoint.clone();
let config = self.config.clone();
let handle = tokio::spawn(async move {
let mut delay = config.reconnect_delay_ms;
let mut attempts = 0u32;
loop {
if config.max_reconnect_attempts > 0 && attempts >= config.max_reconnect_attempts {
log::error!("Max reconnection attempts reached, giving up");
break;
}
attempts += 1;
match Self::stream_events(
&endpoint,
&config,
&queue_clone,
event_type_filter.as_ref(),
)
.await
{
Ok(_) => {
delay = config.reconnect_delay_ms;
attempts = 0;
}
Err(e) => {
log::error!("ShredStream error: {} - retry in {}ms", e, delay);
tokio::time::sleep(tokio::time::Duration::from_millis(delay)).await;
delay = (delay * 2).min(60_000);
}
}
}
});
*self.subscription_handle.lock().await = Some(handle);
Ok(queue)
}
pub async fn stop(&self) {
if let Some(handle) = self.subscription_handle.lock().await.take() {
handle.abort();
}
}
async fn connect_client(
endpoint: &str,
config: &ShredStreamConfig,
) -> crate::common::AnyResult<ShredstreamProxyClient<Channel>> {
let mut builder = Endpoint::from_shared(endpoint.to_string())?;
if config.connection_timeout_ms > 0 {
builder = builder.connect_timeout(Duration::from_millis(config.connection_timeout_ms));
}
let channel = builder.connect().await?;
Ok(ShredstreamProxyClient::new(channel)
.max_decoding_message_size(config.max_decoding_message_size))
}
async fn stream_events(
endpoint: &str,
config: &ShredStreamConfig,
queue: &Arc<ArrayQueue<DexEvent>>,
event_type_filter: Option<&EventTypeFilter>,
) -> Result<(), String> {
let mut client = Self::connect_client(endpoint, config).await.map_err(|e| e.to_string())?;
let request = tonic::Request::new(SubscribeEntriesRequest {});
let response = if config.request_timeout_ms > 0 {
tokio::time::timeout(
Duration::from_millis(config.request_timeout_ms),
client.subscribe_entries(request),
)
.await
.map_err(|_| {
format!(
"ShredStream subscribe request timed out after {}ms",
config.request_timeout_ms
)
})?
.map_err(|e| e.to_string())?
} else {
client.subscribe_entries(request).await.map_err(|e| e.to_string())?
};
let mut stream = response.into_inner();
log::info!("ShredStream connected, receiving entries...");
while let Some(message) = stream.next().await {
match message {
Ok(entry) => {
Self::process_entry(entry, queue, event_type_filter);
}
Err(e) => {
log::error!("Stream error: {:?}", e);
return Err(e.to_string());
}
}
}
Ok(())
}
#[inline]
fn process_entry(
entry: Entry,
queue: &Arc<ArrayQueue<DexEvent>>,
event_type_filter: Option<&EventTypeFilter>,
) {
let slot = entry.slot;
let recv_us = now_micros();
let entries = match bincode::deserialize::<Vec<SolanaEntry>>(&entry.entries) {
Ok(e) => e,
Err(e) => {
log::debug!("Failed to deserialize entries: {}", e);
return;
}
};
let mut events = Vec::with_capacity(4);
let mut tx_index = 0u64;
for entry in entries {
for transaction in entry.transactions.iter() {
events.clear();
Self::process_transaction(
transaction,
slot,
recv_us,
tx_index,
queue,
event_type_filter,
&mut events,
);
tx_index += 1;
}
}
}
#[inline]
fn process_transaction(
transaction: &solana_sdk::transaction::VersionedTransaction,
slot: u64,
recv_us: i64,
tx_index: u64,
queue: &Arc<ArrayQueue<DexEvent>>,
event_type_filter: Option<&EventTypeFilter>,
events: &mut Vec<DexEvent>,
) {
if transaction.signatures.is_empty() {
return;
}
let signature = transaction.signatures[0];
if let VersionedMessage::V0(m) = &transaction.message {
if !m.address_table_lookups.is_empty() {
log::debug!(
target: "sol_parser_sdk::shredstream",
"V0 tx uses address lookup tables; only static keys are available — \
some instruction account indices may resolve to wrong pubkeys (often only 1 BUY gets is_created_buy)"
);
}
}
super::pump_ix::parse_transaction_dex_events_with_filter(
transaction,
signature,
slot,
tx_index,
recv_us,
event_type_filter,
events,
);
crate::core::pumpfun_fee_enrich::enrich_pumpfun_same_tx_post_merge(events);
for mut event in events.drain(..) {
if let Some(meta) = event.metadata_mut() {
meta.grpc_recv_us = recv_us;
}
let _ = queue.push(event);
}
}
}