|
| 1 | +use crate::{Provider, RootProvider}; |
| 2 | +use alloy_json_rpc::{RpcRecv, RpcSend}; |
| 3 | +use alloy_network::{Ethereum, Network}; |
| 4 | +use alloy_transport::TransportResult; |
| 5 | +use std::{borrow::Cow, future::Future, pin::Pin}; |
| 6 | + |
| 7 | +/// A general-purpose subscription request builder |
| 8 | +/// |
| 9 | +/// This struct allows configuring subscription parameters and channel size |
| 10 | +/// before initiating a request to subscribe to Ethereum events. |
| 11 | +pub struct GetSubscription<P, R, N = Ethereum> |
| 12 | +where |
| 13 | + P: RpcSend, |
| 14 | + R: RpcRecv, |
| 15 | + N: Network, |
| 16 | +{ |
| 17 | + root: RootProvider<N>, |
| 18 | + method: Cow<'static, str>, |
| 19 | + params: Option<P>, |
| 20 | + channel_size: Option<usize>, |
| 21 | + _marker: std::marker::PhantomData<fn() -> R>, |
| 22 | +} |
| 23 | + |
| 24 | +impl<P, R, N> GetSubscription<P, R, N> |
| 25 | +where |
| 26 | + N: Network, |
| 27 | + P: RpcSend, |
| 28 | + R: RpcRecv, |
| 29 | +{ |
| 30 | + /// Creates a new [`GetSubscription`] instance |
| 31 | + pub fn new( |
| 32 | + root: RootProvider<N>, |
| 33 | + method: impl Into<Cow<'static, str>>, |
| 34 | + params: Option<P>, |
| 35 | + ) -> Self { |
| 36 | + Self { |
| 37 | + root, |
| 38 | + method: method.into(), |
| 39 | + channel_size: None, |
| 40 | + params, |
| 41 | + _marker: std::marker::PhantomData, |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /// Set the channel_size for the subscription stream. |
| 46 | + pub fn channel_size(mut self, size: usize) -> Self { |
| 47 | + self.channel_size = Some(size); |
| 48 | + self |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl<P, R, N> core::fmt::Debug for GetSubscription<P, R, N> |
| 53 | +where |
| 54 | + N: Network, |
| 55 | + P: RpcSend, |
| 56 | + R: RpcRecv, |
| 57 | +{ |
| 58 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 59 | + f.debug_struct("GetSubscription") |
| 60 | + .field("channel_size", &self.channel_size) |
| 61 | + .field("method", &self.method) |
| 62 | + .finish() |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg(feature = "pubsub")] |
| 67 | +impl<P, R, N> std::future::IntoFuture for GetSubscription<P, R, N> |
| 68 | +where |
| 69 | + N: Network, |
| 70 | + P: RpcSend + 'static, |
| 71 | + R: RpcRecv, |
| 72 | +{ |
| 73 | + type Output = TransportResult<alloy_pubsub::Subscription<R>>; |
| 74 | + type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'static>>; |
| 75 | + |
| 76 | + fn into_future(self) -> Self::IntoFuture { |
| 77 | + Box::pin(async move { |
| 78 | + let pubsub = self.root.pubsub_frontend()?; |
| 79 | + |
| 80 | + // Set config channel size if any |
| 81 | + if let Some(size) = self.channel_size { |
| 82 | + pubsub.set_channel_size(size); |
| 83 | + } |
| 84 | + |
| 85 | + // Handle params and no-params case separately |
| 86 | + let id = if let Some(params) = self.params { |
| 87 | + let mut call = self.root.client().request(self.method, params); |
| 88 | + call.set_is_subscription(); |
| 89 | + call.await? |
| 90 | + } else { |
| 91 | + let mut call = self.root.client().request_noparams(self.method); |
| 92 | + call.set_is_subscription(); |
| 93 | + call.await? |
| 94 | + }; |
| 95 | + |
| 96 | + self.root.get_subscription(id).await |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments