Skip to content
5 changes: 3 additions & 2 deletions crates/contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use alloy_consensus::SignableTransaction;
use alloy_dyn_abi::{DynSolValue, JsonAbiExt};
use alloy_json_abi::Function;
use alloy_network::{
eip2718::Encodable2718, Ethereum, IntoWallet, Network, TransactionBuilder,
TransactionBuilder4844, TransactionBuilder7702, TransactionBuilderError, TxSigner,
eip2718::Encodable2718, Ethereum, IntoWallet, Network, NetworkTransactionBuilder,
TransactionBuilder, TransactionBuilder4844, TransactionBuilder7702, TransactionBuilderError,
TxSigner,
};
use alloy_network_primitives::ReceiptResponse;
use alloy_primitives::{Address, Bytes, ChainId, Signature, TxKind, U256};
Expand Down
16 changes: 9 additions & 7 deletions crates/network/src/any/builder.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{
any::AnyNetwork, BuildResult, Network, NetworkWallet, TransactionBuilder,
TransactionBuilderError,
any::AnyNetwork, BuildResult, Network, NetworkTransactionBuilder, NetworkWallet,
TransactionBuilder, TransactionBuilderError,
};
use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256};
use alloy_rpc_types_eth::{AccessList, TransactionInputKind, TransactionRequest};
use alloy_serde::WithOtherFields;
use std::ops::{Deref, DerefMut};

impl TransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
impl TransactionBuilder for WithOtherFields<TransactionRequest> {
fn chain_id(&self) -> Option<ChainId> {
self.deref().chain_id()
}
Expand Down Expand Up @@ -109,11 +109,9 @@ impl TransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
fn set_access_list(&mut self, access_list: AccessList) {
self.deref_mut().set_access_list(access_list)
}
}

fn complete_type(&self, ty: <AnyNetwork as Network>::TxType) -> Result<(), Vec<&'static str>> {
self.deref().complete_type(ty.try_into().map_err(|_| vec!["unsupported_transaction_type"])?)
}

impl NetworkTransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
fn can_submit(&self) -> bool {
self.deref().can_submit()
}
Expand All @@ -122,6 +120,10 @@ impl TransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
self.deref().can_build()
}

fn complete_type(&self, ty: <AnyNetwork as Network>::TxType) -> Result<(), Vec<&'static str>> {
self.deref().complete_type(ty.try_into().map_err(|_| vec!["unsupported_transaction_type"])?)
}

#[doc(alias = "output_transaction_type")]
fn output_tx_type(&self) -> <AnyNetwork as Network>::TxType {
self.deref().output_tx_type().into()
Expand Down
31 changes: 17 additions & 14 deletions crates/network/src/ethereum/builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{
BuildResult, Ethereum, Network, NetworkWallet, TransactionBuilder, TransactionBuilder7702,
TransactionBuilderError,
BuildResult, Ethereum, Network, NetworkTransactionBuilder, NetworkWallet, TransactionBuilder,
TransactionBuilder7702, TransactionBuilderError,
};
use alloy_consensus::{TxType, TypedTransaction};
use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256};
use alloy_rpc_types_eth::{request::TransactionRequest, AccessList, TransactionInputKind};

impl TransactionBuilder<Ethereum> for TransactionRequest {
impl TransactionBuilder for TransactionRequest {
fn chain_id(&self) -> Option<ChainId> {
self.chain_id
}
Expand Down Expand Up @@ -114,17 +114,9 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {
fn set_access_list(&mut self, access_list: AccessList) {
self.access_list = Some(access_list);
}
}

fn complete_type(&self, ty: TxType) -> Result<(), Vec<&'static str>> {
match ty {
TxType::Legacy => self.complete_legacy(),
TxType::Eip2930 => self.complete_2930(),
TxType::Eip1559 => self.complete_1559(),
TxType::Eip4844 => self.complete_4844(),
TxType::Eip7702 => self.complete_7702(),
}
}

impl NetworkTransactionBuilder<Ethereum> for TransactionRequest {
fn can_submit(&self) -> bool {
// value and data may be None. If they are, they will be set to default.
// gas fields and nonce may be None, if they are, they will be populated
Expand All @@ -150,6 +142,16 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {
common && (legacy || eip2930 || eip1559 || eip4844 || eip7702)
}

fn complete_type(&self, ty: TxType) -> Result<(), Vec<&'static str>> {
match ty {
TxType::Legacy => self.complete_legacy(),
TxType::Eip2930 => self.complete_2930(),
TxType::Eip1559 => self.complete_1559(),
TxType::Eip4844 => self.complete_4844(),
TxType::Eip7702 => self.complete_7702(),
}
}

#[doc(alias = "output_transaction_type")]
fn output_tx_type(&self) -> TxType {
self.preferred_type()
Expand Down Expand Up @@ -185,7 +187,8 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {
#[cfg(test)]
mod tests {
use crate::{
TransactionBuilder, TransactionBuilder4844, TransactionBuilder7702, TransactionBuilderError,
NetworkTransactionBuilder, TransactionBuilder, TransactionBuilder4844,
TransactionBuilder7702, TransactionBuilderError,
};
use alloy_consensus::{
transaction::Recovered, BlobTransactionSidecar, SignableTransaction, TxEip1559, TxEnvelope,
Expand Down
8 changes: 4 additions & 4 deletions crates/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use core::fmt::{Debug, Display};

mod transaction;
pub use transaction::{
BuildResult, FullSigner, FullSignerSync, NetworkWallet, TransactionBuilder,
TransactionBuilder4844, TransactionBuilder7702, TransactionBuilderError, TxSigner,
TxSignerSync, UnbuiltTransactionError,
BuildResult, FullSigner, FullSignerSync, NetworkTransactionBuilder, NetworkWallet,
TransactionBuilder, TransactionBuilder4844, TransactionBuilder7702, TransactionBuilderError,
TxSigner, TxSignerSync, UnbuiltTransactionError,
};

mod ethereum;
Expand Down Expand Up @@ -80,7 +80,7 @@ pub trait Network: Debug + Clone + Copy + Sized + Send + Sync + 'static {
/// The JSON body of a transaction request.
#[doc(alias = "TxRequest")]
type TransactionRequest: RpcObject
+ TransactionBuilder<Self>
+ NetworkTransactionBuilder<Self>
+ Debug
+ From<Self::TxEnvelope>
+ From<Self::UnsignedTx>
Expand Down
66 changes: 33 additions & 33 deletions crates/network/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,9 @@ impl<N: Network> TransactionBuilderError<N> {
}
}

/// A Transaction builder for a network.
///
/// Transaction builders are primarily used to construct typed transactions that can be signed with
/// [`TransactionBuilder::build`], or unsigned typed transactions with
/// [`TransactionBuilder::build_unsigned`].
///
/// Transaction builders should be able to construct all available transaction types on a given
/// network.
/// Transaction builder.
#[doc(alias = "TxBuilder")]
pub trait TransactionBuilder<N: Network>: Default + Sized + Send + Sync + 'static {
pub trait TransactionBuilder: Default + Sized + Send + Sync + 'static {
/// Get the chain ID for the transaction.
fn chain_id(&self) -> Option<ChainId>;

Expand Down Expand Up @@ -292,6 +285,37 @@ pub trait TransactionBuilder<N: Network>: Default + Sized + Send + Sync + 'stati
self
}

/// Apply a function to the builder, returning the modified builder.
fn apply<F>(self, f: F) -> Self
where
F: FnOnce(Self) -> Self,
{
f(self)
}

/// Apply a fallible function to the builder, returning the modified builder or an error.
fn try_apply<F, E>(self, f: F) -> Result<Self, E>
where
F: FnOnce(Self) -> Result<Self, E>,
{
f(self)
}
}

/// Network-specific transaction builder.
///
/// Extends [`TransactionBuilder`] with [`build_unsigned`](Self::build_unsigned)
/// and [`build`](Self::build) methods.
#[doc(alias = "NetworkTxBuilder")]
pub trait NetworkTransactionBuilder<N: Network>: TransactionBuilder {
/// True if the builder contains all necessary information to be submitted
/// to the `eth_sendTransaction` endpoint.
fn can_submit(&self) -> bool;

/// True if the builder contains all necessary information to be built into
/// a valid transaction.
fn can_build(&self) -> bool;
Comment thread
lean-apple marked this conversation as resolved.

/// Check if all necessary keys are present to build the specified type,
/// returning a list of missing keys.
fn complete_type(&self, ty: N::TxType) -> Result<(), Vec<&'static str>>;
Expand Down Expand Up @@ -319,30 +343,6 @@ pub trait TransactionBuilder<N: Network>: Default + Sized + Send + Sync + 'stati
self
}

/// Apply a function to the builder, returning the modified builder.
fn apply<F>(self, f: F) -> Self
where
F: FnOnce(Self) -> Self,
{
f(self)
}

/// Apply a fallible function to the builder, returning the modified builder or an error.
fn try_apply<F, E>(self, f: F) -> Result<Self, E>
where
F: FnOnce(Self) -> Result<Self, E>,
{
f(self)
}

/// True if the builder contains all necessary information to be submitted
/// to the `eth_sendTransaction` endpoint.
fn can_submit(&self) -> bool;

/// True if the builder contains all necessary information to be built into
/// a valid transaction.
fn can_build(&self) -> bool;

/// Returns the transaction type that this builder will attempt to build.
/// This does not imply that the builder is ready to build.
#[doc(alias = "output_transaction_type")]
Expand Down
4 changes: 2 additions & 2 deletions crates/network/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod builder;
pub use builder::{
BuildResult, TransactionBuilder, TransactionBuilder4844, TransactionBuilder7702,
TransactionBuilderError, UnbuiltTransactionError,
BuildResult, NetworkTransactionBuilder, TransactionBuilder, TransactionBuilder4844,
TransactionBuilder7702, TransactionBuilderError, UnbuiltTransactionError,
};

mod signer;
Expand Down
2 changes: 1 addition & 1 deletion crates/network/src/transaction/signer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Network, TransactionBuilder};
use crate::{Network, NetworkTransactionBuilder, TransactionBuilder};
use alloy_consensus::SignableTransaction;
use alloy_primitives::Address;
use alloy_signer::{Signer, SignerSync};
Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/ext/trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ mod test {
use super::*;
use crate::{ext::test::async_ci_only, ProviderBuilder};
use alloy_eips::{BlockNumberOrTag, Encodable2718};
use alloy_network::{EthereumWallet, TransactionBuilder};
use alloy_network::{EthereumWallet, NetworkTransactionBuilder, TransactionBuilder};
use alloy_node_bindings::{utils::run_with_tempdir, Reth};
use alloy_primitives::{address, U256};
use alloy_rpc_types_eth::TransactionRequest;
Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/fillers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ where
/// # use alloy_primitives::{address, U256};
/// # use alloy_provider::ProviderBuilder;
/// # use alloy_rpc_types_eth::TransactionRequest;
/// # use alloy_network::TransactionBuilder;
/// # use alloy_network::{NetworkTransactionBuilder, TransactionBuilder};
///
/// # #[cfg(feature = "anvil-node")]
/// async fn example() -> Result<(), Box<dyn std::error::Error>> {
Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/fillers/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Debug;

use crate::{provider::SendableTx, Provider};
use alloy_json_rpc::RpcError;
use alloy_network::{Network, NetworkWallet, TransactionBuilder};
use alloy_network::{Network, NetworkTransactionBuilder, NetworkWallet, TransactionBuilder};
use alloy_transport::TransportResult;

use super::{FillerControlFlow, TxFiller};
Expand Down
8 changes: 5 additions & 3 deletions crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ pub trait Provider<N: Network = Ethereum>: Send + Sync {

match tx {
SendableTx::Builder(mut tx) => {
alloy_network::TransactionBuilder::prep_for_submission(&mut tx);
alloy_network::NetworkTransactionBuilder::prep_for_submission(&mut tx);
let tx_hash = self.client().request("eth_sendTransaction", (tx,)).await?;
Ok(PendingTransactionBuilder::new(self.root().clone(), tx_hash))
}
Expand Down Expand Up @@ -1239,7 +1239,7 @@ pub trait Provider<N: Network = Ethereum>: Send + Sync {

match tx {
SendableTx::Builder(mut tx) => {
alloy_network::TransactionBuilder::prep_for_submission(&mut tx);
alloy_network::NetworkTransactionBuilder::prep_for_submission(&mut tx);
let receipt = self.client().request("eth_sendTransactionSync", (tx,)).await?;
Ok(receipt)
}
Expand Down Expand Up @@ -1638,7 +1638,9 @@ mod tests {
use super::*;
use crate::{builder, ext::test::async_ci_only, ProviderBuilder, WalletProvider};
use alloy_consensus::{Transaction, TxEnvelope};
use alloy_network::{AnyNetwork, EthereumWallet, TransactionBuilder};
use alloy_network::{
AnyNetwork, EthereumWallet, NetworkTransactionBuilder, TransactionBuilder,
};
use alloy_node_bindings::{utils::run_with_tempdir, Anvil, Reth};
use alloy_primitives::{address, b256, bytes, keccak256};
use alloy_rlp::Decodable;
Expand Down
2 changes: 1 addition & 1 deletion crates/signer-trezor/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ fn signature_from_trezor(x: trezor_client::client::Signature) -> Result<Signatur
#[cfg(test)]
mod tests {
use super::*;
use alloy_network::{EthereumWallet, TransactionBuilder};
use alloy_network::{EthereumWallet, NetworkTransactionBuilder, TransactionBuilder};
use alloy_primitives::{address, b256};
use alloy_rpc_types_eth::{AccessList, AccessListItem, TransactionRequest};

Expand Down
Loading