alloy_network_primitives/
tx_builders.rs

1use core::ops::{Deref, DerefMut};
2
3use alloc::vec::Vec;
4use alloy_consensus::BlobTransactionSidecar;
5use alloy_eips::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
53impl<T> TransactionBuilder4844 for WithOtherFields<T>
54where
55    T: TransactionBuilder4844,
56{
57    fn max_fee_per_blob_gas(&self) -> Option<u128> {
58        self.deref().max_fee_per_blob_gas()
59    }
60
61    fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) {
62        self.deref_mut().set_max_fee_per_blob_gas(max_fee_per_blob_gas)
63    }
64
65    fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> {
66        self.deref().blob_sidecar()
67    }
68
69    fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) {
70        self.deref_mut().set_blob_sidecar(sidecar)
71    }
72}
73
74impl<T> TransactionBuilder7702 for WithOtherFields<T>
75where
76    T: TransactionBuilder7702,
77{
78    fn authorization_list(&self) -> Option<&Vec<SignedAuthorization>> {
79        self.deref().authorization_list()
80    }
81
82    fn set_authorization_list(&mut self, authorization_list: Vec<SignedAuthorization>) {
83        self.deref_mut().set_authorization_list(authorization_list)
84    }
85}