Skip to content

Commit

Permalink
rpc: add getter trait methods to ReceiptResponse (alloy-rs#1251)
Browse files Browse the repository at this point in the history
* rpc: Add getter trait methods to ReceiptResponse

* fix no std

* fix comments

* 7702 docs

---------

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
tcoratger and mattsse authored Sep 7, 2024
1 parent 9627e0a commit 2443101
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
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>;

/// EIP-7702 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

0 comments on commit 2443101

Please sign in to comment.