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
feat: add the methods to the builder trait, update code that was broken.
  • Loading branch information
EmperorOrokuSaki committed Apr 17, 2024
commit 48b8d7a713730cac97ca77411d33ef461286864e
1 change: 1 addition & 0 deletions crates/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ alloy-json-rpc.workspace = true
alloy-primitives.workspace = true
alloy-rpc-types.workspace = true
alloy-signer.workspace = true
alloy-sol-types.workspace = true

async-trait.workspace = true
futures-utils-wasm.workspace = true
Expand Down
13 changes: 13 additions & 0 deletions crates/network/src/any/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,17 @@ impl TransactionBuilder<AnyNetwork> for WithOtherFields<TransactionRequest> {
) -> Result<<AnyNetwork as Network>::TxEnvelope, TransactionBuilderError<AnyNetwork>> {
Ok(signer.sign_request(self).await?)
}

fn as_create(self) -> Self {
// self.deref().as_create()
todo!()
}

fn deploy_code(self, code: Vec<u8>) -> Self {
todo!()
}

fn with_call<T: alloy_sol_types::SolCall>(&mut self, t: &T) -> &mut Self {
todo!()
}
}
36 changes: 31 additions & 5 deletions crates/network/src/ethereum/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::{
};
use alloy_consensus::{BlobTransactionSidecar, TxType, TypedTransaction};
use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256};
use alloy_rpc_types::{request::TransactionRequest, AccessList};
use alloy_rpc_types::{request::TransactionRequest, AccessList, TransactionInput};
use alloy_sol_types::SolCall;

impl TransactionBuilder<Ethereum> for TransactionRequest {
fn chain_id(&self) -> Option<ChainId> {
Expand Down Expand Up @@ -39,16 +40,41 @@ impl TransactionBuilder<Ethereum> for TransactionRequest {
}

fn to(&self) -> Option<TxKind> {
self.to.map(TxKind::Call).or(Some(TxKind::Create))
self.to
}

fn set_to(&mut self, to: TxKind) {
match to {
TxKind::Create => self.to = None,
TxKind::Call(to) => self.to = Some(to),
self.to = Some(to);
}

fn as_create(self) -> Self {
Self { to: Some(TxKind::Create), ..self }
}

fn deploy_code(self, code: Vec<u8>) -> Self {
Self {
to: Some(TxKind::Create),
input: TransactionInput {
input: Some(Bytes::from(code.clone())),
data: Some(Bytes::from(code)),
},
..self
}
}

fn with_call<T: SolCall>(&mut self, t: &T) -> &mut Self {
if matches!(self.to, Some(TxKind::Create)) {
self.to = None;
}

let data: Vec<u8> = t.abi_encode();
self.input = TransactionInput {
input: Some(Bytes::from(data.clone())),
data: Some(Bytes::from(data)),
};
self
}

fn value(&self) -> Option<U256> {
self.value
}
Expand Down
10 changes: 10 additions & 0 deletions crates/network/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::Network;
use alloy_consensus::BlobTransactionSidecar;
use alloy_primitives::{Address, Bytes, ChainId, TxKind, U256};
use alloy_rpc_types::AccessList;
use alloy_sol_types::SolCall;
use futures_utils_wasm::impl_future;

/// Result type for transaction builders
Expand Down Expand Up @@ -104,6 +105,15 @@ pub trait TransactionBuilder<N: Network>: Default + Sized + Send + Sync + 'stati
/// Set the recipient for the transaction.
fn set_to(&mut self, to: TxKind);

/// Set the `to` field to a create call.
fn as_create(self) -> Self;

/// Deploy the code by making a create call with data.
fn deploy_code(self, code: Vec<u8>) -> Self;

/// Make a contract call with data.
fn with_call<T: SolCall>(&mut self, t: &T) -> &mut Self;

/// Builder-pattern method for setting the recipient.
fn with_to(mut self, to: TxKind) -> Self {
self.set_to(to);
Expand Down
9 changes: 7 additions & 2 deletions crates/rpc-types/src/eth/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloy_consensus::{
SignableTransaction, Signed, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEnvelope,
TxLegacy, TxType,
};
use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_primitives::{Address, Bytes, TxKind, B256, U256};
use serde::{Deserialize, Serialize};

pub use alloy_consensus::BlobTransactionSidecar;
Expand Down Expand Up @@ -153,9 +153,14 @@ impl Transaction {
(None, None) => None,
};

let to = match self.to {
Some(to_address) => Some(TxKind::Call(to_address)),
None => None,
};

TransactionRequest {
from: Some(self.from),
to: self.to,
to,
gas: Some(self.gas),
gas_price,
value: Some(self.value),
Expand Down
50 changes: 11 additions & 39 deletions crates/rpc-types/src/eth/transaction/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl TransactionRequest {
/// If required fields are missing. Use `complete_legacy` to check if the
/// request can be built.
fn build_legacy(self) -> TxLegacy {
let checked_to = self.to.expect("the `to` field should have value.");
let checked_to = self.to.expect("checked in complete_legacy.");

TxLegacy {
chain_id: self.chain_id,
Expand All @@ -232,7 +232,7 @@ impl TransactionRequest {
/// If required fields are missing. Use `complete_1559` to check if the
/// request can be built.
fn build_1559(self) -> TxEip1559 {
let checked_to = self.to.expect("the `to` field should have value.");
let checked_to = self.to.expect("checked in complete_1559.");

TxEip1559 {
chain_id: self.chain_id.unwrap_or(1),
Expand All @@ -256,7 +256,7 @@ impl TransactionRequest {
/// If required fields are missing. Use `complete_2930` to check if the
/// request can be built.
fn build_2930(self) -> TxEip2930 {
let checked_to = self.to.expect("the `to` field should have value.");
let checked_to = self.to.expect("checked in complete_2930.");

TxEip2930 {
chain_id: self.chain_id.unwrap_or(1),
Expand All @@ -279,7 +279,7 @@ impl TransactionRequest {
fn build_4844(mut self) -> TxEip4844WithSidecar {
self.populate_blob_hashes();

let checked_to = self.to.expect("the `to` field should have value.");
let checked_to = self.to.expect("checked in complete_4844.");
let to_address = match checked_to {
TxKind::Create => panic!("the field `to` can only be of type TxKind::Call(Account). Please change it accordingly."),
TxKind::Call(to) => to,
Expand Down Expand Up @@ -307,37 +307,6 @@ impl TransactionRequest {
}
}

fn as_create(self) -> Self {
Self { to: Some(TxKind::Create), ..self }
}

fn deploy_code(self, code: Vec<u8>) -> Self {
Self {
to: Some(TxKind::Create),
input: TransactionInput {
input: Some(Bytes::from(code)),
data: Some(Bytes::from(code)),
},
..self
}
}

fn with_call<T: SolCall>(mut self, t: &T) -> Self {

if matches!(self.to, Some(TxKind::Create)) {
self.to = None;
}

let data: Vec<u8> = t.abi_encode();
Self {
input: TransactionInput {
input: Some(Bytes::from(data)),
data: Some(Bytes::from(data)),
},
..self
}
}

fn check_reqd_fields(&self) -> Vec<&'static str> {
let mut missing = Vec::with_capacity(12);
if self.nonce.is_none() {
Expand All @@ -346,6 +315,9 @@ impl TransactionRequest {
if self.gas.is_none() {
missing.push("gas_limit");
}
if self.to.is_none() {
missing.push("to");
}
missing
}

Expand Down Expand Up @@ -626,7 +598,7 @@ impl From<TxLegacy> for TransactionRequest {
fn from(tx: TxLegacy) -> TransactionRequest {
TransactionRequest {
from: None,
to: if let TxKind::Call(to) = tx.to { Some(to) } else { None },
to: if let TxKind::Call(to) = tx.to { Some(TxKind::Call(to)) } else { None },
gas_price: Some(tx.gas_price),
gas: Some(tx.gas_limit),
value: Some(tx.value),
Expand All @@ -643,7 +615,7 @@ impl From<TxEip2930> for TransactionRequest {
fn from(tx: TxEip2930) -> TransactionRequest {
TransactionRequest {
from: None,
to: if let TxKind::Call(to) = tx.to { Some(to) } else { None },
to: if let TxKind::Call(to) = tx.to { Some(TxKind::Call(to)) } else { None },
gas_price: Some(tx.gas_price),
gas: Some(tx.gas_limit),
value: Some(tx.value),
Expand All @@ -661,7 +633,7 @@ impl From<TxEip1559> for TransactionRequest {
fn from(tx: TxEip1559) -> TransactionRequest {
TransactionRequest {
from: None,
to: if let TxKind::Call(to) = tx.to { Some(to) } else { None },
to: if let TxKind::Call(to) = tx.to { Some(TxKind::Call(to)) } else { None },
max_fee_per_gas: Some(tx.max_fee_per_gas),
max_priority_fee_per_gas: Some(tx.max_priority_fee_per_gas),
gas: Some(tx.gas_limit),
Expand Down Expand Up @@ -703,7 +675,7 @@ impl From<TxEip4844WithSidecar> for TransactionRequest {
let tx = tx.tx;
TransactionRequest {
from: None,
to: Some(tx.to),
to: Some(TxKind::Call(tx.to)),
max_fee_per_blob_gas: Some(tx.max_fee_per_blob_gas),
gas: Some(tx.gas_limit),
max_fee_per_gas: Some(tx.max_fee_per_gas),
Expand Down
Loading