diff --git a/ethers-contract/src/call.rs b/ethers-contract/src/call.rs index 1f753414..c9a8ab63 100644 --- a/ethers-contract/src/call.rs +++ b/ethers-contract/src/call.rs @@ -14,7 +14,14 @@ use ethers_providers::{ 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; @@ -211,3 +218,19 @@ where .map_err(ContractError::MiddlewareError) } } + +/// [`ContractCall`] can be turned into [`Future`] automatically with `.await`. +/// Defaults to calling [`ContractCall::call`]. +impl IntoFuture for ContractCall +where + Self: 'static, + M: Middleware, + D: Detokenize, +{ + type Output = Result>; + type IntoFuture = Pin>>; + + fn into_future(self) -> Self::IntoFuture { + Box::pin(async move { self.call().await }) + } +}