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: use the params struct in more places #1892

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
73 changes: 26 additions & 47 deletions crates/provider/src/provider/eth_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ where
{
Preparing {
caller: Arc<dyn Caller<T, N, Resp>>,
data: &'req N::TransactionRequest,
overrides: Option<&'req StateOverride>,
block: Option<BlockId>,
params: EthCallParams<'req, N>,
method: &'static str,
map: Map,
},
Expand All @@ -129,13 +127,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 { caller: _, 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,18 +155,12 @@ 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 { caller, 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 fut =
if method.eq("eth_call") { caller.call(params) } else { caller.estimate_gas(params) }?;

Expand Down Expand Up @@ -229,9 +217,7 @@ where
Map: Fn(Resp) -> Output,
{
caller: Arc<dyn Caller<T, N, Resp>>,
data: &'req N::TransactionRequest,
overrides: Option<&'req StateOverride>,
block: Option<BlockId>,
params: EthCallParams<'req, N>,
method: &'static str,
map: Map,
_pd: PhantomData<fn() -> (Resp, Output)>,
Expand All @@ -245,10 +231,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 +243,35 @@ where
N: Network,
Resp: RpcReturn,
{
/// Create a new CallBuilder.
/// Create a new [`EthCall`].
pub fn new(
caller: impl Caller<T, N, Resp> + 'static,
method: &'static str,
data: &'req N::TransactionRequest,
) -> Self {
Self {
caller: Arc::new(caller),
data,
overrides: None,
block: None,
method: "eth_call",
params: EthCallParams::new(data),
method,
map: std::convert::identity,
_pd: PhantomData,
}
}

/// Create new EthCall for gas estimates.
/// Create a new [`EthCall`] with method set to `"eth_call"`.
pub fn call(
caller: impl Caller<T, N, Resp> + 'static,
data: &'req N::TransactionRequest,
) -> Self {
Self::new(caller, "eth_call", data)
}

/// Create a new [`EthCall`] with method set to `"eth_estimateGas"`.
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,
}
Self::new(caller, "eth_estimateGas", data)
}
}

Expand Down Expand Up @@ -319,24 +302,22 @@ where
{
EthCall {
caller: self.caller,
data: self.data,
overrides: self.overrides,
block: self.block,
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
}
}
Expand All @@ -358,9 +339,7 @@ where
EthCallFut {
inner: EthCallFutInner::Preparing {
caller: self.caller,
data: self.data,
overrides: self.overrides,
block: self.block,
params: self.params,
method: self.method,
map: self.map,
},
Expand Down
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