alloy_network/any/
builder.rs

1use crate::{
2    any::AnyNetwork, BuildResult, Network, NetworkWallet, TransactionBuilder,
3    TransactionBuilderError,
4};
5use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256};
6use alloy_rpc_types_eth::{AccessList, TransactionInputKind, TransactionRequest};
7use alloy_serde::WithOtherFields;
8use std::ops::{Deref, DerefMut};
9
10impl TransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
11    fn chain_id(&self) -> Option<ChainId> {
12        self.deref().chain_id()
13    }
14
15    fn set_chain_id(&mut self, chain_id: ChainId) {
16        self.deref_mut().set_chain_id(chain_id)
17    }
18
19    fn nonce(&self) -> Option<u64> {
20        self.deref().nonce()
21    }
22
23    fn set_nonce(&mut self, nonce: u64) {
24        self.deref_mut().set_nonce(nonce)
25    }
26
27    fn take_nonce(&mut self) -> Option<u64> {
28        self.deref_mut().nonce.take()
29    }
30
31    fn input(&self) -> Option<&Bytes> {
32        self.deref().input()
33    }
34
35    fn set_input<T: Into<Bytes>>(&mut self, input: T) {
36        self.deref_mut().set_input(input);
37    }
38
39    fn set_input_kind<T: Into<Bytes>>(&mut self, input: T, kind: TransactionInputKind) {
40        self.deref_mut().set_input_kind(input, kind)
41    }
42
43    fn from(&self) -> Option<Address> {
44        self.deref().from()
45    }
46
47    fn set_from(&mut self, from: Address) {
48        self.deref_mut().set_from(from);
49    }
50
51    fn kind(&self) -> Option<TxKind> {
52        self.deref().kind()
53    }
54
55    fn clear_kind(&mut self) {
56        self.deref_mut().clear_kind()
57    }
58
59    fn set_kind(&mut self, kind: TxKind) {
60        self.deref_mut().set_kind(kind)
61    }
62
63    fn value(&self) -> Option<U256> {
64        self.deref().value()
65    }
66
67    fn set_value(&mut self, value: U256) {
68        self.deref_mut().set_value(value)
69    }
70
71    fn gas_price(&self) -> Option<u128> {
72        self.deref().gas_price()
73    }
74
75    fn set_gas_price(&mut self, gas_price: u128) {
76        self.deref_mut().set_gas_price(gas_price);
77    }
78
79    fn max_fee_per_gas(&self) -> Option<u128> {
80        self.deref().max_fee_per_gas()
81    }
82
83    fn set_max_fee_per_gas(&mut self, max_fee_per_gas: u128) {
84        self.deref_mut().set_max_fee_per_gas(max_fee_per_gas);
85    }
86
87    fn max_priority_fee_per_gas(&self) -> Option<u128> {
88        self.deref().max_priority_fee_per_gas()
89    }
90
91    fn set_max_priority_fee_per_gas(&mut self, max_priority_fee_per_gas: u128) {
92        self.deref_mut().set_max_priority_fee_per_gas(max_priority_fee_per_gas);
93    }
94
95    fn gas_limit(&self) -> Option<u64> {
96        self.deref().gas_limit()
97    }
98
99    fn set_gas_limit(&mut self, gas_limit: u64) {
100        self.deref_mut().set_gas_limit(gas_limit);
101    }
102
103    /// Get the EIP-2930 access list for the transaction.
104    fn access_list(&self) -> Option<&AccessList> {
105        self.deref().access_list()
106    }
107
108    /// Sets the EIP-2930 access list.
109    fn set_access_list(&mut self, access_list: AccessList) {
110        self.deref_mut().set_access_list(access_list)
111    }
112
113    fn complete_type(&self, ty: <AnyNetwork as Network>::TxType) -> Result<(), Vec<&'static str>> {
114        self.deref().complete_type(ty.try_into().map_err(|_| vec!["unsupported_transaction_type"])?)
115    }
116
117    fn can_submit(&self) -> bool {
118        self.deref().can_submit()
119    }
120
121    fn can_build(&self) -> bool {
122        self.deref().can_build()
123    }
124
125    #[doc(alias = "output_transaction_type")]
126    fn output_tx_type(&self) -> <AnyNetwork as Network>::TxType {
127        self.deref().output_tx_type().into()
128    }
129
130    #[doc(alias = "output_transaction_type_checked")]
131    fn output_tx_type_checked(&self) -> Option<<AnyNetwork as Network>::TxType> {
132        self.deref().output_tx_type_checked().map(Into::into)
133    }
134
135    fn prep_for_submission(&mut self) {
136        self.deref_mut().prep_for_submission()
137    }
138
139    /// Build an unsigned typed transaction.
140    ///
141    /// This method validates that all required fields are present and builds an
142    /// unsigned transaction. Returns an error if any required fields are missing.
143    ///
144    /// # Limitations
145    ///
146    /// The [`TransactionRequest`] can only build Ethereum transaction types
147    /// (Legacy, EIP-2930, EIP-1559, EIP-4844, EIP-7702). Attempting to build
148    /// unknown transaction types will result in an error.
149    ///
150    /// # Errors
151    ///
152    /// Returns [`TransactionBuilderError::InvalidTransactionRequest`] if required
153    /// fields are missing for the transaction type.
154    fn build_unsigned(self) -> BuildResult<<AnyNetwork as Network>::UnsignedTx, AnyNetwork> {
155        if let Err((tx_type, missing)) = self.missing_keys() {
156            return Err(TransactionBuilderError::InvalidTransactionRequest(
157                tx_type.into(),
158                missing,
159            )
160            .into_unbuilt(self));
161        }
162        Ok(self.inner.build_typed_tx().expect("checked by missing_keys").into())
163    }
164
165    /// Build and sign a transaction using the provided wallet.
166    ///
167    /// This method signs the transaction request with the given wallet and returns
168    /// a signed transaction envelope ready for submission to the network.
169    ///
170    /// # Limitations
171    ///
172    /// The [`TransactionRequest`] can only build Ethereum transaction types.
173    /// Unknown transaction types cannot be signed through this builder.
174    ///
175    /// # Errors
176    ///
177    /// Returns an error if signing fails or if the wallet cannot produce the
178    /// required signature type for the transaction.
179    async fn build<W: NetworkWallet<AnyNetwork>>(
180        self,
181        wallet: &W,
182    ) -> Result<<AnyNetwork as Network>::TxEnvelope, TransactionBuilderError<AnyNetwork>> {
183        Ok(wallet.sign_request(self).await?)
184    }
185}