Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: delete Caller trait #1893

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub mod layers;

mod provider;
pub use provider::{
builder, Caller, EthCall, EthCallParams, FilterPollerBuilder, ParamsWithBlock, Provider,
ProviderCall, RootProvider, RpcWithBlock, SendableTx, WalletProvider,
builder, EthCall, EthCallParams, FilterPollerBuilder, ParamsWithBlock, Provider, ProviderCall,
RootProvider, RpcWithBlock, SendableTx, WalletProvider,
};

pub mod utils;
Expand Down
62 changes: 0 additions & 62 deletions crates/provider/src/provider/caller.rs

This file was deleted.

143 changes: 78 additions & 65 deletions crates/provider/src/provider/eth_call.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use crate::ProviderCall;
use alloy_eips::BlockId;
use alloy_json_rpc::RpcReturn;
use alloy_network::Network;
use alloy_rpc_client::WeakClient;
use alloy_rpc_types_eth::state::StateOverride;
use alloy_transport::{Transport, TransportResult};
use alloy_transport::{Transport, TransportErrorKind, TransportResult};
use futures::FutureExt;
use serde::ser::SerializeSeq;
use std::{borrow::Cow, future::Future, marker::PhantomData, sync::Arc, task::Poll};

use crate::{Caller, ProviderCall};
use std::{
borrow::Cow,
future::{Future, IntoFuture},
marker::PhantomData,
task::Poll,
};

/// The parameters for an `"eth_call"` RPC request.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -81,7 +86,12 @@ impl<N: Network> serde::Serialize for EthCallParams<'_, N> {
}
}

/// The [`EthCallFut`] future is the future type for an `eth_call` RPC request.
/// The [`EthCallFut`] future is the future type for an `"eth_call"` RPC
/// request.
///
/// This future is intended to handle RPC requests that take the same
/// parameters as `"eth_call"`, e.g. `"eth_estimateGas"`. It is instantiated by
/// [`EthCall::into_future`], and wraps .
#[derive(Debug)]
#[doc(hidden)] // Not public API.
#[allow(unnameable_types)]
Expand All @@ -105,10 +115,8 @@ where
Map: Fn(Resp) -> Output,
{
Preparing {
caller: Arc<dyn Caller<T, N, Resp>>,
data: &'req N::TransactionRequest,
overrides: Option<&'req StateOverride>,
block: Option<BlockId>,
client: WeakClient<T>,
params: EthCallParams<'req, N>,
method: &'static str,
map: Map,
},
Expand All @@ -129,13 +137,9 @@ where
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Preparing { caller: _, data, overrides, block, method, map: _ } => f
.debug_struct("Preparing")
.field("data", data)
.field("overrides", overrides)
.field("block", block)
.field("method", method)
.finish(),
Self::Preparing { client: _, params, method, map: _ } => {
f.debug_struct("Preparing").field("params", params).field("method", method).finish()
}
Self::Running { .. } => f.debug_tuple("Running").finish(),
Self::Polling => f.debug_tuple("Polling").finish(),
}
Expand All @@ -161,20 +165,15 @@ where
}

fn poll_preparing(&mut self, cx: &mut std::task::Context<'_>) -> Poll<TransportResult<Output>> {
let EthCallFutInner::Preparing { caller, data, overrides, block, method, map } =
let EthCallFutInner::Preparing { client, params, method, map } =
std::mem::replace(&mut self.inner, EthCallFutInner::Polling)
else {
unreachable!("bad state")
};

let params = EthCallParams {
data: Cow::Borrowed(data),
block,
overrides: overrides.map(Cow::Borrowed),
};
let client = client.upgrade().ok_or_else(TransportErrorKind::backend_gone)?;

let fut =
if method.eq("eth_call") { caller.call(params) } else { caller.estimate_gas(params) }?;
let fut = client.request(method, params.into_owned()).into();

self.inner = EthCallFutInner::Running { map, fut };

Expand Down Expand Up @@ -218,6 +217,11 @@ where
/// A builder for an `"eth_call"` request. This type is returned by the
/// [`Provider::call`] method.
///
/// This type is intended to handle RPC requests that take the same
/// parameters as `"eth_call"`, e.g. `"eth_estimateGas"`. It is instantiated by
/// [`Provider::call`] and executed by calling [`IntoFuture::into_future`],
/// or via `.await`.
///
/// [`Provider::call`]: crate::Provider::call
#[must_use = "EthCall must be awaited to execute the call"]
#[derive(Clone)]
Expand All @@ -228,10 +232,8 @@ where
Resp: RpcReturn,
Map: Fn(Resp) -> Output,
{
caller: Arc<dyn Caller<T, N, Resp>>,
data: &'req N::TransactionRequest,
overrides: Option<&'req StateOverride>,
block: Option<BlockId>,
client: WeakClient<T>,
params: EthCallParams<'req, N>,
method: &'static str,
map: Map,
_pd: PhantomData<fn() -> (Resp, Output)>,
Expand All @@ -245,10 +247,8 @@ where
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("EthCall")
.field("params", &self.params)
.field("method", &self.method)
.field("data", &self.data)
.field("block", &self.block)
.field("overrides", &self.overrides)
.finish()
}
}
Expand All @@ -259,36 +259,54 @@ where
N: Network,
Resp: RpcReturn,
{
/// Create a new CallBuilder.
/// Create a new [`EthCall`].
pub fn new(
caller: impl Caller<T, N, Resp> + 'static,
client: WeakClient<T>,
method: &'static str,
data: &'req N::TransactionRequest,
) -> Self {
Self {
caller: Arc::new(caller),
data,
overrides: None,
block: None,
method: "eth_call",
client,
params: EthCallParams::new(data),
method,
map: std::convert::identity,
_pd: PhantomData,
}
}

/// Create new EthCall for gas estimates.
pub fn gas_estimate(
caller: impl Caller<T, N, Resp> + 'static,
data: &'req N::TransactionRequest,
) -> Self {
Self {
caller: Arc::new(caller),
data,
overrides: None,
block: None,
method: "eth_estimateGas",
map: std::convert::identity,
_pd: PhantomData,
}
/// Create a new [`EthCall`] with method set to `"eth_call"`.
pub fn call(client: WeakClient<T>, data: &'req N::TransactionRequest) -> Self {
Self::new(client, "eth_call", data)
}

/// Create a new [`EthCall`] with method set to `"eth_estimateGas"`.
pub fn gas_estimate(client: WeakClient<T>, data: &'req N::TransactionRequest) -> Self {
Self::new(client, "eth_estimateGas", data)
}

/// Create a new [`EthCall`] with method set to `"eth_call"`.
pub fn method(&self) -> &'static str {
self.method
}

/// Set the method for this call.
pub fn set_method(&mut self, method: &'static str) {
self.method = method;
}

/// Returns a reference to the parameters.
pub fn params(&self) -> &EthCallParams<'req, N> {
&self.params
}

/// Returns a mutable reference to the parameters.
pub fn params_mut(&mut self) -> &mut EthCallParams<'req, N> {
&mut self.params
}

/// Get a weak reference to the client.
pub fn client(&self) -> WeakClient<T> {
self.client.clone()
}
}

Expand Down Expand Up @@ -318,31 +336,28 @@ where
NewMap: Fn(Resp) -> NewOutput,
{
EthCall {
caller: self.caller,
data: self.data,
overrides: self.overrides,
block: self.block,
client: self.client,
params: self.params,
method: self.method,
map,
_pd: PhantomData,
}
}

/// Set the state overrides for this call.
pub const fn overrides(mut self, overrides: &'req StateOverride) -> Self {
self.overrides = Some(overrides);
pub fn overrides(mut self, overrides: &'req StateOverride) -> Self {
self.params.overrides = Some(Cow::Borrowed(overrides));
self
}

/// Set the block to use for this call.
pub const fn block(mut self, block: BlockId) -> Self {
self.block = Some(block);
self.params.block = Some(block);
self
}
}

impl<'req, T, N, Resp, Output, Map> std::future::IntoFuture
for EthCall<'req, T, N, Resp, Output, Map>
impl<'req, T, N, Resp, Output, Map> IntoFuture for EthCall<'req, T, N, Resp, Output, Map>
where
T: Transport + Clone,
N: Network,
Expand All @@ -357,10 +372,8 @@ where
fn into_future(self) -> Self::IntoFuture {
EthCallFut {
inner: EthCallFutInner::Preparing {
caller: self.caller,
data: self.data,
overrides: self.overrides,
block: self.block,
client: self.client,
params: self.params,
method: self.method,
map: self.map,
},
Expand Down
3 changes: 0 additions & 3 deletions crates/provider/src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,3 @@ pub use wallet::WalletProvider;

mod with_block;
pub use with_block::{ParamsWithBlock, RpcWithBlock};

mod caller;
pub use caller::Caller;
2 changes: 1 addition & 1 deletion crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
#[doc(alias = "eth_call")]
#[doc(alias = "call_with_overrides")]
fn call<'req>(&self, tx: &'req N::TransactionRequest) -> EthCall<'req, T, N, Bytes> {
EthCall::new(self.weak_client(), tx).block(BlockNumberOrTag::Pending.into())
EthCall::call(self.weak_client(), tx).block(BlockNumberOrTag::Pending.into())
}

/// Executes an arbitrary number of transactions on top of the requested state.
Expand Down
Loading