reth_rpc_eth_api/
types.rs

1//! Trait for specifying `eth` network dependent API types.
2
3use crate::{AsEthApiError, FromEthApiError, RpcNodeCore};
4use alloy_rpc_types_eth::Block;
5use reth_chain_state::CanonStateSubscriptions;
6use reth_rpc_convert::RpcConvert;
7pub use reth_rpc_convert::{RpcTransaction, RpcTxReq, RpcTypes};
8use reth_storage_api::{ProviderTx, ReceiptProvider, TransactionsProvider};
9use reth_transaction_pool::{PoolTransaction, TransactionPool};
10use std::{
11    error::Error,
12    fmt::{self},
13};
14
15/// Network specific `eth` API types.
16///
17/// This trait defines the network specific rpc types and helpers required for the `eth_` and
18/// adjacent endpoints. `NetworkTypes` is [`alloy_network::Network`] as defined by the alloy crate,
19/// see also [`alloy_network::Ethereum`].
20///
21/// This type is stateful so that it can provide additional context if necessary, e.g. populating
22/// receipts with additional data.
23pub trait EthApiTypes: Send + Sync + Clone {
24    /// Extension of [`FromEthApiError`], with network specific errors.
25    type Error: Into<jsonrpsee_types::error::ErrorObject<'static>>
26        + FromEthApiError
27        + AsEthApiError
28        + Error
29        + Send
30        + Sync;
31    /// Blockchain primitive types, specific to network, e.g. block and transaction.
32    type NetworkTypes: RpcTypes;
33    /// Conversion methods for transaction RPC type.
34    type RpcConvert: Send + Sync + Clone + fmt::Debug;
35
36    /// Returns reference to transaction response builder.
37    fn tx_resp_builder(&self) -> &Self::RpcConvert;
38}
39
40/// Adapter for network specific block type.
41pub type RpcBlock<T> = Block<RpcTransaction<T>, RpcHeader<T>>;
42
43/// Adapter for network specific receipt type.
44pub type RpcReceipt<T> = <T as RpcTypes>::Receipt;
45
46/// Adapter for network specific header type.
47pub type RpcHeader<T> = <T as RpcTypes>::Header;
48
49/// Adapter for network specific error type.
50pub type RpcError<T> = <T as EthApiTypes>::Error;
51
52/// Helper trait holds necessary trait bounds on [`EthApiTypes`] to implement `eth` API.
53pub trait FullEthApiTypes
54where
55    Self: RpcNodeCore<
56            Provider: TransactionsProvider + ReceiptProvider + CanonStateSubscriptions,
57            Pool: TransactionPool<
58                Transaction: PoolTransaction<Consensus = ProviderTx<Self::Provider>>,
59            >,
60        > + EthApiTypes<
61            RpcConvert: RpcConvert<
62                Primitives = Self::Primitives,
63                Network = Self::NetworkTypes,
64                Error = RpcError<Self>,
65            >,
66        >,
67{
68}
69
70impl<T> FullEthApiTypes for T where
71    T: RpcNodeCore<
72            Provider: TransactionsProvider + ReceiptProvider + CanonStateSubscriptions,
73            Pool: TransactionPool<
74                Transaction: PoolTransaction<Consensus = ProviderTx<Self::Provider>>,
75            >,
76        > + EthApiTypes<
77            RpcConvert: RpcConvert<
78                Primitives = <Self as RpcNodeCore>::Primitives,
79                Network = Self::NetworkTypes,
80                Error = RpcError<T>,
81            >,
82        >
83{
84}