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

feat: Add getters into TransactionResponse and update implementations #1328

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
107 changes: 105 additions & 2 deletions crates/network-primitives/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloy_eips::eip7702::SignedAuthorization;
use alloy_primitives::{Address, BlockHash, Bytes, TxHash, B256, U256};
use alloc::vec::Vec;
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
use alloy_primitives::{Address, BlockHash, Bytes, ChainId, TxHash, B256, U256};
use alloy_serde::WithOtherFields;

use crate::BlockTransactions;
Expand Down Expand Up @@ -63,10 +64,24 @@ pub trait ReceiptResponse {

/// Transaction JSON-RPC response.
pub trait TransactionResponse {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this now just Transaction + additional functions to get the block context?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, although I'm not entirely across the context. It looks similar to how ReceiptResponse is a projection of the fields on a TransactionReceipt. Happy to be guided by owners on this

/// Signature type of the transaction
type Signature;
/// Hash of the transaction
#[doc(alias = "transaction_hash")]
fn tx_hash(&self) -> TxHash;

/// Nonce
fn nonce(&self) -> u64;

/// Block hash
fn block_hash(&self) -> Option<BlockHash>;

/// Block number
fn block_number(&self) -> Option<u64>;

/// Transaction Index
fn transaction_index(&self) -> Option<u64>;

/// Sender of the transaction
fn from(&self) -> Address;

Expand All @@ -76,12 +91,42 @@ pub trait TransactionResponse {
/// Transferred value
fn value(&self) -> U256;

/// Gas Price
fn gas_price(&self) -> Option<u128>;

/// Gas limit
fn gas(&self) -> u128;

/// Max BaseFeePerGas the user is willing to pay
fn max_fee_per_gas(&self) -> Option<u128>;

/// The miner's tip
fn max_priority_fee_per_gas(&self) -> Option<u128>;

/// Configured max fee per blob gas for eip-4844 transactions
fn max_fee_per_blob_gas(&self) -> Option<u128>;

/// Input data
#[doc(alias = "calldata")]
fn input(&self) -> &Bytes;

/// Transaction signature
fn signature(&self) -> Option<Self::Signature>;

/// The chain id of the transaction
fn chain_id(&self) -> Option<ChainId>;

/// Contains the blob hashes for eip-4844 transactions
fn blob_versioned_hashes(&self) -> Option<Vec<B256>>;

/// EIP2930 access list
fn access_list(&self) -> Option<AccessList>;

/// Transaction type
fn transaction_type(&self) -> Option<u8>;

/// The signed authorization list
fn authorization_list(&self) -> Option<Vec<SignedAuthorization>>;
}

/// Header JSON-RPC response.
Expand Down Expand Up @@ -152,10 +197,28 @@ pub trait BlockResponse {
}

impl<T: TransactionResponse> TransactionResponse for WithOtherFields<T> {
type Signature = T::Signature;

fn tx_hash(&self) -> TxHash {
self.inner.tx_hash()
}

fn nonce(&self) -> u64 {
self.inner.nonce()
}

fn block_hash(&self) -> Option<BlockHash> {
self.inner.block_hash()
}

fn block_number(&self) -> Option<u64> {
self.inner.block_number()
}

fn transaction_index(&self) -> Option<u64> {
self.inner.transaction_index()
}

fn from(&self) -> Address {
self.inner.from()
}
Expand All @@ -168,13 +231,53 @@ impl<T: TransactionResponse> TransactionResponse for WithOtherFields<T> {
self.inner.value()
}

fn gas_price(&self) -> Option<u128> {
self.inner.gas_price()
}

fn gas(&self) -> u128 {
self.inner.gas()
}

fn max_fee_per_gas(&self) -> Option<u128> {
self.inner.max_fee_per_gas()
}

fn max_priority_fee_per_gas(&self) -> Option<u128> {
self.inner.max_priority_fee_per_gas()
}

fn max_fee_per_blob_gas(&self) -> Option<u128> {
self.inner.max_fee_per_blob_gas()
}

fn input(&self) -> &Bytes {
self.inner.input()
}

fn signature(&self) -> Option<T::Signature> {
self.inner.signature()
}

fn chain_id(&self) -> Option<ChainId> {
self.inner.chain_id()
}

fn blob_versioned_hashes(&self) -> Option<Vec<B256>> {
self.inner.blob_versioned_hashes()
}

fn access_list(&self) -> Option<AccessList> {
self.inner.access_list()
}

fn transaction_type(&self) -> Option<u8> {
self.inner.transaction_type()
}

fn authorization_list(&self) -> Option<Vec<SignedAuthorization>> {
self.inner.authorization_list()
}
}

impl<T: ReceiptResponse> ReceiptResponse for WithOtherFields<T> {
Expand Down
59 changes: 59 additions & 0 deletions crates/rpc-types-eth/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,28 @@ impl TryFrom<Transaction> for TxEnvelope {
}

impl TransactionResponse for Transaction {
type Signature = Signature;

fn tx_hash(&self) -> B256 {
self.hash
}

fn nonce(&self) -> u64 {
self.nonce
}

fn block_hash(&self) -> Option<BlockHash> {
self.block_hash
}

fn block_number(&self) -> Option<u64> {
self.block_number
}

fn transaction_index(&self) -> Option<u64> {
self.transaction_index
}

fn from(&self) -> Address {
self.from
}
Expand All @@ -355,14 +373,55 @@ impl TransactionResponse for Transaction {
self.value
}

fn gas_price(&self) -> Option<u128> {
self.gas_price
}

fn gas(&self) -> u128 {
self.gas
}

fn max_fee_per_gas(&self) -> Option<u128> {
self.max_fee_per_gas
}

fn max_priority_fee_per_gas(&self) -> Option<u128> {
self.max_priority_fee_per_gas
}

fn max_fee_per_blob_gas(&self) -> Option<u128> {
self.max_fee_per_blob_gas
}

fn input(&self) -> &Bytes {
&self.input
}

fn signature(&self) -> Option<Signature> {
self.signature
}

fn chain_id(&self) -> Option<ChainId> {
self.chain_id
}

fn blob_versioned_hashes(&self) -> Option<Vec<B256>> {
self.blob_versioned_hashes.clone()
}

fn access_list(&self) -> Option<AccessList> {
self.access_list.clone()
}

fn transaction_type(&self) -> Option<u8> {
self.transaction_type
}

fn authorization_list(&self) -> Option<Vec<SignedAuthorization>> {
self.authorization_list.clone()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down