41 lines
1.3 KiB
Rust
41 lines
1.3 KiB
Rust
use ethers_core::types::U256;
|
|
use futures_util::{stream, FutureExt, StreamExt};
|
|
use std::{future::Future, pin::Pin};
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
use futures_timer::Delay;
|
|
#[cfg(target_arch = "wasm32")]
|
|
use wasm_timer::Delay;
|
|
|
|
use crate::ProviderError;
|
|
|
|
/// A simple gas escalation policy
|
|
pub type EscalationPolicy = Box<dyn Fn(U256, usize) -> U256 + Send + Sync>;
|
|
|
|
// Helper type alias
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub(crate) type PinBoxFut<'a, T> = Pin<Box<dyn Future<Output = Result<T, ProviderError>> + 'a>>;
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub(crate) type PinBoxFut<'a, T> =
|
|
Pin<Box<dyn Future<Output = Result<T, ProviderError>> + Send + 'a>>;
|
|
|
|
/// Calls the future if `item` is None, otherwise returns a `futures::ok`
|
|
pub async fn maybe<F, T, E>(item: Option<T>, f: F) -> Result<T, E>
|
|
where
|
|
F: Future<Output = Result<T, E>>,
|
|
{
|
|
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: std::time::Duration,
|
|
) -> impl futures_core::stream::Stream<Item = ()> + Send + Unpin {
|
|
stream::unfold((), move |_| Delay::new(duration).map(|_| Some(((), ())))).map(drop)
|
|
}
|