Skip to content

Commit 614d6db

Browse files
prestwichmattsse
andauthored
refactor: use the params struct in more places (#1892)
Co-authored-by: Matthias Seitz <[email protected]>
1 parent 46ec4b1 commit 614d6db

File tree

2 files changed

+28
-49
lines changed

2 files changed

+28
-49
lines changed

crates/provider/src/provider/eth_call.rs

+27-48
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ where
104104
{
105105
Preparing {
106106
caller: Arc<dyn Caller<N, Resp>>,
107-
data: &'req N::TransactionRequest,
108-
overrides: Option<&'req StateOverride>,
109-
block: Option<BlockId>,
107+
params: EthCallParams<'req, N>,
110108
method: &'static str,
111109
map: Map,
112110
},
@@ -126,13 +124,9 @@ where
126124
{
127125
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
128126
match self {
129-
Self::Preparing { caller: _, data, overrides, block, method, map: _ } => f
130-
.debug_struct("Preparing")
131-
.field("data", data)
132-
.field("overrides", overrides)
133-
.field("block", block)
134-
.field("method", method)
135-
.finish(),
127+
Self::Preparing { caller: _, params, method, map: _ } => {
128+
f.debug_struct("Preparing").field("params", params).field("method", method).finish()
129+
}
136130
Self::Running { .. } => f.debug_tuple("Running").finish(),
137131
Self::Polling => f.debug_tuple("Polling").finish(),
138132
}
@@ -157,18 +151,12 @@ where
157151
}
158152

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

166-
let params = EthCallParams {
167-
data: Cow::Borrowed(data),
168-
block,
169-
overrides: overrides.map(Cow::Borrowed),
170-
};
171-
172160
let fut =
173161
if method.eq("eth_call") { caller.call(params) } else { caller.estimate_gas(params) }?;
174162

@@ -223,9 +211,7 @@ where
223211
Map: Fn(Resp) -> Output,
224212
{
225213
caller: Arc<dyn Caller<N, Resp>>,
226-
data: &'req N::TransactionRequest,
227-
overrides: Option<&'req StateOverride>,
228-
block: Option<BlockId>,
214+
params: EthCallParams<'req, N>,
229215
method: &'static str,
230216
map: Map,
231217
_pd: PhantomData<fn() -> (Resp, Output)>,
@@ -238,10 +224,8 @@ where
238224
{
239225
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
240226
f.debug_struct("EthCall")
227+
.field("params", &self.params)
241228
.field("method", &self.method)
242-
.field("data", &self.data)
243-
.field("block", &self.block)
244-
.field("overrides", &self.overrides)
245229
.finish()
246230
}
247231
}
@@ -251,33 +235,32 @@ where
251235
N: Network,
252236
Resp: RpcReturn,
253237
{
254-
/// Create a new CallBuilder.
255-
pub fn new(caller: impl Caller<N, Resp> + 'static, data: &'req N::TransactionRequest) -> Self {
238+
/// Create a new [`EthCall`].
239+
pub fn new(
240+
caller: impl Caller<N, Resp> + 'static,
241+
method: &'static str,
242+
data: &'req N::TransactionRequest,
243+
) -> Self {
256244
Self {
257245
caller: Arc::new(caller),
258-
data,
259-
overrides: None,
260-
block: None,
261-
method: "eth_call",
246+
params: EthCallParams::new(data),
247+
method,
262248
map: std::convert::identity,
263249
_pd: PhantomData,
264250
}
265251
}
266252

267-
/// Create new EthCall for gas estimates.
253+
/// Create a new [`EthCall`] with method set to `"eth_call"`.
254+
pub fn call(caller: impl Caller<N, Resp> + 'static, data: &'req N::TransactionRequest) -> Self {
255+
Self::new(caller, "eth_call", data)
256+
}
257+
258+
/// Create a new [`EthCall`] with method set to `"eth_estimateGas"`.
268259
pub fn gas_estimate(
269260
caller: impl Caller<N, Resp> + 'static,
270261
data: &'req N::TransactionRequest,
271262
) -> Self {
272-
Self {
273-
caller: Arc::new(caller),
274-
data,
275-
overrides: None,
276-
block: None,
277-
method: "eth_estimateGas",
278-
map: std::convert::identity,
279-
_pd: PhantomData,
280-
}
263+
Self::new(caller, "eth_estimateGas", data)
281264
}
282265
}
283266

@@ -307,24 +290,22 @@ where
307290
{
308291
EthCall {
309292
caller: self.caller,
310-
data: self.data,
311-
overrides: self.overrides,
312-
block: self.block,
293+
params: self.params,
313294
method: self.method,
314295
map,
315296
_pd: PhantomData,
316297
}
317298
}
318299

319300
/// Set the state overrides for this call.
320-
pub const fn overrides(mut self, overrides: &'req StateOverride) -> Self {
321-
self.overrides = Some(overrides);
301+
pub fn overrides(mut self, overrides: &'req StateOverride) -> Self {
302+
self.params.overrides = Some(Cow::Borrowed(overrides));
322303
self
323304
}
324305

325306
/// Set the block to use for this call.
326307
pub const fn block(mut self, block: BlockId) -> Self {
327-
self.block = Some(block);
308+
self.params.block = Some(block);
328309
self
329310
}
330311
}
@@ -344,9 +325,7 @@ where
344325
EthCallFut {
345326
inner: EthCallFutInner::Preparing {
346327
caller: self.caller,
347-
data: self.data,
348-
overrides: self.overrides,
349-
block: self.block,
328+
params: self.params,
350329
method: self.method,
351330
map: self.map,
352331
},

crates/provider/src/provider/trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub trait Provider<N: Network = Ethereum>: Send + Sync {
150150
#[doc(alias = "eth_call")]
151151
#[doc(alias = "call_with_overrides")]
152152
fn call<'req>(&self, tx: &'req N::TransactionRequest) -> EthCall<'req, N, Bytes> {
153-
EthCall::new(self.weak_client(), tx).block(BlockNumberOrTag::Pending.into())
153+
EthCall::call(self.weak_client(), tx).block(BlockNumberOrTag::Pending.into())
154154
}
155155

156156
/// Executes an arbitrary number of transactions on top of the requested state.

0 commit comments

Comments
 (0)