reth_rpc_eth_api/
types.rs

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