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
Changes from 5 commits
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
49 changes: 42 additions & 7 deletions crates/rpc-types/src/eth/transaction/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use alloy_consensus::{
TxType, TypedTransaction,
};
use alloy_primitives::{Address, Bytes, ChainId, TxKind, B256, U256};
use alloy_sol_types::SolCall;
use serde::{Deserialize, Serialize};
use std::hash::Hash;

Expand All @@ -16,7 +17,7 @@ pub struct TransactionRequest {
/// The address of the transaction author.
pub from: Option<Address>,
/// The destination address of the transaction.
pub to: Option<Address>,
pub to: Option<TxKind>,
/// The legacy gas price.
#[serde(
default,
Expand Down Expand Up @@ -136,7 +137,7 @@ impl TransactionRequest {
/// Sets the recipient address for the transaction.
#[inline]
pub const fn to(mut self, to: Address) -> Self {
self.to = Some(to);
self.to = Some(TxKind::Call(to));
self
}

Expand Down Expand Up @@ -211,12 +212,14 @@ 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.");

TxLegacy {
chain_id: self.chain_id,
nonce: self.nonce.expect("checked in complete_legacy"),
gas_price: self.gas_price.expect("checked in complete_legacy"),
gas_limit: self.gas.expect("checked in complete_legacy"),
to: self.to.into(),
to: checked_to,
value: self.value.unwrap_or_default(),
input: self.input.into_input().unwrap_or_default(),
}
Expand All @@ -229,6 +232,8 @@ 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.");

TxEip1559 {
chain_id: self.chain_id.unwrap_or(1),
nonce: self.nonce.expect("checked in invalid_common_fields"),
Expand All @@ -237,7 +242,7 @@ impl TransactionRequest {
.expect("checked in invalid_1559_fields"),
max_fee_per_gas: self.max_fee_per_gas.expect("checked in invalid_1559_fields"),
gas_limit: self.gas.expect("checked in invalid_common_fields"),
to: self.to.into(),
to: checked_to,
value: self.value.unwrap_or_default(),
input: self.input.into_input().unwrap_or_default(),
access_list: self.access_list.unwrap_or_default(),
Expand All @@ -251,12 +256,14 @@ 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.");

TxEip2930 {
chain_id: self.chain_id.unwrap_or(1),
nonce: self.nonce.expect("checked in complete_2930"),
gas_price: self.gas_price.expect("checked in complete_2930"),
gas_limit: self.gas.expect("checked in complete_2930"),
to: self.to.into(),
to: checked_to,
value: self.value.unwrap_or_default(),
input: self.input.into_input().unwrap_or_default(),
access_list: self.access_list.unwrap_or_default(),
Expand All @@ -272,6 +279,12 @@ 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 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,
};

TxEip4844WithSidecar {
sidecar: self.sidecar.expect("checked in complete_4844"),
tx: TxEip4844 {
Expand All @@ -282,7 +295,7 @@ impl TransactionRequest {
max_priority_fee_per_gas: self
.max_priority_fee_per_gas
.expect("checked in complete_4844"),
to: self.to.expect("checked in complete_4844"),
to: to_address,
value: self.value.unwrap_or_default(),
access_list: self.access_list.unwrap_or_default(),
blob_versioned_hashes: self
Expand All @@ -294,6 +307,28 @@ 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>(self, t: T) -> Self {
Self {
// wip
..self
}
}

fn check_reqd_fields(&self) -> Vec<&'static str> {
let mut missing = Vec::with_capacity(12);
if self.nonce.is_none() {
Expand Down Expand Up @@ -636,7 +671,7 @@ impl From<TxEip4844> for TransactionRequest {
fn from(tx: TxEip4844) -> TransactionRequest {
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