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

rpc: add getter trait methods to ReceiptResponse #1251

Merged
merged 5 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions crates/network-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ workspace = true
[dependencies]
alloy-primitives.workspace = true
alloy-serde.workspace = true
alloy-eips.workspace = true

serde.workspace = true

Expand Down
67 changes: 67 additions & 0 deletions crates/network-primitives/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use alloy_eips::eip7702::SignedAuthorization;
use alloy_primitives::{Address, BlockHash, Bytes, TxHash, B256, U256};
use alloy_serde::WithOtherFields;

use crate::BlockTransactions;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Receipt JSON-RPC response.
pub trait ReceiptResponse {
/// Address of the created contract, or `None` if the transaction was not a deployment.
Expand All @@ -23,6 +27,33 @@ pub trait ReceiptResponse {

/// Number of the block this transaction was included within.
fn block_number(&self) -> Option<u64>;

/// Transaction Hash.
fn transaction_hash(&self) -> TxHash;

/// Index within the block.
fn transaction_index(&self) -> Option<u64>;

/// Gas used by this transaction alone.
fn gas_used(&self) -> u128;

/// Effective gas price.
fn effective_gas_price(&self) -> u128;

/// Blob gas used by the eip-4844 transaction.
fn blob_gas_used(&self) -> Option<u128>;

/// Blob gas price paid by the eip-4844 transaction.
fn blob_gas_price(&self) -> Option<u128>;

/// Address of the sender.
fn from(&self) -> Address;

/// Address of the receiver.
fn to(&self) -> Option<Address>;

/// Authorization list.
fn authorization_list(&self) -> Option<&[SignedAuthorization]>;
}

/// Transaction JSON-RPC response.
Expand Down Expand Up @@ -157,6 +188,42 @@ impl<T: ReceiptResponse> ReceiptResponse for WithOtherFields<T> {
fn block_number(&self) -> Option<u64> {
self.inner.block_number()
}

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

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

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

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

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

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

fn from(&self) -> Address {
self.inner.from()
}

fn to(&self) -> Option<Address> {
self.inner.to()
}

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

impl<T: BlockResponse> BlockResponse for WithOtherFields<T> {
Expand Down
40 changes: 38 additions & 2 deletions crates/rpc-types-eth/src/transaction/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,57 @@ impl<T> TransactionReceipt<T> {
pub type AnyTransactionReceipt = WithOtherFields<TransactionReceipt<AnyReceiptEnvelope<Log>>>;

impl<T: TxReceipt<Log>> ReceiptResponse for TransactionReceipt<T> {
fn contract_address(&self) -> Option<alloy_primitives::Address> {
fn contract_address(&self) -> Option<Address> {
self.contract_address
}

fn status(&self) -> bool {
self.inner.status()
}

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

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

fn transaction_hash(&self) -> TxHash {
self.transaction_hash
}

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

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

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

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

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

fn from(&self) -> Address {
self.from
}

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

fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
self.authorization_list.as_deref()
}
}

#[cfg(test)]
Expand Down