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;
34pub use alloy_network_primitives::{
35    self as primitives, BlockResponse, ReceiptResponse, TransactionResponse,
36};
37
38/// Captures type info for network-specific RPC requests/responses.
39///
40/// Networks are only containers for types, so it is recommended to use ZSTs for their definition.
41// todo: block responses are ethereum only, so we need to include this in here too, or make `Block`
42// generic over tx/header type
43pub trait Network: Debug + Clone + Copy + Sized + Send + Sync + 'static {
44    // -- Consensus types --
45
46    /// The network transaction type enum.
47    ///
48    /// This should be a simple `#[repr(u8)]` enum, and as such has strict type
49    /// bounds for better use in error messages, assertions etc.
50    #[doc(alias = "TransactionType")]
51    type TxType: Into<u8>
52        + PartialEq
53        + Eq
54        + TryFrom<u8, Error = Eip2718Error>
55        + Debug
56        + Display
57        + Clone
58        + Copy
59        + Send
60        + Sync
61        + 'static;
62
63    /// The network transaction envelope type.
64    #[doc(alias = "TransactionEnvelope")]
65    type TxEnvelope: Eip2718Envelope + Debug;
66
67    /// An enum over the various transaction types.
68    #[doc(alias = "UnsignedTransaction")]
69    type UnsignedTx: From<Self::TxEnvelope>;
70
71    /// The network receipt envelope type.
72    #[doc(alias = "TransactionReceiptEnvelope", alias = "TxReceiptEnvelope")]
73    type ReceiptEnvelope: Eip2718Envelope + TxReceipt;
74
75    /// The network header type.
76    type Header: BlockHeader;
77
78    // -- JSON RPC types --
79
80    /// The JSON body of a transaction request.
81    #[doc(alias = "TxRequest")]
82    type TransactionRequest: RpcObject
83        + TransactionBuilder<Self>
84        + Debug
85        + From<Self::TxEnvelope>
86        + From<Self::UnsignedTx>;
87
88    /// The JSON body of a transaction response.
89    #[doc(alias = "TxResponse")]
90    type TransactionResponse: RpcObject + TransactionResponse + AsRef<Self::TxEnvelope>;
91
92    /// The JSON body of a transaction receipt.
93    #[doc(alias = "TransactionReceiptResponse", alias = "TxReceiptResponse")]
94    type ReceiptResponse: RpcObject + ReceiptResponse;
95
96    /// The JSON body of a header response.
97    type HeaderResponse: RpcObject + HeaderResponse + AsRef<Self::Header>;
98
99    /// The JSON body of a block response.
100    type BlockResponse: RpcObject
101        + BlockResponse<Transaction = Self::TransactionResponse, Header = Self::HeaderResponse>;
102}
103
104/// Utility to implement IntoWallet for signer over the specified network.
105#[macro_export]
106macro_rules! impl_into_wallet {
107    ($(@[$($generics:tt)*])? $signer:ty) => {
108        impl $(<$($generics)*>)? $crate::IntoWallet for $signer {
109            type NetworkWallet = $crate::EthereumWallet;
110            fn into_wallet(self) -> Self::NetworkWallet {
111                $crate::EthereumWallet::from(self)
112            }
113        }
114
115        impl $(<$($generics)*>)? $crate::IntoWallet<$crate::AnyNetwork> for $signer {
116            type NetworkWallet = $crate::EthereumWallet;
117            fn into_wallet(self) -> Self::NetworkWallet {
118                $crate::EthereumWallet::from(self)
119            }
120        }
121    };
122}