alloy_network/
lib.rs

1#![doc = include_str!("../README.md")]
2#![doc(
3    html_logo_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/alloy.jpg",
4    html_favicon_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/favicon.ico"
5)]
6#![cfg_attr(not(test), warn(unused_crate_dependencies))]
7#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
8
9use alloy_consensus::{BlockHeader, TxReceipt};
10use alloy_eips::eip2718::{Eip2718Envelope, Eip2718Error};
11use alloy_json_rpc::RpcObject;
12use alloy_network_primitives::HeaderResponse;
13use core::fmt::{Debug, Display};
14
15mod transaction;
16pub use transaction::{
17    BuildResult, FullSigner, FullSignerSync, NetworkWallet, TransactionBuilder,
18    TransactionBuilder4844, TransactionBuilder7702, TransactionBuilderError, TxSigner,
19    TxSignerSync, UnbuiltTransactionError,
20};
21
22mod ethereum;
23pub use ethereum::{Ethereum, EthereumWallet, IntoWallet};
24
25/// Types for handling unknown network types.
26pub mod any;
27pub use any::{
28    AnyHeader, AnyNetwork, AnyReceiptEnvelope, AnyRpcBlock, AnyRpcHeader, AnyRpcTransaction,
29    AnyTransactionReceipt, AnyTxEnvelope, AnyTxType, AnyTypedTransaction, UnknownTxEnvelope,
30    UnknownTypedTransaction,
31};
32
33pub use alloy_eips::eip2718;
34use alloy_eips::Typed2718;
35pub use alloy_network_primitives::{
36    self as primitives, BlockResponse, ReceiptResponse, TransactionResponse,
37};
38
39/// Captures type info for network-specific RPC requests/responses.
40///
41/// Networks are only containers for types, so it is recommended to use ZSTs for their definition.
42// todo: block responses are ethereum only, so we need to include this in here too, or make `Block`
43// generic over tx/header type
44pub trait Network: Debug + Clone + Copy + Sized + Send + Sync + 'static {
45    // -- Consensus types --
46
47    /// The network transaction type enum.
48    ///
49    /// This should be a simple `#[repr(u8)]` enum, and as such has strict type
50    /// bounds for better use in error messages, assertions etc.
51    #[doc(alias = "TransactionType")]
52    type TxType: Typed2718
53        + Into<u8>
54        + PartialEq
55        + Eq
56        + TryFrom<u8, Error = Eip2718Error>
57        + Debug
58        + Display
59        + Clone
60        + Copy
61        + Send
62        + Sync
63        + 'static;
64
65    /// The network transaction envelope type.
66    #[doc(alias = "TransactionEnvelope")]
67    type TxEnvelope: Eip2718Envelope + Debug;
68
69    /// An enum over the various transaction types.
70    #[doc(alias = "UnsignedTransaction")]
71    type UnsignedTx: From<Self::TxEnvelope>;
72
73    /// The network receipt envelope type.
74    #[doc(alias = "TransactionReceiptEnvelope", alias = "TxReceiptEnvelope")]
75    type ReceiptEnvelope: Eip2718Envelope + TxReceipt;
76
77    /// The network header type.
78    type Header: BlockHeader;
79
80    // -- JSON RPC types --
81
82    /// The JSON body of a transaction request.
83    #[doc(alias = "TxRequest")]
84    type TransactionRequest: RpcObject
85        + TransactionBuilder<Self>
86        + Debug
87        + From<Self::TxEnvelope>
88        + From<Self::UnsignedTx>;
89
90    /// The JSON body of a transaction response.
91    #[doc(alias = "TxResponse")]
92    type TransactionResponse: RpcObject + TransactionResponse + AsRef<Self::TxEnvelope>;
93
94    /// The JSON body of a transaction receipt.
95    #[doc(alias = "TransactionReceiptResponse", alias = "TxReceiptResponse")]
96    type ReceiptResponse: RpcObject + ReceiptResponse;
97
98    /// The JSON body of a header response.
99    type HeaderResponse: RpcObject + HeaderResponse + AsRef<Self::Header>;
100
101    /// The JSON body of a block response.
102    type BlockResponse: RpcObject
103        + BlockResponse<Transaction = Self::TransactionResponse, Header = Self::HeaderResponse>;
104}
105
106/// Utility to implement IntoWallet for signer over the specified network.
107#[macro_export]
108macro_rules! impl_into_wallet {
109    ($(@[$($generics:tt)*])? $signer:ty) => {
110        impl $(<$($generics)*>)? $crate::IntoWallet for $signer {
111            type NetworkWallet = $crate::EthereumWallet;
112            fn into_wallet(self) -> Self::NetworkWallet {
113                $crate::EthereumWallet::from(self)
114            }
115        }
116
117        impl $(<$($generics)*>)? $crate::IntoWallet<$crate::AnyNetwork> for $signer {
118            type NetworkWallet = $crate::EthereumWallet;
119            fn into_wallet(self) -> Self::NetworkWallet {
120                $crate::EthereumWallet::from(self)
121            }
122        }
123    };
124}