Skip to main content

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    /// Returns true if the transaction contains any blob sidecar data.
26    ///
27    /// By default this checks only the EIP-4844 sidecar accessor. Types that can also carry
28    /// alternate blob sidecar formats, such as EIP-7594, should override this method.
29    fn has_blob_sidecar(&self) -> bool {
30        self.blob_sidecar().is_some()
31    }
32
33    /// Sets the EIP-4844 blob sidecar of the transaction.
34    ///
35    /// Note: This will also set the versioned blob hashes accordingly:
36    /// [BlobTransactionSidecar::versioned_hashes]
37    fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar);
38
39    /// Builder-pattern method for setting the EIP-4844 blob sidecar of the transaction.
40    fn with_blob_sidecar(mut self, sidecar: BlobTransactionSidecar) -> Self {
41        self.set_blob_sidecar(sidecar);
42        self
43    }
44}
45
46/// Transaction builder type supporting EIP-7702 transaction fields.
47pub trait TransactionBuilder7702: Default + Sized + Send + Sync + 'static {
48    /// Get the EIP-7702 authorization list for the transaction.
49    fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>>;
50
51    /// Sets the EIP-7702 authorization list.
52    fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>);
53
54    /// Builder-pattern method for setting the authorization list.
55    fn with_authorization_list(mut self, authorization_list: Vec<SignedAuthorization>) -> Self {
56        self.set_authorization_list(authorization_list);
57        self
58    }
59}
60
61/// Transaction builder type supporting EIP-7594 transaction fields.
62pub trait TransactionBuilder7594: Default + Sized + Send + Sync + 'static {
63    /// Get the max fee per blob gas for the transaction.
64    fn max_fee_per_blob_gas(&self) -> Option<u128>;
65
66    /// Set the max fee per blob gas for the transaction.
67    fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128);
68
69    /// Gets the EIP-7594 blob sidecar of the transaction.
70    fn blob_sidecar_7594(&self) -> Option<&BlobTransactionSidecarEip7594>;
71
72    /// Sets the EIP-7594 blob sidecar of the transaction.
73    fn set_blob_sidecar_7594(&mut self, sidecar: BlobTransactionSidecarEip7594);
74
75    /// Builder-pattern method for setting the EIP-7594 blob sidecar of the transaction.
76    fn with_blob_sidecar(mut self, sidecar: BlobTransactionSidecarEip7594) -> Self {
77        self.set_blob_sidecar_7594(sidecar);
78        self
79    }
80}
81
82impl<T> TransactionBuilder4844 for WithOtherFields<T>
83where
84    T: TransactionBuilder4844,
85{
86    fn max_fee_per_blob_gas(&self) -> Option<u128> {
87        self.deref().max_fee_per_blob_gas()
88    }
89
90    fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) {
91        self.deref_mut().set_max_fee_per_blob_gas(max_fee_per_blob_gas)
92    }
93
94    fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> {
95        self.deref().blob_sidecar()
96    }
97
98    fn has_blob_sidecar(&self) -> bool {
99        self.deref().has_blob_sidecar()
100    }
101
102    fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) {
103        self.deref_mut().set_blob_sidecar(sidecar)
104    }
105}
106
107impl<T> TransactionBuilder7702 for WithOtherFields<T>
108where
109    T: TransactionBuilder7702,
110{
111    fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>> {
112        self.deref().authorization_list()
113    }
114
115    fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>) {
116        self.deref_mut().set_authorization_list(authorization_list)
117    }
118}
119
120impl<T> TransactionBuilder7594 for WithOtherFields<T>
121where
122    T: TransactionBuilder7594,
123{
124    fn max_fee_per_blob_gas(&self) -> Option<u128> {
125        self.deref().max_fee_per_blob_gas()
126    }
127
128    fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) {
129        self.deref_mut().set_max_fee_per_blob_gas(max_fee_per_blob_gas)
130    }
131
132    fn blob_sidecar_7594(&self) -> Option<&BlobTransactionSidecarEip7594> {
133        self.deref().blob_sidecar_7594()
134    }
135
136    fn set_blob_sidecar_7594(&mut self, sidecar: BlobTransactionSidecarEip7594) {
137        self.deref_mut().set_blob_sidecar_7594(sidecar)
138    }
139}