Skip to main content

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_rpc_convert::{RpcConvert, SignableTxRequest};
6pub use reth_rpc_convert::{RpcTransaction, RpcTxReq, RpcTypes};
7use reth_rpc_eth_types::EthApiSettings;
8use reth_storage_api::ProviderTx;
9use std::error::Error;
10
11/// Network specific `eth` API types.
12///
13/// This trait defines the network specific rpc types and helpers required for the `eth_` and
14/// adjacent endpoints. `NetworkTypes` is [`alloy_network::Network`] as defined by the alloy crate,
15/// see also [`alloy_network::Ethereum`].
16///
17/// This type is stateful so that it can provide additional context if necessary, e.g. populating
18/// receipts with additional data.
19pub trait EthApiTypes: Send + Sync + Clone {
20    /// Extension of [`FromEthApiError`], with network specific errors.
21    type Error: Into<jsonrpsee_types::error::ErrorObject<'static>>
22        + FromEthApiError
23        + AsEthApiError
24        + From<<Self::RpcConvert as RpcConvert>::Error>
25        + Error
26        + Send
27        + Sync;
28    /// Blockchain primitive types, specific to network, e.g. block and transaction.
29    type NetworkTypes: RpcTypes;
30    /// Conversion methods for transaction RPC type.
31    type RpcConvert: RpcConvert<Network = Self::NetworkTypes>;
32
33    /// Returns the settings shared by the `eth` RPC helpers.
34    fn eth_api_settings(&self) -> &EthApiSettings;
35
36    /// Returns reference to transaction response builder.
37    fn converter(&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        + EthApiTypes<
57            NetworkTypes: RpcTypes<
58                TransactionRequest: SignableTxRequest<ProviderTx<Self::Provider>>,
59            >,
60            RpcConvert: RpcConvert<Primitives = Self::Primitives>,
61        >,
62{
63}
64
65impl<T> FullEthApiTypes for T where
66    T: RpcNodeCore
67        + EthApiTypes<
68            NetworkTypes: RpcTypes<
69                TransactionRequest: SignableTxRequest<ProviderTx<Self::Provider>>,
70            >,
71            RpcConvert: RpcConvert<Primitives = Self::Primitives>,
72        >
73{
74}