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