1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 Regit
use super::super::wire::{
CanonicalBlock,
execution::{Call, FeeBlock},
invalid_response, parse_quantity, state_error,
};
use super::EvmClient;
use crate::{
domain::evm::{
AccountNonce, CallResult, FeeSuggestions, GasEstimate, OperationObservation,
OptionalFeeSuggestion, Quantity, ReadOperation, ReadState, TransactionCall,
},
domain::{Address, BlockContext, BlockSelector},
error::{Error, ProviderError},
transport::OperationBudget,
};
fn call_error(code: i64) -> Error {
if code == 3 {
Error::ExecutionReverted
} else {
state_error(code)
}
}
impl EvmClient {
/// Queries separate node price suggestions and an identity-matched latest header.
///
/// Gas price, optional priority price and base fee are independently sourced;
/// no atomic fee snapshot or transaction fee policy is implied. Missing base
/// fee remains absent. All stages and read retries share one total budget.
/// # Errors
/// Reports chain/header mismatch, malformed required prices and backend failures.
/// Unknown optional-method RPC failures fail rather than invent a suggestion.
/// # Panics
/// On native targets, Tokio may panic if the caller's runtime lacks I/O or time drivers.
pub async fn get_fee_suggestions(&self) -> Result<OperationObservation<FeeSuggestions>, Error> {
let budget = OperationBudget::new(self.config.limits())?;
budget
.run(async {
self.verify_chain(&budget).await?;
let block = self.resolve_block(BlockSelector::Latest, &budget).await?;
let header: FeeBlock = self
.http
.read_rpc(
&budget,
"eth_getBlockByHash",
&(block.hash().to_string(), false),
state_error,
)
.await?
.ok_or(Error::UnavailableData)?;
let block_fee = header.into_domain(block)?;
let gas_price: String = self
.http
.read_rpc(&budget, "eth_gasPrice", &[] as &[(); 0], state_error)
.await?
.ok_or(Error::UnavailableData)?;
let gas_price = Quantity::new(parse_quantity(&gas_price)?);
let max_priority_fee_per_gas = self.priority_fee(&budget).await?;
OperationObservation::new(
FeeSuggestions {
chain_id: self.chain_id,
gas_price,
max_priority_fee_per_gas,
block_fee,
},
self.read_context(
ReadOperation::FeeSuggestions,
ReadState::Unanchored,
"eth_gasPrice+eth_maxPriorityFeePerGas+eth_getBlockByHash",
)?,
)
})
.await
}
/// Reads the exact account nonce at one captured canonical block hash.
///
/// Pending transactions are excluded. This does not reserve or automatically
/// choose a signing nonce. Retries retain the identical account and hash.
/// # Errors
/// Reports unavailable state, inconsistent identity, nonce overflow and backend failures.
/// # Panics
/// On native targets, Tokio may panic if the caller's runtime lacks I/O or time drivers.
pub async fn get_account_nonce(
&self,
address: Address,
selector: Option<BlockSelector>,
) -> Result<OperationObservation<AccountNonce>, Error> {
let budget = OperationBudget::new(self.config.limits())?;
budget
.run(async {
let (selector, block) = self.execution_state(selector, &budget).await?;
let result: String = self
.http
.read_rpc(
&budget,
"eth_getTransactionCount",
&(address, CanonicalBlock::new(*block.hash())),
state_error,
)
.await?
.ok_or(Error::UnavailableData)?;
let nonce =
u64::try_from(parse_quantity(&result)?).map_err(|_| invalid_response())?;
OperationObservation::new(
AccountNonce {
chain_id: self.chain_id,
address,
nonce,
},
self.read_context(
ReadOperation::AccountNonce,
ReadState::CanonicalHash {
requested_selector: selector,
block,
},
"eth_getTransactionCount",
)?,
)
})
.await
}
/// Executes explicit parameters locally at one captured canonical state.
///
/// No transaction is signed or submitted. Returned bytes are exact source
/// output, without token boolean interpretation or a future-success guarantee.
/// Sender, nonce, gas cap, value and fees are explicit; no overrides are used.
/// # Errors
/// Reports source code3 revert, null result, chain/state and fixed backend failures.
/// # Panics
/// On native targets, Tokio may panic if the caller's runtime lacks I/O or time drivers.
pub async fn call(
&self,
call: TransactionCall,
selector: Option<BlockSelector>,
) -> Result<OperationObservation<CallResult>, Error> {
self.check_call_chain(&call)?;
let budget = OperationBudget::new(self.config.limits())?;
budget
.run(async {
let (selector, block) = self.execution_state(selector, &budget).await?;
let output = self
.http
.read_rpc(
&budget,
"eth_call",
&(Call(&call), CanonicalBlock::new(*block.hash())),
call_error,
)
.await?
.ok_or(Error::UnavailableData)?;
OperationObservation::new(
CallResult { call, output },
self.read_context(
ReadOperation::Call,
ReadState::CanonicalHash {
requested_selector: selector,
block,
},
"eth_call",
)?,
)
})
.await
}
/// Estimates gas with explicit parameters at one captured canonical block hash.
///
/// This requires the Geth-compatible hash-selector extension: the execution
/// API standard does not require hash selection for `eth_estimateGas`. An
/// unsupported selector remains a typed backend failure, with no fallback to
/// height/latest. RPC `-32602` remains an ambiguous fixed RPC error. Estimates
/// retain the caller's cap and do not guarantee later execution or add margin.
/// # Errors
/// Reports source revert, unsupported capability, malformed/out-of-cap estimate,
/// unavailable state and backend failures. No signing or submission occurs.
/// # Panics
/// On native targets, Tokio may panic if the caller's runtime lacks I/O or time drivers.
pub async fn estimate_gas(
&self,
call: TransactionCall,
selector: Option<BlockSelector>,
) -> Result<OperationObservation<GasEstimate>, Error> {
self.check_call_chain(&call)?;
let budget = OperationBudget::new(self.config.limits())?;
budget
.run(async {
let (selector, block) = self.execution_state(selector, &budget).await?;
let result: String = self
.http
.read_rpc(
&budget,
"eth_estimateGas",
&(Call(&call), CanonicalBlock::new(*block.hash())),
call_error,
)
.await?
.ok_or(Error::UnavailableData)?;
let gas =
u64::try_from(parse_quantity(&result)?).map_err(|_| invalid_response())?;
let value = GasEstimate::new(call, gas).map_err(|_| invalid_response())?;
OperationObservation::new(
value,
self.read_context(
ReadOperation::GasEstimate,
ReadState::CanonicalHash {
requested_selector: selector,
block,
},
"eth_estimateGas",
)?,
)
})
.await
}
fn check_call_chain(&self, call: &TransactionCall) -> Result<(), Error> {
if call.data().chain_id != self.chain_id {
return Err(Error::Provider(ProviderError::ChainMismatch));
}
Ok(())
}
async fn execution_state(
&self,
selector: Option<BlockSelector>,
budget: &OperationBudget,
) -> Result<(BlockSelector, BlockContext), Error> {
self.verify_chain(budget).await?;
let selector = selector.unwrap_or_else(|| self.config.default_selector());
let block = self.resolve_block(selector, budget).await?;
Ok((selector, block))
}
async fn priority_fee(&self, budget: &OperationBudget) -> Result<OptionalFeeSuggestion, Error> {
let result: Result<Option<String>, Error> = self
.http
.read_rpc(
budget,
"eth_maxPriorityFeePerGas",
&[] as &[(); 0],
state_error,
)
.await;
match result {
Ok(Some(value)) => Ok(OptionalFeeSuggestion::Available(Quantity::new(
parse_quantity(&value)?,
))),
Ok(None) => Ok(OptionalFeeSuggestion::NullResult),
Err(Error::UnsupportedCapability) => Ok(OptionalFeeSuggestion::Unsupported),
Err(Error::UnavailableData) => Ok(OptionalFeeSuggestion::Unavailable),
Err(error) => Err(error),
}
}
}
impl super::super::FeeReader for EvmClient {
async fn get_fee_suggestions(&self) -> Result<OperationObservation<FeeSuggestions>, Error> {
Self::get_fee_suggestions(self).await
}
}
impl super::super::ExecutionReader for EvmClient {
async fn get_account_nonce(
&self,
address: Address,
selector: Option<BlockSelector>,
) -> Result<OperationObservation<AccountNonce>, Error> {
Self::get_account_nonce(self, address, selector).await
}
async fn call(
&self,
call: TransactionCall,
selector: Option<BlockSelector>,
) -> Result<OperationObservation<CallResult>, Error> {
Self::call(self, call, selector).await
}
async fn estimate_gas(
&self,
call: TransactionCall,
selector: Option<BlockSelector>,
) -> Result<OperationObservation<GasEstimate>, Error> {
Self::estimate_gas(self, call, selector).await
}
}