reth_rpc_eth_api/
core.rs

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