Impl `IntoFuture` for `ContractCall` (#1826)

This commit is contained in:
Lev Khoroshansky 2022-11-07 02:54:18 +01:00 committed by GitHub
parent 14e038860b
commit cfc301d6b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 1 deletions

View File

@ -14,7 +14,14 @@ use ethers_providers::{
Middleware, PendingTransaction, ProviderError, Middleware, PendingTransaction, ProviderError,
}; };
use std::{borrow::Cow, fmt::Debug, future::Future, marker::PhantomData, sync::Arc}; use std::{
borrow::Cow,
fmt::Debug,
future::{Future, IntoFuture},
marker::PhantomData,
pin::Pin,
sync::Arc,
};
use thiserror::Error as ThisError; use thiserror::Error as ThisError;
@ -211,3 +218,19 @@ where
.map_err(ContractError::MiddlewareError) .map_err(ContractError::MiddlewareError)
} }
} }
/// [`ContractCall`] can be turned into [`Future`] automatically with `.await`.
/// Defaults to calling [`ContractCall::call`].
impl<M, D> IntoFuture for ContractCall<M, D>
where
Self: 'static,
M: Middleware,
D: Detokenize,
{
type Output = Result<D, ContractError<M>>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output>>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.call().await })
}
}