Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Blocked] Update TransactionRequest's to field to TxKind #553

Merged
merged 20 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: kind vs to
  • Loading branch information
prestwich committed Apr 18, 2024
commit a80433835ff2edebf258eb1802517d08c39fc657
16 changes: 11 additions & 5 deletions crates/contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{Error, Result};
use alloy_dyn_abi::{DynSolValue, FunctionExt, JsonAbiExt};
use alloy_json_abi::Function;
use alloy_network::{Ethereum, Network, ReceiptResponse, TransactionBuilder};
use alloy_primitives::{Address, Bytes, U256};
use alloy_primitives::{Address, Bytes, TxKind, U256};
use alloy_provider::{PendingTransactionBuilder, Provider};
use alloy_rpc_types::{state::StateOverride, BlockId};
use alloy_sol_types::SolCall;
Expand Down Expand Up @@ -226,7 +226,7 @@ impl<'a, T: Transport + Clone, P: Provider<T, N>, C: SolCall, N: Network>
// `sol!` macro constructor, see `#[sol(rpc)]`. Not public API.
// NOTE: please avoid changing this function due to its use in the `sol!` macro.
pub fn new_sol(provider: &'a P, address: &Address, call: &C) -> Self {
Self::new_inner(provider, call.abi_encode().into(), PhantomData::<C>).to(Some(*address))
Self::new_inner(provider, call.abi_encode().into(), PhantomData::<C>).to(*address)
}
}

Expand Down Expand Up @@ -274,9 +274,15 @@ impl<T: Transport + Clone, P: Provider<T, N>, D: CallDecoder, N: Network> CallBu
self
}

/// Sets the transaction request to the provided tx kind.
pub fn kind(mut self, to: TxKind) -> Self {
self.request.set_kind(to);
self
}

/// Sets the `to` field in the transaction to the provided address.
pub fn to(mut self, to: Option<Address>) -> Self {
self.request.set_to(to.into());
pub fn to(mut self, to: Address) -> Self {
self.request.set_to(to);
self
}

Expand Down Expand Up @@ -386,7 +392,7 @@ impl<T: Transport + Clone, P: Provider<T, N>, D: CallDecoder, N: Network> CallBu
/// Note that the deployment address can be pre-calculated if the `from` address and `nonce` are
/// known using [`calculate_create_address`](Self::calculate_create_address).
pub async fn deploy(&self) -> Result<Address> {
if !self.request.to().is_some_and(|to| to.is_create()) {
if !self.request.kind().is_some_and(|to| to.is_create()) {
return Err(Error::NotADeploymentTransaction);
}
let pending_tx = self.send().await?;
Expand Down
12 changes: 6 additions & 6 deletions crates/network/src/any/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ impl TransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
self.deref_mut().set_from(from);
}

fn to(&self) -> Option<alloy_primitives::TxKind> {
self.deref().to()
fn kind(&self) -> Option<alloy_primitives::TxKind> {
self.deref().kind()
}

fn clear_to(&mut self) {
self.deref_mut().clear_to()
fn clear_kind(&mut self) {
self.deref_mut().clear_kind()
}

fn set_to(&mut self, to: alloy_primitives::TxKind) {
self.deref_mut().set_to(to)
fn set_kind(&mut self, kind: alloy_primitives::TxKind) {
self.deref_mut().set_kind(kind)
}

fn value(&self) -> Option<alloy_primitives::U256> {
Expand Down
14 changes: 7 additions & 7 deletions crates/network/src/ethereum/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {
self.from = Some(from);
}

fn to(&self) -> Option<TxKind> {
fn kind(&self) -> Option<TxKind> {
self.to
}

fn set_to(&mut self, to: TxKind) {
self.to = Some(to);
fn set_kind(&mut self, kind: TxKind) {
self.to = Some(kind);
}

fn clear_to(&mut self) {
fn clear_kind(&mut self) {
self.to = None;
}

Expand Down Expand Up @@ -193,7 +193,7 @@ mod tests {
.with_gas_limit(0)
.with_max_fee_per_gas(0)
.with_max_priority_fee_per_gas(0)
.with_to(Address::ZERO.into())
.with_to(Address::ZERO)
.with_blob_sidecar(BlobTransactionSidecar::default())
.with_max_fee_per_blob_gas(0);

Expand All @@ -213,7 +213,7 @@ mod tests {
.with_gas_limit(0)
.with_max_fee_per_gas(0)
.with_max_priority_fee_per_gas(0)
.with_to(Address::ZERO.into())
.with_to(Address::ZERO)
.with_gas_price(0)
.access_list(AccessList::default());

Expand All @@ -229,7 +229,7 @@ mod tests {
.with_gas_limit(0)
.with_max_fee_per_gas(0)
.with_max_priority_fee_per_gas(0)
.with_to(Address::ZERO.into());
.with_to(Address::ZERO);

let tx = request.clone().build_unsigned().unwrap();

Expand Down
40 changes: 29 additions & 11 deletions crates/network/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,43 @@ pub trait TransactionBuilder<N: Network>: Default + Sized + Send + Sync + 'stati
self
}

/// Get the kind of transaction.
fn kind(&self) -> Option<alloy_primitives::TxKind>;

/// Clear the kind of transaction.
fn clear_kind(&mut self);

/// Set the kind of transaction.
fn set_kind(&mut self, kind: alloy_primitives::TxKind);

/// Builder-pattern method for setting the kind of transaction.
fn with_kind(mut self, kind: alloy_primitives::TxKind) -> Self {
self.set_kind(kind);
self
}

/// Get the recipient for the transaction.
fn to(&self) -> Option<TxKind>;
fn to(&self) -> Option<Address> {
if let Some(TxKind::Call(addr)) = self.kind() {
return Some(addr);
}
None
}

/// Set the recipient for the transaction.
fn set_to(&mut self, to: TxKind);

/// Clear the recipient for the transaction. Note that this sets the
/// receipient to `None`, not to [`TxKind::Create`];
fn clear_to(&mut self);
fn set_to(&mut self, to: Address) {
self.set_kind(TxKind::Call(to));
}

/// Builder-pattern method for setting the recipient.
fn with_to(mut self, to: TxKind) -> Self {
fn with_to(mut self, to: Address) -> Self {
self.set_to(to);
self
}

/// Set the `to` field to a create call.
fn set_create(&mut self) {
self.set_to(TxKind::Create);
self.set_kind(TxKind::Create);
}

/// Set the `to` field to a create call.
Expand All @@ -144,8 +162,8 @@ pub trait TransactionBuilder<N: Network>: Default + Sized + Send + Sync + 'stati
/// if it is set to [`TxKind::Create`].
fn set_call<T: SolCall>(&mut self, t: &T) {
self.set_input(t.abi_encode().into());
if matches!(self.to(), Some(TxKind::Create)) {
self.clear_to();
if matches!(self.kind(), Some(TxKind::Create)) {
self.clear_kind();
}
}

Expand All @@ -160,7 +178,7 @@ pub trait TransactionBuilder<N: Network>: Default + Sized + Send + Sync + 'stati
/// Returns `None` if the transaction is not a contract creation (the `to` field is set), or if
/// the `from` or `nonce` fields are not set.
fn calculate_create_address(&self) -> Option<Address> {
if !self.to().is_some_and(|to| to.is_create()) {
if !self.kind().is_some_and(|to| to.is_create()) {
return None;
}
let from = self.from()?;
Expand Down
Loading