alloy_network_primitives/
tx_builders.rs

1use core::ops::{Deref, DerefMut};
2
3use alloc::vec::Vec;
4use alloy_consensus::BlobTransactionSidecar;
5use alloy_eips::{eip7594::BlobTransactionSidecarEip7594, eip7702::SignedAuthorization};
6use alloy_serde::WithOtherFields;
7
8/// Transaction builder type supporting EIP-4844 transaction fields.
9pub trait TransactionBuilder4844: Default + Sized + Send + Sync + 'static {
10    /// Get the max fee per blob gas for the transaction.
11    fn max_fee_per_blob_gas(&self) -> Option<u128>;
12
13    /// Set the max fee per blob gas  for the transaction.
14    fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128);
15
16    /// Builder-pattern method for setting max fee per blob gas .
17    fn with_max_fee_per_blob_gas(mut self, max_fee_per_blob_gas: u128) -> Self {
18        self.set_max_fee_per_blob_gas(max_fee_per_blob_gas);
19        self
20    }
21
22    /// Gets the EIP-4844 blob sidecar of the transaction.
23    fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar>;
24
25    /// Sets the EIP-4844 blob sidecar of the transaction.
26    ///
27    /// Note: This will also set the versioned blob hashes accordingly:
28    /// [BlobTransactionSidecar::versioned_hashes]
29    fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar);
30
31    /// Builder-pattern method for setting the EIP-4844 blob sidecar of the transaction.
32    fn with_blob_sidecar(mut self, sidecar: BlobTransactionSidecar) -> Self {
33        self.set_blob_sidecar(sidecar);
34        self
35    }
36}
37
38/// Transaction builder type supporting EIP-7702 transaction fields.
39pub trait TransactionBuilder7702: Default + Sized + Send + Sync + 'static {
40    /// Get the EIP-7702 authorization list for the transaction.
41    fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>>;
42
43    /// Sets the EIP-7702 authorization list.
44    fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>);
45
46    /// Builder-pattern method for setting the authorization list.
47    fn with_authorization_list(mut self, authorization_list: Vec<SignedAuthorization>) -> Self {
48        self.set_authorization_list(authorization_list);
49        self
50    }
51}
52
53/// Transaction builder type supporting EIP-7594 transaction fields.
54pub trait TransactionBuilder7594: Default + Sized + Send + Sync + 'static {
55    /// Get the max fee per blob gas for the transaction.
56    fn max_fee_per_blob_gas(&self) -> Option<u128>;
57
58    /// Set the max fee per blob gas for the transaction.
59    fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128);
60
61    /// Gets the EIP-7594 blob sidecar of the transaction.
62    fn blob_sidecar_7594(&self) -> Option<&BlobTransactionSidecarEip7594>;
63
64    /// Sets the EIP-7594 blob sidecar of the transaction.
65    fn set_blob_sidecar_7594(&mut self, sidecar: BlobTransactionSidecarEip7594);
66
67    /// Builder-pattern method for setting the EIP-7594 blob sidecar of the transaction.
68    fn with_blob_sidecar(mut self, sidecar: BlobTransactionSidecarEip7594) -> Self {
69        self.set_blob_sidecar_7594(sidecar);
70        self
71    }
72}
73
74impl<T> TransactionBuilder4844 for WithOtherFields<T>
75where
76    T: TransactionBuilder4844,
77{
78    fn max_fee_per_blob_gas(&self) -> Option<u128> {
79        self.deref().max_fee_per_blob_gas()
80    }
81
82    fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) {
83        self.deref_mut().set_max_fee_per_blob_gas(max_fee_per_blob_gas)
84    }
85
86    fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> {
87        self.deref().blob_sidecar()
88    }
89
90    fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) {
91        self.deref_mut().set_blob_sidecar(sidecar)
92    }
93}
94
95impl<T> TransactionBuilder7702 for WithOtherFields<T>
96where
97    T: TransactionBuilder7702,
98{
99    fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>> {
100        self.deref().authorization_list()
101    }
102
103    fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>) {
104        self.deref_mut().set_authorization_list(authorization_list)
105    }
106}
107
108impl<T> TransactionBuilder7594 for WithOtherFields<T>
109where
110    T: TransactionBuilder7594,
111{
112    fn max_fee_per_blob_gas(&self) -> Option<u128> {
113        self.deref().max_fee_per_blob_gas()
114    }
115
116    fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) {
117        self.deref_mut().set_max_fee_per_blob_gas(max_fee_per_blob_gas)
118    }
119
120    fn blob_sidecar_7594(&self) -> Option<&BlobTransactionSidecarEip7594> {
121        self.deref().blob_sidecar_7594()
122    }
123
124    fn set_blob_sidecar_7594(&mut self, sidecar: BlobTransactionSidecarEip7594) {
125        self.deref_mut().set_blob_sidecar_7594(sidecar)
126    }
127}