1use std::{future::Future, time::Duration};
2
3use futures::{SinkExt, StreamExt};
4use serde::{Deserialize, de::DeserializeOwned};
5use simulator_api::EncodedBinary;
6use solana_client::{
7 nonblocking::pubsub_client::PubsubClient,
8 rpc_response::{Response, RpcLogsResponse},
9};
10use solana_commitment_config::CommitmentConfig;
11use solana_rpc_client_api::config::{RpcTransactionLogsConfig, RpcTransactionLogsFilter};
12use thiserror::Error;
13use tokio::{
14 sync::{oneshot, watch},
15 task::JoinHandle,
16};
17use tokio_tungstenite::tungstenite::Message;
18
19use crate::urls::{UrlError, http_to_ws_url};
20
21#[derive(Debug, Error)]
23pub enum SubscriptionError {
24 #[error(transparent)]
25 InvalidUrl(#[from] UrlError),
26
27 #[error("pubsub connect to {url} failed: {source}")]
28 Connect {
29 url: String,
30 #[source]
31 source: Box<dyn std::error::Error + Send + Sync>,
32 },
33
34 #[error("logs_subscribe failed: {source}")]
35 Subscribe {
36 #[source]
37 source: Box<dyn std::error::Error + Send + Sync>,
38 },
39
40 #[error("subscription task exited unexpectedly before signaling ready")]
41 TaskDropped,
42
43 #[error("session has no rpc_endpoint (was the session created?)")]
44 NoRpcEndpoint,
45}
46
47#[derive(Debug, Error)]
48pub enum SubscriptionRuntimeError {
49 #[error("{kind} subscription for {target} closed unexpectedly")]
50 Closed { kind: &'static str, target: String },
51
52 #[error("{kind} subscription callback worker for {target} failed: {source}")]
53 CallbackWorker {
54 kind: &'static str,
55 target: String,
56 #[source]
57 source: tokio::task::JoinError,
58 },
59}
60
61const SUBSCRIPTION_DRAIN_IDLE_TIMEOUT: Duration = Duration::from_millis(250);
62const SUBSCRIPTION_DRAIN_MAX_DURATION: Duration = Duration::from_secs(5);
63
64type SubscriptionTaskHandle = JoinHandle<Result<(), SubscriptionRuntimeError>>;
65type AccountDiffWs =
66 tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>;
67
68pub struct SubscriptionHandle {
70 pub join_handle: SubscriptionTaskHandle,
71 pub stop: watch::Sender<bool>,
72}
73
74impl From<LogSubscriptionHandle> for SubscriptionHandle {
75 fn from(h: LogSubscriptionHandle) -> Self {
76 Self {
77 join_handle: h.join_handle,
78 stop: h.stop,
79 }
80 }
81}
82
83impl From<AccountDiffSubscriptionHandle> for SubscriptionHandle {
84 fn from(h: AccountDiffSubscriptionHandle) -> Self {
85 Self {
86 join_handle: h.join_handle,
87 stop: h.stop,
88 }
89 }
90}
91
92impl From<ActionSubscriptionHandle> for SubscriptionHandle {
93 fn from(h: ActionSubscriptionHandle) -> Self {
94 Self {
95 join_handle: h.join_handle,
96 stop: h.stop,
97 }
98 }
99}
100
101pub struct LogSubscriptionHandle {
103 pub join_handle: SubscriptionTaskHandle,
108
109 pub stop: watch::Sender<bool>,
112}
113
114pub async fn subscribe_program_logs<F, Fut>(
152 rpc_endpoint: &str,
153 program_id: &str,
154 commitment: CommitmentConfig,
155 on_notification: F,
156) -> Result<LogSubscriptionHandle, SubscriptionError>
157where
158 F: Fn(Response<RpcLogsResponse>) -> Fut + Send + Sync + 'static,
159 Fut: Future<Output = ()> + Send + 'static,
160{
161 let ws_url = http_to_ws_url(rpc_endpoint)?;
162 let program_id = program_id.to_string();
163
164 let (ready_tx, ready_rx) = oneshot::channel::<Result<(), SubscriptionError>>();
165 let (stop_tx, mut stop_rx) = watch::channel(false);
166
167 let join_handle = tokio::spawn(async move {
171 let client = match PubsubClient::new(&ws_url).await {
172 Ok(c) => c,
173 Err(e) => {
174 let _ = ready_tx.send(Err(SubscriptionError::Connect {
175 url: ws_url,
176 source: Box::new(e),
177 }));
178 return Ok(());
179 }
180 };
181
182 let (mut stream, _unsubscribe) = match client
183 .logs_subscribe(
184 RpcTransactionLogsFilter::Mentions(vec![program_id.clone()]),
185 RpcTransactionLogsConfig {
186 commitment: Some(commitment),
187 },
188 )
189 .await
190 {
191 Ok(s) => s,
192 Err(e) => {
193 let _ = ready_tx.send(Err(SubscriptionError::Subscribe {
194 source: Box::new(e),
195 }));
196 return Ok(());
197 }
198 };
199
200 let _ = ready_tx.send(Ok(()));
201
202 let mut tasks: Vec<JoinHandle<()>> = Vec::new();
203 let kind = "program logs";
204
205 loop {
206 if *stop_rx.borrow() {
207 let drain_deadline = tokio::time::Instant::now() + SUBSCRIPTION_DRAIN_MAX_DURATION;
208 while let Ok(Ok(Some(notification))) = tokio::time::timeout_at(
209 drain_deadline,
210 tokio::time::timeout(SUBSCRIPTION_DRAIN_IDLE_TIMEOUT, stream.next()),
211 )
212 .await
213 {
214 tasks.push(tokio::spawn(on_notification(notification)));
215 }
216 break;
217 }
218
219 let notification = tokio::select! {
220 n = stream.next() => n,
221 _ = stop_rx.changed() => continue,
222 };
223
224 match notification {
225 Some(n) => tasks.push(tokio::spawn(on_notification(n))),
226 None => return Err(subscription_runtime_closed(kind, &program_id)),
227 }
228 }
229
230 for task in tasks {
232 if let Err(source) = task.await {
233 return Err(callback_worker_failed(kind, &program_id, source));
234 }
235 }
236
237 Ok(())
238 });
239
240 match ready_rx.await {
241 Ok(Ok(())) => Ok(LogSubscriptionHandle {
242 join_handle,
243 stop: stop_tx,
244 }),
245 Ok(Err(e)) => {
246 join_handle.abort();
247 Err(e)
248 }
249 Err(_) => {
250 join_handle.abort();
251 Err(SubscriptionError::TaskDropped)
252 }
253 }
254}
255
256#[derive(Debug, Clone, Deserialize)]
260pub struct AccountDiffContext {
261 pub slot: u64,
262}
263
264#[derive(Debug, Clone, Deserialize)]
266pub struct AccountDiffNotification {
267 pub context: AccountDiffContext,
268 pub account: Option<String>,
270 pub signature: Option<String>,
272 #[serde(default)]
274 pub tx_index: Option<u32>,
275 #[serde(default)]
277 pub block_time: Option<i64>,
278 pub pre: Option<serde_json::Value>,
280 pub post: Option<serde_json::Value>,
282}
283
284#[derive(Debug, Clone, Deserialize)]
285pub struct ActionResultContext {
286 pub slot: u64,
287}
288
289#[derive(Debug, Clone, Deserialize)]
291#[serde(rename_all = "camelCase")]
292pub struct ActionResultNotification {
293 pub context: ActionResultContext,
294 pub slot: u64,
295 #[serde(default)]
297 pub batch_index: Option<u32>,
298 pub action_index: u32,
300 #[serde(default)]
301 pub label: Option<String>,
302 pub committed: bool,
303 #[serde(default)]
306 pub transaction_outcomes: Vec<ActionTransactionOutcome>,
307 #[serde(default)]
310 pub accounts: Vec<Option<serde_json::Value>>,
311 #[serde(default)]
315 pub matched: Option<EncodedBinary>,
316}
317
318#[derive(Debug, Clone, Deserialize)]
320#[serde(rename_all = "camelCase")]
321pub struct ActionTransactionOutcome {
322 #[serde(default)]
324 pub err: Option<String>,
325 #[serde(default)]
326 pub logs: Vec<String>,
327 pub units_consumed: u64,
328 #[serde(default)]
329 pub fee: Option<u64>,
330 #[serde(default)]
332 pub return_data: Option<serde_json::Value>,
333}
334
335#[derive(Debug, Clone)]
337pub struct RoutedAccountDiffNotification {
338 pub account: String,
339 pub notification: AccountDiffNotification,
340}
341
342pub struct AccountDiffSubscriptionHandle {
346 pub join_handle: SubscriptionTaskHandle,
347 pub stop: watch::Sender<bool>,
348}
349
350fn subscription_runtime_closed(
351 kind: &'static str,
352 target: impl Into<String>,
353) -> SubscriptionRuntimeError {
354 SubscriptionRuntimeError::Closed {
355 kind,
356 target: target.into(),
357 }
358}
359
360fn callback_worker_failed(
361 kind: &'static str,
362 target: impl Into<String>,
363 source: tokio::task::JoinError,
364) -> SubscriptionRuntimeError {
365 SubscriptionRuntimeError::CallbackWorker {
366 kind,
367 target: target.into(),
368 source,
369 }
370}
371
372pub async fn subscribe_account_diffs<F, Fut>(
402 rpc_endpoint: &str,
403 account: &str,
404 on_notification: F,
405) -> Result<AccountDiffSubscriptionHandle, SubscriptionError>
406where
407 F: Fn(AccountDiffNotification) -> Fut + Send + Sync + 'static,
408 Fut: Future<Output = ()> + Send + 'static,
409{
410 subscribe_account_diffs_many(rpc_endpoint, [account.to_string()], move |notification| {
411 on_notification(notification.notification)
412 })
413 .await
414}
415
416pub async fn subscribe_account_diffs_many<F, Fut, I, S>(
422 rpc_endpoint: &str,
423 accounts: I,
424 on_notification: F,
425) -> Result<AccountDiffSubscriptionHandle, SubscriptionError>
426where
427 F: Fn(RoutedAccountDiffNotification) -> Fut + Send + Sync + 'static,
428 Fut: Future<Output = ()> + Send + 'static,
429 I: IntoIterator<Item = S>,
430 S: Into<String>,
431{
432 let ws_url = http_to_ws_url(rpc_endpoint)?;
433 let accounts = dedup_accounts(accounts);
434 if accounts.is_empty() {
435 let (stop_tx, stop_rx) = watch::channel(false);
436 return Ok(AccountDiffSubscriptionHandle {
437 join_handle: tokio::spawn(async move {
438 let _ = stop_rx;
439 Ok(())
440 }),
441 stop: stop_tx,
442 });
443 }
444
445 let (ready_tx, ready_rx) = oneshot::channel::<Result<(), SubscriptionError>>();
446 let (stop_tx, mut stop_rx) = watch::channel(false);
447 let target = format!("{} accounts", accounts.len());
448
449 let join_handle = tokio::spawn(async move {
450 let (notification_tx, mut notification_rx) = tokio::sync::mpsc::unbounded_channel();
451 let callback_handle = tokio::spawn(async move {
452 while let Some(notification) = notification_rx.recv().await {
453 on_notification(notification).await;
454 }
455 });
456
457 let (mut ws, _) = match tokio_tungstenite::connect_async(&ws_url).await {
458 Ok(connection) => connection,
459 Err(e) => {
460 let _ = ready_tx.send(Err(SubscriptionError::Connect {
461 url: ws_url,
462 source: Box::new(e),
463 }));
464 return Ok(());
465 }
466 };
467
468 let subscriptions =
469 match send_account_diff_subscribe_many(&mut ws, &accounts, ¬ification_tx).await {
470 Ok(subscriptions) => subscriptions,
471 Err(error) => {
472 let _ = ready_tx.send(Err(error));
473 return Ok(());
474 }
475 };
476
477 let _ = ready_tx.send(Ok(()));
478
479 if let Err(error) =
480 drive_account_diff_stream_many(&mut ws, &subscriptions, ¬ification_tx, &mut stop_rx)
481 .await
482 {
483 drop(notification_tx);
484 if let Err(source) = callback_handle.await {
485 return Err(callback_worker_failed("account diff", target, source));
486 }
487 return Err(error);
488 }
489
490 drop(notification_tx);
491 if let Err(source) = callback_handle.await {
492 return Err(callback_worker_failed("account diff", target, source));
493 }
494
495 Ok(())
496 });
497
498 match ready_rx.await {
499 Ok(Ok(())) => Ok(AccountDiffSubscriptionHandle {
500 join_handle,
501 stop: stop_tx,
502 }),
503 Ok(Err(e)) => {
504 join_handle.abort();
505 Err(e)
506 }
507 Err(_) => {
508 join_handle.abort();
509 Err(SubscriptionError::TaskDropped)
510 }
511 }
512}
513
514#[derive(Deserialize)]
515struct AccountDiffMessage {
516 method: String,
517 params: AccountDiffParams,
518}
519
520#[derive(Deserialize)]
521struct AccountDiffParams {
522 subscription: u64,
523 result: AccountDiffNotification,
524}
525
526async fn send_account_diff_subscribe_many(
527 ws: &mut AccountDiffWs,
528 accounts: &[String],
529 notification_tx: &tokio::sync::mpsc::UnboundedSender<RoutedAccountDiffNotification>,
530) -> Result<std::collections::HashMap<u64, String>, SubscriptionError> {
531 #[derive(Deserialize)]
532 struct SubscriptionConfirmation {
533 id: u64,
534 result: Option<u64>,
535 }
536
537 let mut pending: std::collections::HashMap<u64, String> = std::collections::HashMap::new();
538 let mut subscriptions = std::collections::HashMap::with_capacity(accounts.len());
539
540 for (index, account) in accounts.iter().enumerate() {
541 let request_id = (index + 1) as u64;
542 let req = serde_json::json!({
543 "jsonrpc": "2.0",
544 "id": request_id,
545 "method": "accountDiffSubscribe",
546 "params": [account]
547 });
548 ws.send(Message::Text(req.to_string()))
549 .await
550 .map_err(|source| SubscriptionError::Subscribe {
551 source: Box::new(source),
552 })?;
553 pending.insert(request_id, account.clone());
554 }
555
556 while !pending.is_empty() {
557 match ws.next().await {
558 Some(Ok(Message::Text(text))) => {
559 if let Ok(confirmation) = serde_json::from_str::<SubscriptionConfirmation>(&text) {
560 let Some(account) = pending.remove(&confirmation.id) else {
561 continue;
562 };
563 let Some(subscription_id) = confirmation.result else {
564 return Err(SubscriptionError::TaskDropped);
565 };
566 subscriptions.insert(subscription_id, account);
567 continue;
568 }
569
570 if let Some(notification) =
571 parse_routed_account_diff_notification(&text, &subscriptions)
572 {
573 let _ = notification_tx.send(notification);
574 }
575 }
576 Some(Ok(_)) => {}
577 _ => return Err(SubscriptionError::TaskDropped),
578 }
579 }
580
581 Ok(subscriptions)
582}
583
584async fn drive_account_diff_stream_many(
585 ws: &mut AccountDiffWs,
586 subscriptions: &std::collections::HashMap<u64, String>,
587 notification_tx: &tokio::sync::mpsc::UnboundedSender<RoutedAccountDiffNotification>,
588 stop_rx: &mut watch::Receiver<bool>,
589) -> Result<(), SubscriptionRuntimeError> {
590 loop {
591 if *stop_rx.borrow() {
592 let drain_deadline = tokio::time::Instant::now() + SUBSCRIPTION_DRAIN_MAX_DURATION;
593 loop {
594 match tokio::time::timeout_at(
595 drain_deadline,
596 tokio::time::timeout(SUBSCRIPTION_DRAIN_IDLE_TIMEOUT, ws.next()),
597 )
598 .await
599 {
600 Ok(Ok(Some(Ok(Message::Text(text))))) => {
601 if let Some(notification) =
602 parse_routed_account_diff_notification(&text, subscriptions)
603 {
604 let _ = notification_tx.send(notification);
605 }
606 }
607 _ => return Ok(()),
608 }
609 }
610 }
611
612 let msg = tokio::select! {
613 m = ws.next() => m,
614 _ = stop_rx.changed() => continue,
615 };
616
617 match msg {
618 Some(Ok(Message::Text(text))) => {
619 if let Some(notification) =
620 parse_routed_account_diff_notification(&text, subscriptions)
621 {
622 let _ = notification_tx.send(notification);
623 }
624 }
625 Some(Ok(_)) => {}
626 _ => {
627 return Err(subscription_runtime_closed(
628 "account diff",
629 format!("{} accounts", subscriptions.len()),
630 ));
631 }
632 }
633 }
634}
635
636fn parse_account_diff_message(text: &str) -> Option<AccountDiffMessage> {
637 let msg: AccountDiffMessage = serde_json::from_str(text).ok()?;
638 (msg.method == "accountDiffNotification").then_some(msg)
639}
640
641fn parse_routed_account_diff_notification(
642 text: &str,
643 subscriptions: &std::collections::HashMap<u64, String>,
644) -> Option<RoutedAccountDiffNotification> {
645 let msg = parse_account_diff_message(text)?;
646 let account = subscriptions.get(&msg.params.subscription)?.clone();
647 Some(RoutedAccountDiffNotification {
648 account,
649 notification: msg.params.result,
650 })
651}
652
653fn dedup_accounts<I, S>(accounts: I) -> Vec<String>
654where
655 I: IntoIterator<Item = S>,
656 S: Into<String>,
657{
658 let mut unique = std::collections::BTreeSet::new();
659 accounts
660 .into_iter()
661 .map(Into::into)
662 .filter(|account| unique.insert(account.clone()))
663 .collect()
664}
665
666pub async fn subscribe_program_diffs<F, Fut>(
699 rpc_endpoint: &str,
700 program_id: &str,
701 on_notification: F,
702) -> Result<AccountDiffSubscriptionHandle, SubscriptionError>
703where
704 F: Fn(AccountDiffNotification) -> Fut + Send + Sync + 'static,
705 Fut: Future<Output = ()> + Send + 'static,
706{
707 let ws_url = http_to_ws_url(rpc_endpoint)?;
708 let program_id = program_id.to_string();
709
710 let (ready_tx, ready_rx) = oneshot::channel::<Result<(), SubscriptionError>>();
711 let (stop_tx, mut stop_rx) = watch::channel(false);
712
713 let join_handle = tokio::spawn(async move {
714 let (notification_tx, mut notification_rx) = tokio::sync::mpsc::unbounded_channel();
715 let callback_handle = tokio::spawn(async move {
716 while let Some(notification) = notification_rx.recv().await {
717 on_notification(notification).await;
718 }
719 });
720
721 let (mut ws, _) = match tokio_tungstenite::connect_async(&ws_url).await {
722 Ok(connection) => connection,
723 Err(e) => {
724 let _ = ready_tx.send(Err(SubscriptionError::Connect {
725 url: ws_url,
726 source: Box::new(e),
727 }));
728 return Ok(());
729 }
730 };
731
732 if let Err(error) = send_program_diff_subscribe(&mut ws, &program_id).await {
733 let _ = ready_tx.send(Err(error));
734 return Ok(());
735 }
736
737 let _ = ready_tx.send(Ok(()));
738
739 if let Err(error) =
740 drive_program_diff_stream(&mut ws, ¬ification_tx, &mut stop_rx, &program_id).await
741 {
742 drop(notification_tx);
743 if let Err(source) = callback_handle.await {
744 return Err(callback_worker_failed(
745 "program account diff",
746 &program_id,
747 source,
748 ));
749 }
750 return Err(error);
751 }
752
753 drop(notification_tx);
754 if let Err(source) = callback_handle.await {
755 return Err(callback_worker_failed(
756 "program account diff",
757 &program_id,
758 source,
759 ));
760 }
761
762 Ok(())
763 });
764
765 match ready_rx.await {
766 Ok(Ok(())) => Ok(AccountDiffSubscriptionHandle {
767 join_handle,
768 stop: stop_tx,
769 }),
770 Ok(Err(e)) => {
771 join_handle.abort();
772 Err(e)
773 }
774 Err(_) => {
775 join_handle.abort();
776 Err(SubscriptionError::TaskDropped)
777 }
778 }
779}
780
781async fn send_program_diff_subscribe(
782 ws: &mut AccountDiffWs,
783 program_id: &str,
784) -> Result<(), SubscriptionError> {
785 #[derive(Deserialize)]
786 struct SubscriptionConfirmation {
787 result: Option<u64>,
788 }
789
790 let req = serde_json::json!({
791 "jsonrpc": "2.0",
792 "id": 1,
793 "method": "accountDiffSubscribe",
794 "params": [program_id, {"address_type": "program"}]
795 });
796 ws.send(Message::Text(req.to_string()))
797 .await
798 .map_err(|source| SubscriptionError::Subscribe {
799 source: Box::new(source),
800 })?;
801
802 loop {
803 match ws.next().await {
804 Some(Ok(Message::Text(text))) => {
805 match serde_json::from_str::<SubscriptionConfirmation>(&text) {
806 Ok(SubscriptionConfirmation { result: Some(_) }) => return Ok(()),
807 Ok(_) => continue,
808 Err(source) => {
809 return Err(SubscriptionError::Subscribe {
810 source: Box::new(source),
811 });
812 }
813 }
814 }
815 Some(Ok(_)) => continue,
816 _ => return Err(SubscriptionError::TaskDropped),
817 }
818 }
819}
820
821async fn drive_program_diff_stream(
822 ws: &mut AccountDiffWs,
823 notification_tx: &tokio::sync::mpsc::UnboundedSender<AccountDiffNotification>,
824 stop_rx: &mut watch::Receiver<bool>,
825 program_id: &str,
826) -> Result<(), SubscriptionRuntimeError> {
827 loop {
828 if *stop_rx.borrow() {
829 let drain_deadline = tokio::time::Instant::now() + SUBSCRIPTION_DRAIN_MAX_DURATION;
830 loop {
831 match tokio::time::timeout_at(
832 drain_deadline,
833 tokio::time::timeout(SUBSCRIPTION_DRAIN_IDLE_TIMEOUT, ws.next()),
834 )
835 .await
836 {
837 Ok(Ok(Some(Ok(Message::Text(text))))) => {
838 if let Some(msg) = parse_account_diff_message(&text) {
839 let _ = notification_tx.send(msg.params.result);
840 }
841 }
842 _ => return Ok(()),
843 }
844 }
845 }
846
847 let msg = tokio::select! {
848 m = ws.next() => m,
849 _ = stop_rx.changed() => continue,
850 };
851
852 match msg {
853 Some(Ok(Message::Text(text))) => {
854 if let Some(msg) = parse_account_diff_message(&text) {
855 let _ = notification_tx.send(msg.params.result);
856 }
857 }
858 Some(Ok(_)) => {}
859 _ => {
860 return Err(subscription_runtime_closed(
861 "program account diff",
862 program_id,
863 ));
864 }
865 }
866 }
867}
868
869#[derive(Clone, Copy)]
873struct NotificationStream {
874 subscribe_method: &'static str,
875 notification_method: &'static str,
876 kind: &'static str,
878 target: &'static str,
880}
881
882const ACTION_STREAM: NotificationStream = NotificationStream {
883 subscribe_method: "actionSubscribe",
884 notification_method: "actionNotification",
885 kind: "action",
886 target: "actions",
887};
888
889const REROUTE_STREAM: NotificationStream = NotificationStream {
890 subscribe_method: "rerouteSubscribe",
891 notification_method: "rerouteNotification",
892 kind: "reroute",
893 target: "reroutes",
894};
895
896#[derive(Deserialize)]
897struct NotificationMessage<N> {
898 method: String,
899 params: NotificationParams<N>,
900}
901
902#[derive(Deserialize)]
903struct NotificationParams<N> {
904 #[allow(dead_code)]
905 subscription: u64,
906 result: N,
907}
908
909fn parse_notification_message<N: DeserializeOwned>(
910 text: &str,
911 notification_method: &str,
912) -> Option<N> {
913 let msg: NotificationMessage<N> = serde_json::from_str(text).ok()?;
914 (msg.method == notification_method).then_some(msg.params.result)
915}
916
917fn is_subscription_complete_message(text: &str) -> bool {
918 #[derive(Deserialize)]
919 struct SubscriptionCompleteMessage {
920 method: String,
921 }
922
923 serde_json::from_str::<SubscriptionCompleteMessage>(text)
924 .is_ok_and(|msg| msg.method == "subscriptionComplete")
925}
926
927async fn subscribe_notifications<N, F, Fut>(
932 rpc_endpoint: &str,
933 stream: NotificationStream,
934 on_notification: F,
935) -> Result<SubscriptionHandle, SubscriptionError>
936where
937 N: DeserializeOwned + Send + 'static,
938 F: Fn(N) -> Fut + Send + Sync + 'static,
939 Fut: Future<Output = ()> + Send + 'static,
940{
941 let ws_url = http_to_ws_url(rpc_endpoint)?;
942
943 let (ready_tx, ready_rx) = oneshot::channel::<Result<(), SubscriptionError>>();
944 let (stop_tx, mut stop_rx) = watch::channel(false);
945
946 let join_handle = tokio::spawn(async move {
947 let (notification_tx, mut notification_rx) = tokio::sync::mpsc::unbounded_channel();
948 let callback_handle = tokio::spawn(async move {
949 while let Some(notification) = notification_rx.recv().await {
950 on_notification(notification).await;
951 }
952 });
953
954 let (mut ws, _) = match tokio_tungstenite::connect_async(&ws_url).await {
955 Ok(connection) => connection,
956 Err(e) => {
957 let _ = ready_tx.send(Err(SubscriptionError::Connect {
958 url: ws_url,
959 source: Box::new(e),
960 }));
961 return Ok(());
962 }
963 };
964
965 if let Err(error) = send_stream_subscribe(&mut ws, stream.subscribe_method).await {
966 let _ = ready_tx.send(Err(error));
967 return Ok(());
968 }
969
970 let _ = ready_tx.send(Ok(()));
971
972 if let Err(error) =
973 drive_notification_stream::<N>(&mut ws, stream, ¬ification_tx, &mut stop_rx).await
974 {
975 drop(notification_tx);
976 if let Err(source) = callback_handle.await {
977 return Err(callback_worker_failed(stream.kind, stream.target, source));
978 }
979 return Err(error);
980 }
981
982 drop(notification_tx);
983 if let Err(source) = callback_handle.await {
984 return Err(callback_worker_failed(stream.kind, stream.target, source));
985 }
986
987 Ok(())
988 });
989
990 match ready_rx.await {
991 Ok(Ok(())) => Ok(SubscriptionHandle {
992 join_handle,
993 stop: stop_tx,
994 }),
995 Ok(Err(e)) => {
996 join_handle.abort();
997 Err(e)
998 }
999 Err(_) => {
1000 join_handle.abort();
1001 Err(SubscriptionError::TaskDropped)
1002 }
1003 }
1004}
1005
1006async fn send_stream_subscribe(
1007 ws: &mut AccountDiffWs,
1008 subscribe_method: &str,
1009) -> Result<(), SubscriptionError> {
1010 #[derive(Deserialize)]
1011 struct SubscriptionConfirmation {
1012 result: Option<u64>,
1013 }
1014
1015 let req = serde_json::json!({
1017 "jsonrpc": "2.0",
1018 "id": 1,
1019 "method": subscribe_method,
1020 "params": [serde_json::Value::Null, {}]
1021 });
1022
1023 ws.send(Message::Text(req.to_string()))
1024 .await
1025 .map_err(|source| SubscriptionError::Subscribe {
1026 source: Box::new(source),
1027 })?;
1028
1029 loop {
1030 match ws.next().await {
1031 Some(Ok(Message::Text(text))) => {
1032 match serde_json::from_str::<SubscriptionConfirmation>(&text) {
1033 Ok(SubscriptionConfirmation { result: Some(_) }) => return Ok(()),
1034 Ok(_) => continue,
1035 Err(source) => {
1036 return Err(SubscriptionError::Subscribe {
1037 source: Box::new(source),
1038 });
1039 }
1040 }
1041 }
1042 Some(Ok(_)) => continue,
1043 _ => return Err(SubscriptionError::TaskDropped),
1044 }
1045 }
1046}
1047
1048async fn drive_notification_stream<N: DeserializeOwned>(
1049 ws: &mut AccountDiffWs,
1050 stream: NotificationStream,
1051 notification_tx: &tokio::sync::mpsc::UnboundedSender<N>,
1052 stop_rx: &mut watch::Receiver<bool>,
1053) -> Result<(), SubscriptionRuntimeError> {
1054 loop {
1055 if *stop_rx.borrow() {
1056 let drain_deadline = tokio::time::Instant::now() + SUBSCRIPTION_DRAIN_MAX_DURATION;
1057 loop {
1058 match tokio::time::timeout_at(
1059 drain_deadline,
1060 tokio::time::timeout(SUBSCRIPTION_DRAIN_IDLE_TIMEOUT, ws.next()),
1061 )
1062 .await
1063 {
1064 Ok(Ok(Some(Ok(Message::Text(text))))) => {
1065 if is_subscription_complete_message(&text) {
1066 return Ok(());
1067 }
1068 if let Some(result) =
1069 parse_notification_message::<N>(&text, stream.notification_method)
1070 {
1071 let _ = notification_tx.send(result);
1072 }
1073 }
1074 _ => return Ok(()),
1075 }
1076 }
1077 }
1078
1079 let msg = tokio::select! {
1080 m = ws.next() => m,
1081 _ = stop_rx.changed() => continue,
1082 };
1083
1084 match msg {
1085 Some(Ok(Message::Text(text))) => {
1086 if is_subscription_complete_message(&text) {
1087 return Ok(());
1088 }
1089 if let Some(result) =
1090 parse_notification_message::<N>(&text, stream.notification_method)
1091 {
1092 let _ = notification_tx.send(result);
1093 }
1094 }
1095 Some(Ok(_)) => {}
1096 _ => return Err(subscription_runtime_closed(stream.kind, stream.target)),
1097 }
1098 }
1099}
1100
1101pub struct ActionSubscriptionHandle {
1107 pub join_handle: SubscriptionTaskHandle,
1108 pub stop: watch::Sender<bool>,
1109}
1110
1111pub async fn subscribe_actions<F, Fut>(
1141 rpc_endpoint: &str,
1142 on_notification: F,
1143) -> Result<ActionSubscriptionHandle, SubscriptionError>
1144where
1145 F: Fn(ActionResultNotification) -> Fut + Send + Sync + 'static,
1146 Fut: Future<Output = ()> + Send + 'static,
1147{
1148 let SubscriptionHandle { join_handle, stop } =
1149 subscribe_notifications(rpc_endpoint, ACTION_STREAM, on_notification).await?;
1150 Ok(ActionSubscriptionHandle { join_handle, stop })
1151}
1152
1153#[derive(Debug, Clone, Deserialize)]
1156pub struct RerouteContext {
1157 pub slot: u64,
1158}
1159
1160#[derive(Debug, Clone, Deserialize)]
1162#[serde(rename_all = "camelCase")]
1163pub struct RerouteLegNotification {
1164 pub input_mint: String,
1165 pub output_mint: String,
1166 pub amount: u64,
1167 pub swap_mode: String,
1168 pub original_quoted_out: u64,
1169 pub metis_quoted_out: u64,
1170 pub route_summary: String,
1171 #[serde(default)]
1174 pub route_plan: Option<String>,
1175}
1176
1177#[derive(Debug, Clone, Deserialize)]
1180#[serde(rename_all = "camelCase")]
1181pub struct RerouteNotification {
1182 pub context: RerouteContext,
1183 pub slot: u64,
1184 pub batch_index: u32,
1186 pub original_signature: String,
1188 #[serde(default)]
1190 pub legs: Vec<RerouteLegNotification>,
1191 pub routed_transaction: EncodedBinary,
1194 #[serde(default)]
1196 pub err: Option<String>,
1197 #[serde(default)]
1198 pub logs: Vec<String>,
1199 pub compute_units_consumed: u64,
1200 #[serde(default)]
1201 pub fee: Option<u64>,
1202 #[serde(default)]
1205 pub realized_output_amount: Option<u64>,
1206 #[serde(default)]
1210 pub original_realized_output_amount: Option<u64>,
1211}
1212
1213pub async fn subscribe_reroutes<F, Fut>(
1242 rpc_endpoint: &str,
1243 on_notification: F,
1244) -> Result<SubscriptionHandle, SubscriptionError>
1245where
1246 F: Fn(RerouteNotification) -> Fut + Send + Sync + 'static,
1247 Fut: Future<Output = ()> + Send + 'static,
1248{
1249 subscribe_notifications(rpc_endpoint, REROUTE_STREAM, on_notification).await
1250}
1251
1252#[cfg(test)]
1253mod tests {
1254 use super::*;
1255
1256 #[test]
1257 fn parse_account_diff_notification_ignores_other_messages() {
1258 let confirmation = r#"{"jsonrpc":"2.0","result":1,"id":1}"#;
1259 assert!(parse_account_diff_message(confirmation).is_none());
1260 }
1261
1262 #[test]
1263 fn parse_account_diff_notification_extracts_payload() {
1264 let text = r#"{
1265 "jsonrpc":"2.0",
1266 "method":"accountDiffNotification",
1267 "params":{
1268 "subscription":7,
1269 "result":{
1270 "context":{"slot":123},
1271 "signature":"sig",
1272 "pre":{"a":1},
1273 "post":{"a":2}
1274 }
1275 }
1276 }"#;
1277
1278 let notification = parse_account_diff_message(text)
1279 .expect("notification")
1280 .params
1281 .result;
1282 assert_eq!(notification.context.slot, 123);
1283 assert_eq!(notification.signature.as_deref(), Some("sig"));
1284 assert_eq!(notification.pre, Some(serde_json::json!({"a": 1})));
1285 assert_eq!(notification.post, Some(serde_json::json!({"a": 2})));
1286 }
1287
1288 #[test]
1289 fn parse_routed_account_diff_notification_extracts_subscription_account() {
1290 let text = r#"{
1291 "jsonrpc":"2.0",
1292 "method":"accountDiffNotification",
1293 "params":{
1294 "subscription":42,
1295 "result":{
1296 "context":{"slot":456},
1297 "signature":"sig",
1298 "pre":null,
1299 "post":{"a":2}
1300 }
1301 }
1302 }"#;
1303 let subscriptions = std::collections::HashMap::from([(42_u64, "acct".to_string())]);
1304
1305 let notification =
1306 parse_routed_account_diff_notification(text, &subscriptions).expect("notification");
1307 assert_eq!(notification.account, "acct");
1308 assert_eq!(notification.notification.context.slot, 456);
1309 }
1310
1311 #[test]
1312 fn dedup_accounts_preserves_first_seen_order() {
1313 let accounts = dedup_accounts([
1314 "b".to_string(),
1315 "a".to_string(),
1316 "b".to_string(),
1317 "c".to_string(),
1318 ]);
1319 assert_eq!(accounts, vec!["b", "a", "c"]);
1320 }
1321}