use crate::ProviderError; use ethers_core::types::U256; use futures_timer::Delay; use futures_util::{stream, FutureExt, StreamExt}; use std::{future::Future, pin::Pin}; /// A simple gas escalation policy pub type EscalationPolicy = Box U256 + Send + Sync>; // Helper type alias #[cfg(target_arch = "wasm32")] pub(crate) type PinBoxFut<'a, T> = Pin> + 'a>>; #[cfg(not(target_arch = "wasm32"))] pub(crate) type PinBoxFut<'a, T> = Pin> + Send + 'a>>; /// Calls the future if `item` is None, otherwise returns a `futures::ok` pub async fn maybe(item: Option, f: F) -> Result where F: Future>, { if let Some(item) = item { futures_util::future::ok(item).await } else { f.await } } // https://github.com/tomusdrw/rust-web3/blob/befcb2fb8f3ca0a43e3081f68886fa327e64c8e6/src/api/eth_filter.rs#L20 /// Create a stream that emits items at a fixed interval. Used for rate control pub fn interval( duration: instant::Duration, ) -> impl futures_core::stream::Stream + Send + Unpin { stream::unfold((), move |_| Delay::new(duration).map(|_| Some(((), ())))).map(drop) }