diff --git a/crates/network-primitives/src/lib.rs b/crates/network-primitives/src/lib.rs index 54080e5c95e..a6303490722 100644 --- a/crates/network-primitives/src/lib.rs +++ b/crates/network-primitives/src/lib.rs @@ -14,3 +14,6 @@ pub use traits::{BlockResponse, ReceiptResponse, TransactionResponse}; mod block; pub use block::{BlockTransactionHashes, BlockTransactions, BlockTransactionsKind}; + +mod tx_builders; +pub use tx_builders::{TransactionBuilder4844, TransactionBuilder7702}; diff --git a/crates/network-primitives/src/tx_builders.rs b/crates/network-primitives/src/tx_builders.rs new file mode 100644 index 00000000000..a3db9e78f3c --- /dev/null +++ b/crates/network-primitives/src/tx_builders.rs @@ -0,0 +1,85 @@ +use core::ops::{Deref, DerefMut}; + +use alloc::vec::Vec; +use alloy_consensus::BlobTransactionSidecar; +use alloy_eips::eip7702::SignedAuthorization; +use alloy_serde::WithOtherFields; + +/// Transaction builder type supporting EIP-4844 transaction fields. +pub trait TransactionBuilder4844: Default + Sized + Send + Sync + 'static { + /// Get the max fee per blob gas for the transaction. + fn max_fee_per_blob_gas(&self) -> Option; + + /// Set the max fee per blob gas for the transaction. + fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128); + + /// Builder-pattern method for setting max fee per blob gas . + fn with_max_fee_per_blob_gas(mut self, max_fee_per_blob_gas: u128) -> Self { + self.set_max_fee_per_blob_gas(max_fee_per_blob_gas); + self + } + + /// Gets the EIP-4844 blob sidecar of the transaction. + fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar>; + + /// Sets the EIP-4844 blob sidecar of the transaction. + /// + /// Note: This will also set the versioned blob hashes accordingly: + /// [BlobTransactionSidecar::versioned_hashes] + fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar); + + /// Builder-pattern method for setting the EIP-4844 blob sidecar of the transaction. + fn with_blob_sidecar(mut self, sidecar: BlobTransactionSidecar) -> Self { + self.set_blob_sidecar(sidecar); + self + } +} + +/// Transaction builder type supporting EIP-7702 transaction fields. +pub trait TransactionBuilder7702: Default + Sized + Send + Sync + 'static { + /// Get the EIP-7702 authorization list for the transaction. + fn authorization_list(&self) -> Option<&Vec>; + + /// Sets the EIP-7702 authorization list. + fn set_authorization_list(&mut self, authorization_list: Vec); + + /// Builder-pattern method for setting the authorization list. + fn with_authorization_list(mut self, authorization_list: Vec) -> Self { + self.set_authorization_list(authorization_list); + self + } +} + +impl TransactionBuilder4844 for WithOtherFields +where + T: TransactionBuilder4844, +{ + fn max_fee_per_blob_gas(&self) -> Option { + self.deref().max_fee_per_blob_gas() + } + + fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) { + self.deref_mut().set_max_fee_per_blob_gas(max_fee_per_blob_gas) + } + + fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> { + self.deref().blob_sidecar() + } + + fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) { + self.deref_mut().set_blob_sidecar(sidecar) + } +} + +impl TransactionBuilder7702 for WithOtherFields +where + T: TransactionBuilder7702, +{ + fn authorization_list(&self) -> Option<&Vec> { + self.deref().authorization_list() + } + + fn set_authorization_list(&mut self, authorization_list: Vec) { + self.deref_mut().set_authorization_list(authorization_list) + } +} diff --git a/crates/network/src/any/builder.rs b/crates/network/src/any/builder.rs index 637ef03ad83..e54901e0b2c 100644 --- a/crates/network/src/any/builder.rs +++ b/crates/network/src/any/builder.rs @@ -1,9 +1,7 @@ use crate::{ any::AnyNetwork, BuildResult, Network, NetworkWallet, TransactionBuilder, - TransactionBuilder4844, TransactionBuilder7702, TransactionBuilderError, + TransactionBuilderError, }; -use alloy_consensus::BlobTransactionSidecar; -use alloy_eips::eip7702::SignedAuthorization; use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256}; use alloy_rpc_types_eth::{AccessList, TransactionRequest}; use alloy_serde::WithOtherFields; @@ -148,31 +146,3 @@ impl TransactionBuilder for WithOtherFields { Ok(wallet.sign_request(self).await?) } } - -impl TransactionBuilder4844 for WithOtherFields { - fn max_fee_per_blob_gas(&self) -> Option { - self.deref().max_fee_per_blob_gas() - } - - fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) { - self.deref_mut().set_max_fee_per_blob_gas(max_fee_per_blob_gas) - } - - fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> { - self.deref().blob_sidecar() - } - - fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) { - self.deref_mut().set_blob_sidecar(sidecar) - } -} - -impl TransactionBuilder7702 for WithOtherFields { - fn authorization_list(&self) -> Option<&Vec> { - self.deref().authorization_list() - } - - fn set_authorization_list(&mut self, authorization_list: Vec) { - self.deref_mut().set_authorization_list(authorization_list) - } -} diff --git a/crates/network/src/ethereum/builder.rs b/crates/network/src/ethereum/builder.rs index 42c7647ea5a..6850a42ac44 100644 --- a/crates/network/src/ethereum/builder.rs +++ b/crates/network/src/ethereum/builder.rs @@ -1,9 +1,8 @@ use crate::{ - BuildResult, Ethereum, Network, NetworkWallet, TransactionBuilder, TransactionBuilder4844, - TransactionBuilder7702, TransactionBuilderError, + BuildResult, Ethereum, Network, NetworkWallet, TransactionBuilder, TransactionBuilder7702, + TransactionBuilderError, }; -use alloy_consensus::{BlobTransactionSidecar, TxType, TypedTransaction}; -use alloy_eips::eip7702::SignedAuthorization; +use alloy_consensus::{TxType, TypedTransaction}; use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256}; use alloy_rpc_types_eth::{request::TransactionRequest, AccessList}; @@ -167,35 +166,6 @@ impl TransactionBuilder for TransactionRequest { } } -impl TransactionBuilder4844 for TransactionRequest { - fn max_fee_per_blob_gas(&self) -> Option { - self.max_fee_per_blob_gas - } - - fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) { - self.max_fee_per_blob_gas = Some(max_fee_per_blob_gas) - } - - fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> { - self.sidecar.as_ref() - } - - fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) { - self.sidecar = Some(sidecar); - self.populate_blob_hashes(); - } -} - -impl TransactionBuilder7702 for TransactionRequest { - fn authorization_list(&self) -> Option<&Vec> { - self.authorization_list.as_ref() - } - - fn set_authorization_list(&mut self, authorization_list: Vec) { - self.authorization_list = Some(authorization_list); - } -} - #[cfg(test)] mod tests { use crate::{ diff --git a/crates/network/src/transaction/builder.rs b/crates/network/src/transaction/builder.rs index 6209b25c6bc..0adb5b36e5b 100644 --- a/crates/network/src/transaction/builder.rs +++ b/crates/network/src/transaction/builder.rs @@ -1,12 +1,12 @@ use super::signer::NetworkWallet; use crate::Network; -use alloy_consensus::BlobTransactionSidecar; -use alloy_eips::eip7702::SignedAuthorization; use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256}; use alloy_rpc_types_eth::AccessList; use alloy_sol_types::SolCall; use futures_utils_wasm::impl_future; +pub use alloy_network_primitives::{TransactionBuilder4844, TransactionBuilder7702}; + /// Result type for transaction builders pub type BuildResult = Result>; @@ -343,48 +343,3 @@ pub trait TransactionBuilder: Default + Sized + Send + Sync + 'stati wallet: &W, ) -> impl_future!(>>); } - -/// Transaction builder type supporting EIP-4844 transaction fields. -pub trait TransactionBuilder4844: Default + Sized + Send + Sync + 'static { - /// Get the max fee per blob gas for the transaction. - fn max_fee_per_blob_gas(&self) -> Option; - - /// Set the max fee per blob gas for the transaction. - fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128); - - /// Builder-pattern method for setting max fee per blob gas . - fn with_max_fee_per_blob_gas(mut self, max_fee_per_blob_gas: u128) -> Self { - self.set_max_fee_per_blob_gas(max_fee_per_blob_gas); - self - } - - /// Gets the EIP-4844 blob sidecar of the transaction. - fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar>; - - /// Sets the EIP-4844 blob sidecar of the transaction. - /// - /// Note: This will also set the versioned blob hashes accordingly: - /// [BlobTransactionSidecar::versioned_hashes] - fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar); - - /// Builder-pattern method for setting the EIP-4844 blob sidecar of the transaction. - fn with_blob_sidecar(mut self, sidecar: BlobTransactionSidecar) -> Self { - self.set_blob_sidecar(sidecar); - self - } -} - -/// Transaction builder type supporting EIP-7702 transaction fields. -pub trait TransactionBuilder7702: Default + Sized + Send + Sync + 'static { - /// Get the EIP-7702 authorization list for the transaction. - fn authorization_list(&self) -> Option<&Vec>; - - /// Sets the EIP-7702 authorization list. - fn set_authorization_list(&mut self, authorization_list: Vec); - - /// Builder-pattern method for setting the authorization list. - fn with_authorization_list(mut self, authorization_list: Vec) -> Self { - self.set_authorization_list(authorization_list); - self - } -} diff --git a/crates/rpc-types-eth/src/transaction/request.rs b/crates/rpc-types-eth/src/transaction/request.rs index 63b431ae0ed..8366610eaff 100644 --- a/crates/rpc-types-eth/src/transaction/request.rs +++ b/crates/rpc-types-eth/src/transaction/request.rs @@ -6,6 +6,7 @@ use alloy_consensus::{ TxEip7702, TxEnvelope, TxLegacy, TxType, TypedTransaction, }; use alloy_eips::eip7702::SignedAuthorization; +use alloy_network_primitives::{TransactionBuilder4844, TransactionBuilder7702}; use alloy_primitives::{Address, Bytes, ChainId, TxKind, B256, U256}; use core::hash::Hash; @@ -657,6 +658,35 @@ impl TransactionRequest { } } +impl TransactionBuilder4844 for TransactionRequest { + fn max_fee_per_blob_gas(&self) -> Option { + self.max_fee_per_blob_gas + } + + fn set_max_fee_per_blob_gas(&mut self, max_fee_per_blob_gas: u128) { + self.max_fee_per_blob_gas = Some(max_fee_per_blob_gas) + } + + fn blob_sidecar(&self) -> Option<&BlobTransactionSidecar> { + self.sidecar.as_ref() + } + + fn set_blob_sidecar(&mut self, sidecar: BlobTransactionSidecar) { + self.sidecar = Some(sidecar); + self.populate_blob_hashes(); + } +} + +impl TransactionBuilder7702 for TransactionRequest { + fn authorization_list(&self) -> Option<&Vec> { + self.authorization_list.as_ref() + } + + fn set_authorization_list(&mut self, authorization_list: Vec) { + self.authorization_list = Some(authorization_list); + } +} + /// Helper type that supports both `data` and `input` fields that map to transaction input data. /// /// This is done for compatibility reasons where older implementations used `data` instead of the