2021-06-05 17:08:33 +00:00
|
|
|
use crate::{JsonRpcClient, Middleware, Provider, TransactionStream};
|
2020-11-30 09:33:06 +00:00
|
|
|
|
2021-06-05 17:08:33 +00:00
|
|
|
use ethers_core::types::{TxHash, U256};
|
2020-11-30 09:33:06 +00:00
|
|
|
|
|
|
|
use futures_util::stream::Stream;
|
|
|
|
use pin_project::{pin_project, pinned_drop};
|
|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
use serde_json::Value;
|
|
|
|
use std::{
|
|
|
|
marker::PhantomData,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A transport implementation supporting pub sub subscriptions.
|
|
|
|
pub trait PubsubClient: JsonRpcClient {
|
|
|
|
/// The type of stream this transport returns
|
2021-08-23 14:55:36 +00:00
|
|
|
type NotificationStream: futures_core::Stream<Item = Value> + Send + Unpin;
|
2020-11-30 09:33:06 +00:00
|
|
|
|
|
|
|
/// Add a subscription to this transport
|
|
|
|
fn subscribe<T: Into<U256>>(&self, id: T) -> Result<Self::NotificationStream, Self::Error>;
|
|
|
|
|
|
|
|
/// Remove a subscription from this transport
|
|
|
|
fn unsubscribe<T: Into<U256>>(&self, id: T) -> Result<(), Self::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use = "subscriptions do nothing unless you stream them"]
|
|
|
|
#[pin_project(PinnedDrop)]
|
2020-12-31 19:08:12 +00:00
|
|
|
/// Streams data from an installed filter via `eth_subscribe`
|
2020-11-30 09:33:06 +00:00
|
|
|
pub struct SubscriptionStream<'a, P: PubsubClient, R: DeserializeOwned> {
|
|
|
|
/// The subscription's installed id on the ethereum node
|
|
|
|
pub id: U256,
|
|
|
|
|
|
|
|
provider: &'a Provider<P>,
|
|
|
|
|
|
|
|
#[pin]
|
|
|
|
rx: P::NotificationStream,
|
|
|
|
|
|
|
|
ret: PhantomData<R>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, P, R> SubscriptionStream<'a, P, R>
|
|
|
|
where
|
|
|
|
P: PubsubClient,
|
|
|
|
R: DeserializeOwned,
|
|
|
|
{
|
2021-08-20 16:58:13 +00:00
|
|
|
/// Creates a new subscription stream for the provided subscription id.
|
|
|
|
///
|
|
|
|
/// ### Note
|
|
|
|
/// Most providers treat `SubscriptionStream` IDs as global singletons.
|
2021-08-20 17:23:39 +00:00
|
|
|
/// Instantiating this directly with a known ID will likely cause any
|
2021-08-20 16:58:13 +00:00
|
|
|
/// existing streams with that ID to end. To avoid this, start a new stream
|
|
|
|
/// using [`Provider::subscribe`] instead of `SubscriptionStream::new`.
|
2020-11-30 09:33:06 +00:00
|
|
|
pub fn new(id: U256, provider: &'a Provider<P>) -> Result<Self, P::Error> {
|
|
|
|
// Call the underlying PubsubClient's subscribe
|
|
|
|
let rx = provider.as_ref().subscribe(id)?;
|
|
|
|
Ok(Self {
|
|
|
|
id,
|
|
|
|
provider,
|
|
|
|
rx,
|
|
|
|
ret: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-20 16:58:13 +00:00
|
|
|
/// Unsubscribes from the subscription.
|
2020-11-30 09:33:06 +00:00
|
|
|
pub async fn unsubscribe(&self) -> Result<bool, crate::ProviderError> {
|
|
|
|
self.provider.unsubscribe(self.id).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Each subscription item is a serde_json::Value which must be decoded to the
|
|
|
|
// subscription's return type.
|
|
|
|
// TODO: Can this be replaced with an `rx.map` in the constructor?
|
|
|
|
impl<'a, P, R> Stream for SubscriptionStream<'a, P, R>
|
|
|
|
where
|
|
|
|
P: PubsubClient,
|
|
|
|
R: DeserializeOwned,
|
|
|
|
{
|
|
|
|
type Item = R;
|
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Option<Self::Item>> {
|
|
|
|
let this = self.project();
|
|
|
|
match futures_util::ready!(this.rx.poll_next(ctx)) {
|
|
|
|
Some(item) => match serde_json::from_value(item) {
|
|
|
|
Ok(res) => Poll::Ready(Some(res)),
|
|
|
|
_ => Poll::Pending,
|
|
|
|
},
|
2021-08-20 16:58:13 +00:00
|
|
|
None => Poll::Ready(None),
|
2020-11-30 09:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pinned_drop]
|
2020-12-31 17:19:14 +00:00
|
|
|
impl<P, R> PinnedDrop for SubscriptionStream<'_, P, R>
|
2020-11-30 09:33:06 +00:00
|
|
|
where
|
|
|
|
P: PubsubClient,
|
|
|
|
R: DeserializeOwned,
|
|
|
|
{
|
|
|
|
fn drop(self: Pin<&mut Self>) {
|
|
|
|
// on drop it removes the handler from the websocket so that it stops
|
|
|
|
// getting populated. We need to call `unsubscribe` explicitly to cancel
|
|
|
|
// the subscription
|
|
|
|
let _ = (*self.provider).as_ref().unsubscribe(self.id);
|
|
|
|
}
|
|
|
|
}
|
2021-06-05 17:08:33 +00:00
|
|
|
|
|
|
|
impl<'a, P> SubscriptionStream<'a, P, TxHash>
|
|
|
|
where
|
|
|
|
P: PubsubClient,
|
|
|
|
{
|
|
|
|
/// Returns a stream that yields the `Transaction`s for the transaction hashes this stream yields.
|
|
|
|
///
|
|
|
|
/// This internally calls `Provider::get_transaction` with every new transaction.
|
|
|
|
/// No more than n futures will be buffered at any point in time, and less than n may also be
|
|
|
|
/// buffered depending on the state of each future.
|
|
|
|
pub fn transactions_unordered(self, n: usize) -> TransactionStream<'a, P, Self> {
|
|
|
|
TransactionStream::new(self.provider, self, n)
|
|
|
|
}
|
|
|
|
}
|