From cfc301d6b1f6a521a6a634c74c8ba8b5941765c7 Mon Sep 17 00:00:00 2001 From: Lev Khoroshansky Date: Mon, 7 Nov 2022 02:54:18 +0100 Subject: [PATCH] Impl `IntoFuture` for `ContractCall` (#1826) --- ethers-contract/src/call.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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 }) + } +}