Skip to main content

reth_rpc_eth_api/
core.rs

1//! Implementation of the [`jsonrpsee`] generated [`EthApiServer`] trait. Handles RPC requests for
2//! the `eth_` namespace.
3use crate::{
4    helpers::{EthApiSpec, EthBlocks, EthCall, EthFees, EthState, EthTransactions, FullEthApi},
5    RpcBlock, RpcHeader, RpcReceipt, RpcTransaction,
6};
7use alloy_dyn_abi::TypedData;
8use alloy_eips::{eip2930::AccessListResult, BlockId, BlockNumberOrTag};
9use alloy_json_rpc::RpcObject;
10use alloy_primitives::{Address, Bytes, B256, B64, U256, U64};
11use alloy_rpc_types_eth::{
12    simulate::{SimulatePayload, SimulatedBlock},
13    state::{EvmOverrides, StateOverride},
14    BlockOverrides, Bundle, EIP1186AccountProofResponse, EthCallResponse, FeeHistory, Index,
15    StateContext, SyncStatus, Work,
16};
17use alloy_serde::JsonStorageKey;
18use jsonrpsee::{core::RpcResult, proc_macros::rpc};
19use reth_primitives_traits::TxTy;
20use reth_rpc_convert::RpcTxReq;
21use reth_rpc_eth_types::FillTransaction;
22use reth_rpc_server_types::{result::internal_rpc_err, ToRpcResult};
23use std::collections::HashMap;
24use tracing::trace;
25
26/// Helper trait, unifies functionality that must be supported to implement all RPC methods for
27/// server.
28pub trait FullEthApiServer:
29    EthApiServer<
30        RpcTxReq<Self::NetworkTypes>,
31        RpcTransaction<Self::NetworkTypes>,
32        RpcBlock<Self::NetworkTypes>,
33        RpcReceipt<Self::NetworkTypes>,
34        RpcHeader<Self::NetworkTypes>,
35        TxTy<Self::Primitives>,
36    > + FullEthApi
37    + Clone
38{
39}
40
41impl<T> FullEthApiServer for T where
42    T: EthApiServer<
43            RpcTxReq<T::NetworkTypes>,
44            RpcTransaction<T::NetworkTypes>,
45            RpcBlock<T::NetworkTypes>,
46            RpcReceipt<T::NetworkTypes>,
47            RpcHeader<T::NetworkTypes>,
48            TxTy<T::Primitives>,
49        > + FullEthApi
50        + Clone
51{
52}
53
54/// Eth rpc interface: <https://ethereum.github.io/execution-apis/api-documentation>
55#[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))]
56#[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))]
57pub trait EthApi<
58    TxReq: RpcObject,
59    T: RpcObject,
60    B: RpcObject,
61    R: RpcObject,
62    H: RpcObject,
63    RawTx: RpcObject,
64>
65{
66    /// Returns the protocol version encoded as a string.
67    #[method(name = "protocolVersion")]
68    async fn protocol_version(&self) -> RpcResult<U64>;
69
70    /// Returns an object with data about the sync status or false.
71    #[method(name = "syncing")]
72    fn syncing(&self) -> RpcResult<SyncStatus>;
73
74    /// Returns the client coinbase address.
75    #[method(name = "coinbase")]
76    async fn author(&self) -> RpcResult<Address>;
77
78    /// Returns a list of addresses owned by client.
79    #[method(name = "accounts")]
80    fn accounts(&self) -> RpcResult<Vec<Address>>;
81
82    /// Returns the number of most recent block.
83    #[method(name = "blockNumber")]
84    fn block_number(&self) -> RpcResult<U256>;
85
86    /// Returns the chain ID of the current network.
87    #[method(name = "chainId")]
88    async fn chain_id(&self) -> RpcResult<Option<U64>>;
89
90    /// Returns information about a block by hash.
91    #[method(name = "getBlockByHash")]
92    async fn block_by_hash(&self, hash: B256, full: bool) -> RpcResult<Option<B>>;
93
94    /// Returns information about a block by number.
95    #[method(name = "getBlockByNumber")]
96    async fn block_by_number(&self, number: BlockNumberOrTag, full: bool) -> RpcResult<Option<B>>;
97
98    /// Returns the number of transactions in a block from a block matching the given block hash.
99    #[method(name = "getBlockTransactionCountByHash")]
100    async fn block_transaction_count_by_hash(&self, hash: B256) -> RpcResult<Option<U256>>;
101
102    /// Returns the number of transactions in a block matching the given block number.
103    #[method(name = "getBlockTransactionCountByNumber")]
104    async fn block_transaction_count_by_number(
105        &self,
106        number: BlockNumberOrTag,
107    ) -> RpcResult<Option<U256>>;
108
109    /// Returns the number of uncles in a block from a block matching the given block hash.
110    #[method(name = "getUncleCountByBlockHash")]
111    async fn block_uncles_count_by_hash(&self, hash: B256) -> RpcResult<Option<U256>>;
112
113    /// Returns the number of uncles in a block with given block number.
114    #[method(name = "getUncleCountByBlockNumber")]
115    async fn block_uncles_count_by_number(
116        &self,
117        number: BlockNumberOrTag,
118    ) -> RpcResult<Option<U256>>;
119
120    /// Returns all transaction receipts for a given block.
121    #[method(name = "getBlockReceipts")]
122    async fn block_receipts(&self, block_id: BlockId) -> RpcResult<Option<Vec<R>>>;
123
124    /// Returns an uncle block of the given block and index.
125    #[method(name = "getUncleByBlockHashAndIndex")]
126    async fn uncle_by_block_hash_and_index(&self, hash: B256, index: Index)
127        -> RpcResult<Option<B>>;
128
129    /// Returns an uncle block of the given block and index.
130    #[method(name = "getUncleByBlockNumberAndIndex")]
131    async fn uncle_by_block_number_and_index(
132        &self,
133        number: BlockNumberOrTag,
134        index: Index,
135    ) -> RpcResult<Option<B>>;
136
137    /// Returns the EIP-2718 encoded transaction if it exists.
138    ///
139    /// If this is a EIP-4844 transaction that is in the pool it will include the sidecar.
140    #[method(name = "getRawTransactionByHash")]
141    async fn raw_transaction_by_hash(&self, hash: B256) -> RpcResult<Option<Bytes>>;
142
143    /// Returns the information about a transaction requested by transaction hash.
144    #[method(name = "getTransactionByHash")]
145    async fn transaction_by_hash(&self, hash: B256) -> RpcResult<Option<T>>;
146
147    /// Returns information about a raw transaction by block hash and transaction index position.
148    #[method(name = "getRawTransactionByBlockHashAndIndex")]
149    async fn raw_transaction_by_block_hash_and_index(
150        &self,
151        hash: B256,
152        index: Index,
153    ) -> RpcResult<Option<Bytes>>;
154
155    /// Returns information about a transaction by block hash and transaction index position.
156    #[method(name = "getTransactionByBlockHashAndIndex")]
157    async fn transaction_by_block_hash_and_index(
158        &self,
159        hash: B256,
160        index: Index,
161    ) -> RpcResult<Option<T>>;
162
163    /// Returns information about a raw transaction by block number and transaction index
164    /// position.
165    #[method(name = "getRawTransactionByBlockNumberAndIndex")]
166    async fn raw_transaction_by_block_number_and_index(
167        &self,
168        number: BlockNumberOrTag,
169        index: Index,
170    ) -> RpcResult<Option<Bytes>>;
171
172    /// Returns information about a transaction by block number and transaction index position.
173    #[method(name = "getTransactionByBlockNumberAndIndex")]
174    async fn transaction_by_block_number_and_index(
175        &self,
176        number: BlockNumberOrTag,
177        index: Index,
178    ) -> RpcResult<Option<T>>;
179
180    /// Returns information about a transaction by sender and nonce.
181    #[method(name = "getTransactionBySenderAndNonce")]
182    async fn transaction_by_sender_and_nonce(
183        &self,
184        address: Address,
185        nonce: U64,
186    ) -> RpcResult<Option<T>>;
187
188    /// Returns the receipt of a transaction by transaction hash.
189    #[method(name = "getTransactionReceipt")]
190    async fn transaction_receipt(&self, hash: B256) -> RpcResult<Option<R>>;
191
192    /// Returns the balance of the account of given address.
193    #[method(name = "getBalance")]
194    async fn balance(&self, address: Address, block_number: Option<BlockId>) -> RpcResult<U256>;
195
196    /// Returns the value from a storage position at a given address
197    #[method(name = "getStorageAt")]
198    async fn storage_at(
199        &self,
200        address: Address,
201        index: JsonStorageKey,
202        block_number: Option<BlockId>,
203    ) -> RpcResult<B256>;
204
205    /// Returns values from multiple storage positions across multiple addresses.
206    #[method(name = "getStorageValues")]
207    async fn storage_values(
208        &self,
209        requests: HashMap<Address, Vec<JsonStorageKey>>,
210        block_number: Option<BlockId>,
211    ) -> RpcResult<HashMap<Address, Vec<B256>>>;
212
213    /// Returns the number of transactions sent from an address at given block number.
214    #[method(name = "getTransactionCount")]
215    async fn transaction_count(
216        &self,
217        address: Address,
218        block_number: Option<BlockId>,
219    ) -> RpcResult<U256>;
220
221    /// Returns code at a given address at given block number.
222    #[method(name = "getCode")]
223    async fn get_code(&self, address: Address, block_number: Option<BlockId>) -> RpcResult<Bytes>;
224
225    /// Returns the block's header at given number.
226    #[method(name = "getHeaderByNumber")]
227    async fn header_by_number(&self, hash: BlockNumberOrTag) -> RpcResult<Option<H>>;
228
229    /// Returns the block's header at given hash.
230    #[method(name = "getHeaderByHash")]
231    async fn header_by_hash(&self, hash: B256) -> RpcResult<Option<H>>;
232
233    /// `eth_simulateV1` executes an arbitrary number of transactions on top of the requested state.
234    /// The transactions are packed into individual blocks. Overrides can be provided.
235    #[method(name = "simulateV1")]
236    async fn simulate_v1(
237        &self,
238        opts: SimulatePayload<TxReq>,
239        block_number: Option<BlockId>,
240    ) -> RpcResult<Vec<SimulatedBlock<B>>>;
241
242    /// Executes a new message call immediately without creating a transaction on the block chain.
243    #[method(name = "call")]
244    async fn call(
245        &self,
246        request: TxReq,
247        block_number: Option<BlockId>,
248        state_overrides: Option<StateOverride>,
249        block_overrides: Option<Box<BlockOverrides>>,
250    ) -> RpcResult<Bytes>;
251
252    /// Fills the defaults on a given unsigned transaction.
253    #[method(name = "fillTransaction")]
254    async fn fill_transaction(&self, request: TxReq) -> RpcResult<FillTransaction<RawTx>>;
255
256    /// Simulate arbitrary number of transactions at an arbitrary blockchain index, with the
257    /// optionality of state overrides
258    #[method(name = "callMany")]
259    async fn call_many(
260        &self,
261        bundles: Vec<Bundle<TxReq>>,
262        state_context: Option<StateContext>,
263        state_override: Option<StateOverride>,
264    ) -> RpcResult<Vec<Vec<EthCallResponse>>>;
265
266    /// Generates an access list for a transaction.
267    ///
268    /// This method creates an [EIP2930](https://eips.ethereum.org/EIPS/eip-2930) type accessList based on a given Transaction.
269    ///
270    /// An access list contains all storage slots and addresses touched by the transaction, except
271    /// for the sender account and the chain's precompiles.
272    ///
273    /// It returns list of addresses and storage keys used by the transaction, plus the gas
274    /// consumed when the access list is added. That is, it gives you the list of addresses and
275    /// storage keys that will be used by that transaction, plus the gas consumed if the access
276    /// list is included. Like `eth_estimateGas`, this is an estimation; the list could change
277    /// when the transaction is actually mined. Adding an accessList to your transaction does
278    /// not necessary result in lower gas usage compared to a transaction without an access
279    /// list.
280    #[method(name = "createAccessList")]
281    async fn create_access_list(
282        &self,
283        request: TxReq,
284        block_number: Option<BlockId>,
285        state_override: Option<StateOverride>,
286    ) -> RpcResult<AccessListResult>;
287
288    /// Generates and returns an estimate of how much gas is necessary to allow the transaction to
289    /// complete.
290    #[method(name = "estimateGas")]
291    async fn estimate_gas(
292        &self,
293        request: TxReq,
294        block_number: Option<BlockId>,
295        state_override: Option<StateOverride>,
296    ) -> RpcResult<U256>;
297
298    /// Returns the current price per gas in wei.
299    #[method(name = "gasPrice")]
300    async fn gas_price(&self) -> RpcResult<U256>;
301
302    /// Returns the account details by specifying an address and a block number/tag
303    #[method(name = "getAccount")]
304    async fn get_account(
305        &self,
306        address: Address,
307        block: BlockId,
308    ) -> RpcResult<Option<alloy_rpc_types_eth::Account>>;
309
310    /// Introduced in EIP-1559, returns suggestion for the priority for dynamic fee transactions.
311    #[method(name = "maxPriorityFeePerGas")]
312    async fn max_priority_fee_per_gas(&self) -> RpcResult<U256>;
313
314    /// Introduced in EIP-4844, returns the current blob base fee in wei.
315    #[method(name = "blobBaseFee")]
316    async fn blob_base_fee(&self) -> RpcResult<U256>;
317
318    /// Returns the Transaction fee history
319    ///
320    /// Introduced in EIP-1559 for getting information on the appropriate priority fee to use.
321    ///
322    /// Returns transaction base fee per gas and effective priority fee per gas for the
323    /// requested/supported block range. The returned Fee history for the returned block range
324    /// can be a subsection of the requested range if not all blocks are available.
325    #[method(name = "feeHistory")]
326    async fn fee_history(
327        &self,
328        block_count: U64,
329        newest_block: BlockNumberOrTag,
330        reward_percentiles: Option<Vec<f64>>,
331    ) -> RpcResult<FeeHistory>;
332
333    /// Returns whether the client is actively mining new blocks.
334    #[method(name = "mining")]
335    async fn is_mining(&self) -> RpcResult<bool>;
336
337    /// Returns the number of hashes per second that the node is mining with.
338    #[method(name = "hashrate")]
339    async fn hashrate(&self) -> RpcResult<U256>;
340
341    /// Returns the hash of the current block, the seedHash, and the boundary condition to be met
342    /// (“target”)
343    #[method(name = "getWork")]
344    async fn get_work(&self) -> RpcResult<Work>;
345
346    /// Used for submitting mining hashrate.
347    ///
348    /// Can be used for remote miners to submit their hash rate.
349    /// It accepts the miner hash rate and an identifier which must be unique between nodes.
350    /// Returns `true` if the block was successfully submitted, `false` otherwise.
351    #[method(name = "submitHashrate")]
352    async fn submit_hashrate(&self, hashrate: U256, id: B256) -> RpcResult<bool>;
353
354    /// Used for submitting a proof-of-work solution.
355    #[method(name = "submitWork")]
356    async fn submit_work(&self, nonce: B64, pow_hash: B256, mix_digest: B256) -> RpcResult<bool>;
357
358    /// Sends transaction; will block waiting for signer to return the
359    /// transaction hash.
360    #[method(name = "sendTransaction")]
361    async fn send_transaction(&self, request: TxReq) -> RpcResult<B256>;
362
363    /// Sends signed transaction, returning its hash.
364    #[method(name = "sendRawTransaction")]
365    async fn send_raw_transaction(&self, bytes: Bytes) -> RpcResult<B256>;
366
367    /// Sends a signed transaction and awaits the transaction receipt.
368    ///
369    /// This will return a timeout error if the transaction isn't included within some time period.
370    #[method(name = "sendRawTransactionSync")]
371    async fn send_raw_transaction_sync(&self, bytes: Bytes) -> RpcResult<R>;
372
373    /// Returns an Ethereum specific signature with: sign(keccak256("\x19Ethereum Signed Message:\n"
374    /// + len(message) + message))).
375    #[method(name = "sign")]
376    async fn sign(&self, address: Address, message: Bytes) -> RpcResult<Bytes>;
377
378    /// Signs a transaction that can be submitted to the network at a later time using with
379    /// `sendRawTransaction.`
380    #[method(name = "signTransaction")]
381    async fn sign_transaction(&self, transaction: TxReq) -> RpcResult<Bytes>;
382
383    /// Signs data via [EIP-712](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md).
384    #[method(name = "signTypedData")]
385    async fn sign_typed_data(&self, address: Address, data: TypedData) -> RpcResult<Bytes>;
386
387    /// Returns the account and storage values of the specified account including the Merkle-proof.
388    /// This call can be used to verify that the data you are pulling from is not tampered with.
389    #[method(name = "getProof")]
390    async fn get_proof(
391        &self,
392        address: Address,
393        keys: Vec<JsonStorageKey>,
394        block_number: Option<BlockId>,
395    ) -> RpcResult<EIP1186AccountProofResponse>;
396
397    /// Returns the account's balance, nonce, and code.
398    ///
399    /// This is similar to `eth_getAccount` but does not return the storage root.
400    #[method(name = "getAccountInfo")]
401    async fn get_account_info(
402        &self,
403        address: Address,
404        block: BlockId,
405    ) -> RpcResult<alloy_rpc_types_eth::AccountInfo>;
406
407    /// Returns the EIP-7928 block access list for a block by hash.
408    #[method(name = "getBlockAccessListByBlockHash")]
409    async fn block_access_list_by_block_hash(&self, hash: B256) -> RpcResult<Option<Bytes>>;
410
411    /// Returns the EIP-7928 block access list for a block by number.
412    #[method(name = "getBlockAccessListByBlockNumber")]
413    async fn block_access_list_by_block_number(
414        &self,
415        number: BlockNumberOrTag,
416    ) -> RpcResult<Option<Bytes>>;
417}
418
419#[async_trait::async_trait]
420impl<T>
421    EthApiServer<
422        RpcTxReq<T::NetworkTypes>,
423        RpcTransaction<T::NetworkTypes>,
424        RpcBlock<T::NetworkTypes>,
425        RpcReceipt<T::NetworkTypes>,
426        RpcHeader<T::NetworkTypes>,
427        TxTy<T::Primitives>,
428    > for T
429where
430    T: FullEthApi,
431    jsonrpsee_types::error::ErrorObject<'static>: From<T::Error>,
432{
433    /// Handler for: `eth_protocolVersion`
434    async fn protocol_version(&self) -> RpcResult<U64> {
435        trace!(target: "rpc::eth", "Serving eth_protocolVersion");
436        EthApiSpec::protocol_version(self).await.to_rpc_result()
437    }
438
439    /// Handler for: `eth_syncing`
440    fn syncing(&self) -> RpcResult<SyncStatus> {
441        trace!(target: "rpc::eth", "Serving eth_syncing");
442        EthApiSpec::sync_status(self).to_rpc_result()
443    }
444
445    /// Handler for: `eth_coinbase`
446    async fn author(&self) -> RpcResult<Address> {
447        Err(internal_rpc_err("unimplemented"))
448    }
449
450    /// Handler for: `eth_accounts`
451    fn accounts(&self) -> RpcResult<Vec<Address>> {
452        trace!(target: "rpc::eth", "Serving eth_accounts");
453        Ok(EthTransactions::accounts(self))
454    }
455
456    /// Handler for: `eth_blockNumber`
457    fn block_number(&self) -> RpcResult<U256> {
458        trace!(target: "rpc::eth", "Serving eth_blockNumber");
459        Ok(U256::from(
460            EthApiSpec::chain_info(self).with_message("failed to read chain info")?.best_number,
461        ))
462    }
463
464    /// Handler for: `eth_chainId`
465    async fn chain_id(&self) -> RpcResult<Option<U64>> {
466        trace!(target: "rpc::eth", "Serving eth_chainId");
467        Ok(Some(EthApiSpec::chain_id(self)))
468    }
469
470    /// Handler for: `eth_getBlockByHash`
471    async fn block_by_hash(
472        &self,
473        hash: B256,
474        full: bool,
475    ) -> RpcResult<Option<RpcBlock<T::NetworkTypes>>> {
476        trace!(target: "rpc::eth", ?hash, ?full, "Serving eth_getBlockByHash");
477        Ok(EthBlocks::rpc_block(self, hash.into(), full).await?)
478    }
479
480    /// Handler for: `eth_getBlockByNumber`
481    async fn block_by_number(
482        &self,
483        number: BlockNumberOrTag,
484        full: bool,
485    ) -> RpcResult<Option<RpcBlock<T::NetworkTypes>>> {
486        trace!(target: "rpc::eth", ?number, ?full, "Serving eth_getBlockByNumber");
487        Ok(EthBlocks::rpc_block(self, number.into(), full).await?)
488    }
489
490    /// Handler for: `eth_getBlockTransactionCountByHash`
491    async fn block_transaction_count_by_hash(&self, hash: B256) -> RpcResult<Option<U256>> {
492        trace!(target: "rpc::eth", ?hash, "Serving eth_getBlockTransactionCountByHash");
493        Ok(EthBlocks::block_transaction_count(self, hash.into()).await?.map(U256::from))
494    }
495
496    /// Handler for: `eth_getBlockTransactionCountByNumber`
497    async fn block_transaction_count_by_number(
498        &self,
499        number: BlockNumberOrTag,
500    ) -> RpcResult<Option<U256>> {
501        trace!(target: "rpc::eth", ?number, "Serving eth_getBlockTransactionCountByNumber");
502        Ok(EthBlocks::block_transaction_count(self, number.into()).await?.map(U256::from))
503    }
504
505    /// Handler for: `eth_getUncleCountByBlockHash`
506    async fn block_uncles_count_by_hash(&self, hash: B256) -> RpcResult<Option<U256>> {
507        trace!(target: "rpc::eth", ?hash, "Serving eth_getUncleCountByBlockHash");
508
509        if let Some(block) = self.block_by_hash(hash, false).await? {
510            Ok(Some(U256::from(block.uncles.len())))
511        } else {
512            Ok(None)
513        }
514    }
515
516    /// Handler for: `eth_getUncleCountByBlockNumber`
517    async fn block_uncles_count_by_number(
518        &self,
519        number: BlockNumberOrTag,
520    ) -> RpcResult<Option<U256>> {
521        trace!(target: "rpc::eth", ?number, "Serving eth_getUncleCountByBlockNumber");
522
523        if let Some(block) = self.block_by_number(number, false).await? {
524            Ok(Some(U256::from(block.uncles.len())))
525        } else {
526            Ok(None)
527        }
528    }
529
530    /// Handler for: `eth_getBlockReceipts`
531    async fn block_receipts(
532        &self,
533        block_id: BlockId,
534    ) -> RpcResult<Option<Vec<RpcReceipt<T::NetworkTypes>>>> {
535        trace!(target: "rpc::eth", ?block_id, "Serving eth_getBlockReceipts");
536        Ok(EthBlocks::block_receipts(self, block_id).await?)
537    }
538
539    /// Handler for: `eth_getUncleByBlockHashAndIndex`
540    async fn uncle_by_block_hash_and_index(
541        &self,
542        hash: B256,
543        index: Index,
544    ) -> RpcResult<Option<RpcBlock<T::NetworkTypes>>> {
545        trace!(target: "rpc::eth", ?hash, ?index, "Serving eth_getUncleByBlockHashAndIndex");
546        Ok(EthBlocks::ommer_by_block_and_index(self, hash.into(), index).await?)
547    }
548
549    /// Handler for: `eth_getUncleByBlockNumberAndIndex`
550    async fn uncle_by_block_number_and_index(
551        &self,
552        number: BlockNumberOrTag,
553        index: Index,
554    ) -> RpcResult<Option<RpcBlock<T::NetworkTypes>>> {
555        trace!(target: "rpc::eth", ?number, ?index, "Serving eth_getUncleByBlockNumberAndIndex");
556        Ok(EthBlocks::ommer_by_block_and_index(self, number.into(), index).await?)
557    }
558
559    /// Handler for: `eth_getRawTransactionByHash`
560    async fn raw_transaction_by_hash(&self, hash: B256) -> RpcResult<Option<Bytes>> {
561        trace!(target: "rpc::eth", ?hash, "Serving eth_getRawTransactionByHash");
562        Ok(EthTransactions::raw_transaction_by_hash(self, hash).await?)
563    }
564
565    /// Handler for: `eth_getTransactionByHash`
566    async fn transaction_by_hash(
567        &self,
568        hash: B256,
569    ) -> RpcResult<Option<RpcTransaction<T::NetworkTypes>>> {
570        trace!(target: "rpc::eth", ?hash, "Serving eth_getTransactionByHash");
571        Ok(EthTransactions::transaction_by_hash(self, hash)
572            .await?
573            .map(|tx| tx.into_transaction(self.converter()))
574            .transpose()
575            .map_err(T::Error::from)?)
576    }
577
578    /// Handler for: `eth_getRawTransactionByBlockHashAndIndex`
579    async fn raw_transaction_by_block_hash_and_index(
580        &self,
581        hash: B256,
582        index: Index,
583    ) -> RpcResult<Option<Bytes>> {
584        trace!(target: "rpc::eth", ?hash, ?index, "Serving eth_getRawTransactionByBlockHashAndIndex");
585        Ok(EthTransactions::raw_transaction_by_block_and_tx_index(self, hash.into(), index.into())
586            .await?)
587    }
588
589    /// Handler for: `eth_getTransactionByBlockHashAndIndex`
590    async fn transaction_by_block_hash_and_index(
591        &self,
592        hash: B256,
593        index: Index,
594    ) -> RpcResult<Option<RpcTransaction<T::NetworkTypes>>> {
595        trace!(target: "rpc::eth", ?hash, ?index, "Serving eth_getTransactionByBlockHashAndIndex");
596        Ok(EthTransactions::transaction_by_block_and_tx_index(self, hash.into(), index.into())
597            .await?)
598    }
599
600    /// Handler for: `eth_getRawTransactionByBlockNumberAndIndex`
601    async fn raw_transaction_by_block_number_and_index(
602        &self,
603        number: BlockNumberOrTag,
604        index: Index,
605    ) -> RpcResult<Option<Bytes>> {
606        trace!(target: "rpc::eth", ?number, ?index, "Serving eth_getRawTransactionByBlockNumberAndIndex");
607        Ok(EthTransactions::raw_transaction_by_block_and_tx_index(
608            self,
609            number.into(),
610            index.into(),
611        )
612        .await?)
613    }
614
615    /// Handler for: `eth_getTransactionByBlockNumberAndIndex`
616    async fn transaction_by_block_number_and_index(
617        &self,
618        number: BlockNumberOrTag,
619        index: Index,
620    ) -> RpcResult<Option<RpcTransaction<T::NetworkTypes>>> {
621        trace!(target: "rpc::eth", ?number, ?index, "Serving eth_getTransactionByBlockNumberAndIndex");
622        Ok(EthTransactions::transaction_by_block_and_tx_index(self, number.into(), index.into())
623            .await?)
624    }
625
626    /// Handler for: `eth_getTransactionBySenderAndNonce`
627    async fn transaction_by_sender_and_nonce(
628        &self,
629        sender: Address,
630        nonce: U64,
631    ) -> RpcResult<Option<RpcTransaction<T::NetworkTypes>>> {
632        trace!(target: "rpc::eth", ?sender, ?nonce, "Serving eth_getTransactionBySenderAndNonce");
633        Ok(EthTransactions::get_transaction_by_sender_and_nonce(self, sender, nonce.to(), true)
634            .await?)
635    }
636
637    /// Handler for: `eth_getTransactionReceipt`
638    async fn transaction_receipt(
639        &self,
640        hash: B256,
641    ) -> RpcResult<Option<RpcReceipt<T::NetworkTypes>>> {
642        trace!(target: "rpc::eth", ?hash, "Serving eth_getTransactionReceipt");
643        Ok(EthTransactions::transaction_receipt(self, hash).await?)
644    }
645
646    /// Handler for: `eth_getBalance`
647    async fn balance(&self, address: Address, block_number: Option<BlockId>) -> RpcResult<U256> {
648        trace!(target: "rpc::eth", ?address, ?block_number, "Serving eth_getBalance");
649        Ok(EthState::balance(self, address, block_number).await?)
650    }
651
652    /// Handler for: `eth_getStorageAt`
653    async fn storage_at(
654        &self,
655        address: Address,
656        index: JsonStorageKey,
657        block_number: Option<BlockId>,
658    ) -> RpcResult<B256> {
659        trace!(target: "rpc::eth", ?address, ?block_number, "Serving eth_getStorageAt");
660        Ok(EthState::storage_at(self, address, index, block_number).await?)
661    }
662
663    /// Handler for: `eth_getStorageValues`
664    async fn storage_values(
665        &self,
666        requests: HashMap<Address, Vec<JsonStorageKey>>,
667        block_number: Option<BlockId>,
668    ) -> RpcResult<HashMap<Address, Vec<B256>>> {
669        trace!(target: "rpc::eth", ?block_number, "Serving eth_getStorageValues");
670        Ok(EthState::storage_values(self, requests, block_number).await?)
671    }
672
673    /// Handler for: `eth_getTransactionCount`
674    async fn transaction_count(
675        &self,
676        address: Address,
677        block_number: Option<BlockId>,
678    ) -> RpcResult<U256> {
679        trace!(target: "rpc::eth", ?address, ?block_number, "Serving eth_getTransactionCount");
680        Ok(EthState::transaction_count(self, address, block_number).await?)
681    }
682
683    /// Handler for: `eth_getCode`
684    async fn get_code(&self, address: Address, block_number: Option<BlockId>) -> RpcResult<Bytes> {
685        trace!(target: "rpc::eth", ?address, ?block_number, "Serving eth_getCode");
686        Ok(EthState::get_code(self, address, block_number).await?)
687    }
688
689    /// Handler for: `eth_getHeaderByNumber`
690    async fn header_by_number(
691        &self,
692        block_number: BlockNumberOrTag,
693    ) -> RpcResult<Option<RpcHeader<T::NetworkTypes>>> {
694        trace!(target: "rpc::eth", ?block_number, "Serving eth_getHeaderByNumber");
695        Ok(EthBlocks::rpc_block_header(self, block_number.into()).await?)
696    }
697
698    /// Handler for: `eth_getHeaderByHash`
699    async fn header_by_hash(&self, hash: B256) -> RpcResult<Option<RpcHeader<T::NetworkTypes>>> {
700        trace!(target: "rpc::eth", ?hash, "Serving eth_getHeaderByHash");
701        Ok(EthBlocks::rpc_block_header(self, hash.into()).await?)
702    }
703
704    /// Handler for: `eth_simulateV1`
705    async fn simulate_v1(
706        &self,
707        payload: SimulatePayload<RpcTxReq<T::NetworkTypes>>,
708        block_number: Option<BlockId>,
709    ) -> RpcResult<Vec<SimulatedBlock<RpcBlock<T::NetworkTypes>>>> {
710        trace!(target: "rpc::eth", ?block_number, "Serving eth_simulateV1");
711        let _permit = self.tracing_task_guard().clone().acquire_owned().await;
712        Ok(EthCall::simulate_v1(self, payload, block_number).await?)
713    }
714
715    /// Handler for: `eth_call`
716    async fn call(
717        &self,
718        request: RpcTxReq<T::NetworkTypes>,
719        block_number: Option<BlockId>,
720        state_overrides: Option<StateOverride>,
721        block_overrides: Option<Box<BlockOverrides>>,
722    ) -> RpcResult<Bytes> {
723        trace!(target: "rpc::eth", ?request, ?block_number, ?state_overrides, ?block_overrides, "Serving eth_call");
724        Ok(EthCall::call(
725            self,
726            request,
727            block_number,
728            EvmOverrides::new(state_overrides, block_overrides),
729        )
730        .await?)
731    }
732
733    /// Handler for: `eth_fillTransaction`
734    async fn fill_transaction(
735        &self,
736        request: RpcTxReq<T::NetworkTypes>,
737    ) -> RpcResult<FillTransaction<TxTy<T::Primitives>>> {
738        trace!(target: "rpc::eth", ?request, "Serving eth_fillTransaction");
739        Ok(EthTransactions::fill_transaction(self, request).await?)
740    }
741
742    /// Handler for: `eth_callMany`
743    async fn call_many(
744        &self,
745        bundles: Vec<Bundle<RpcTxReq<T::NetworkTypes>>>,
746        state_context: Option<StateContext>,
747        state_override: Option<StateOverride>,
748    ) -> RpcResult<Vec<Vec<EthCallResponse>>> {
749        trace!(target: "rpc::eth", ?bundles, ?state_context, ?state_override, "Serving eth_callMany");
750        Ok(EthCall::call_many(self, bundles, state_context, state_override).await?)
751    }
752
753    /// Handler for: `eth_createAccessList`
754    async fn create_access_list(
755        &self,
756        request: RpcTxReq<T::NetworkTypes>,
757        block_number: Option<BlockId>,
758        state_override: Option<StateOverride>,
759    ) -> RpcResult<AccessListResult> {
760        trace!(target: "rpc::eth", ?request, ?block_number, ?state_override, "Serving eth_createAccessList");
761        Ok(EthCall::create_access_list_at(self, request, block_number, state_override).await?)
762    }
763
764    /// Handler for: `eth_estimateGas`
765    async fn estimate_gas(
766        &self,
767        request: RpcTxReq<T::NetworkTypes>,
768        block_number: Option<BlockId>,
769        state_override: Option<StateOverride>,
770    ) -> RpcResult<U256> {
771        trace!(target: "rpc::eth", ?request, ?block_number, "Serving eth_estimateGas");
772        Ok(EthCall::estimate_gas_at(
773            self,
774            request,
775            block_number.unwrap_or_default(),
776            state_override,
777        )
778        .await?)
779    }
780
781    /// Handler for: `eth_gasPrice`
782    async fn gas_price(&self) -> RpcResult<U256> {
783        trace!(target: "rpc::eth", "Serving eth_gasPrice");
784        Ok(EthFees::gas_price(self).await?)
785    }
786
787    /// Handler for: `eth_getAccount`
788    async fn get_account(
789        &self,
790        address: Address,
791        block: BlockId,
792    ) -> RpcResult<Option<alloy_rpc_types_eth::Account>> {
793        trace!(target: "rpc::eth", "Serving eth_getAccount");
794        Ok(EthState::get_account(self, address, block).await?)
795    }
796
797    /// Handler for: `eth_maxPriorityFeePerGas`
798    async fn max_priority_fee_per_gas(&self) -> RpcResult<U256> {
799        trace!(target: "rpc::eth", "Serving eth_maxPriorityFeePerGas");
800        Ok(EthFees::suggested_priority_fee(self).await?)
801    }
802
803    /// Handler for: `eth_blobBaseFee`
804    async fn blob_base_fee(&self) -> RpcResult<U256> {
805        trace!(target: "rpc::eth", "Serving eth_blobBaseFee");
806        Ok(EthFees::blob_base_fee(self).await?)
807    }
808
809    // FeeHistory is calculated based on lazy evaluation of fees for historical blocks, and further
810    // caching of it in the LRU cache.
811    // When new RPC call is executed, the cache gets locked, we check it for the historical fees
812    // according to the requested block range, and fill any cache misses (in both RPC response
813    // and cache itself) with the actual data queried from the database.
814    // To minimize the number of database seeks required to query the missing data, we calculate the
815    // first non-cached block number and last non-cached block number. After that, we query this
816    // range of consecutive blocks from the database.
817    /// Handler for: `eth_feeHistory`
818    async fn fee_history(
819        &self,
820        block_count: U64,
821        newest_block: BlockNumberOrTag,
822        reward_percentiles: Option<Vec<f64>>,
823    ) -> RpcResult<FeeHistory> {
824        trace!(target: "rpc::eth", ?block_count, ?newest_block, ?reward_percentiles, "Serving eth_feeHistory");
825        Ok(EthFees::fee_history(self, block_count.to(), newest_block, reward_percentiles).await?)
826    }
827
828    /// Handler for: `eth_mining`
829    async fn is_mining(&self) -> RpcResult<bool> {
830        Err(internal_rpc_err("unimplemented"))
831    }
832
833    /// Handler for: `eth_hashrate`
834    async fn hashrate(&self) -> RpcResult<U256> {
835        Ok(U256::ZERO)
836    }
837
838    /// Handler for: `eth_getWork`
839    async fn get_work(&self) -> RpcResult<Work> {
840        Err(internal_rpc_err("unimplemented"))
841    }
842
843    /// Handler for: `eth_submitHashrate`
844    async fn submit_hashrate(&self, _hashrate: U256, _id: B256) -> RpcResult<bool> {
845        Ok(false)
846    }
847
848    /// Handler for: `eth_submitWork`
849    async fn submit_work(
850        &self,
851        _nonce: B64,
852        _pow_hash: B256,
853        _mix_digest: B256,
854    ) -> RpcResult<bool> {
855        Err(internal_rpc_err("unimplemented"))
856    }
857
858    /// Handler for: `eth_sendTransaction`
859    async fn send_transaction(&self, request: RpcTxReq<T::NetworkTypes>) -> RpcResult<B256> {
860        trace!(target: "rpc::eth", ?request, "Serving eth_sendTransaction");
861        Ok(EthTransactions::send_transaction_request(self, request).await?)
862    }
863
864    /// Handler for: `eth_sendRawTransaction`
865    async fn send_raw_transaction(&self, tx: Bytes) -> RpcResult<B256> {
866        trace!(target: "rpc::eth", ?tx, "Serving eth_sendRawTransaction");
867        Ok(EthTransactions::send_raw_transaction(self, tx).await?)
868    }
869
870    /// Handler for: `eth_sendRawTransactionSync`
871    async fn send_raw_transaction_sync(&self, tx: Bytes) -> RpcResult<RpcReceipt<T::NetworkTypes>> {
872        trace!(target: "rpc::eth", ?tx, "Serving eth_sendRawTransactionSync");
873        Ok(EthTransactions::send_raw_transaction_sync(self, tx).await?)
874    }
875
876    /// Handler for: `eth_sign`
877    async fn sign(&self, address: Address, message: Bytes) -> RpcResult<Bytes> {
878        trace!(target: "rpc::eth", ?address, ?message, "Serving eth_sign");
879        Ok(EthTransactions::sign(self, address, message).await?)
880    }
881
882    /// Handler for: `eth_signTransaction`
883    async fn sign_transaction(&self, request: RpcTxReq<T::NetworkTypes>) -> RpcResult<Bytes> {
884        trace!(target: "rpc::eth", ?request, "Serving eth_signTransaction");
885        Ok(EthTransactions::sign_transaction(self, request).await?)
886    }
887
888    /// Handler for: `eth_signTypedData`
889    async fn sign_typed_data(&self, address: Address, data: TypedData) -> RpcResult<Bytes> {
890        trace!(target: "rpc::eth", ?address, ?data, "Serving eth_signTypedData");
891        Ok(EthTransactions::sign_typed_data(self, &data, address)?)
892    }
893
894    /// Handler for: `eth_getProof`
895    async fn get_proof(
896        &self,
897        address: Address,
898        keys: Vec<JsonStorageKey>,
899        block_number: Option<BlockId>,
900    ) -> RpcResult<EIP1186AccountProofResponse> {
901        trace!(target: "rpc::eth", ?address, ?keys, ?block_number, "Serving eth_getProof");
902        Ok(EthState::get_proof(self, address, keys, block_number)?.await?)
903    }
904
905    /// Handler for: `eth_getAccountInfo`
906    async fn get_account_info(
907        &self,
908        address: Address,
909        block: BlockId,
910    ) -> RpcResult<alloy_rpc_types_eth::AccountInfo> {
911        trace!(target: "rpc::eth", "Serving eth_getAccountInfo");
912        Ok(EthState::get_account_info(self, address, block).await?)
913    }
914
915    /// Handler for: `eth_getBlockAccessListByBlockHash`
916    async fn block_access_list_by_block_hash(&self, hash: B256) -> RpcResult<Option<Bytes>> {
917        trace!(target: "rpc::eth", ?hash, "Serving eth_getBlockAccessListByBlockHash");
918        Err(internal_rpc_err("unimplemented"))
919    }
920
921    /// Handler for: `eth_getBlockAccessListByBlockNumber`
922    async fn block_access_list_by_block_number(
923        &self,
924        number: BlockNumberOrTag,
925    ) -> RpcResult<Option<Bytes>> {
926        trace!(target: "rpc::eth", ?number, "Serving eth_getBlockAccessListByBlockNumber");
927        Err(internal_rpc_err("unimplemented"))
928    }
929}