alloy_network_primitives/
traits.rsuse alloy_consensus::{BlockHeader, Transaction};
use alloy_eips::eip7702::SignedAuthorization;
use alloy_primitives::{Address, BlockHash, TxHash, B256};
use alloy_serde::WithOtherFields;
use crate::BlockTransactions;
pub trait ReceiptResponse {
fn contract_address(&self) -> Option<Address>;
fn status(&self) -> bool;
fn block_hash(&self) -> Option<BlockHash>;
fn block_number(&self) -> Option<u64>;
fn transaction_hash(&self) -> TxHash;
fn transaction_index(&self) -> Option<u64>;
fn gas_used(&self) -> u128;
fn effective_gas_price(&self) -> u128;
fn blob_gas_used(&self) -> Option<u128>;
fn blob_gas_price(&self) -> Option<u128>;
fn from(&self) -> Address;
fn to(&self) -> Option<Address>;
fn authorization_list(&self) -> Option<&[SignedAuthorization]>;
fn cumulative_gas_used(&self) -> u128;
fn state_root(&self) -> Option<B256>;
}
pub trait TransactionResponse: Transaction {
#[doc(alias = "transaction_hash")]
fn tx_hash(&self) -> TxHash;
fn block_hash(&self) -> Option<BlockHash>;
fn block_number(&self) -> Option<u64>;
fn transaction_index(&self) -> Option<u64>;
fn from(&self) -> Address;
fn to(&self) -> Option<Address> {
self.kind().to().copied()
}
fn gas_price(&self) -> Option<u128> {
if self.ty() < 2 {
return Some(Transaction::max_fee_per_gas(self));
}
None
}
fn max_fee_per_gas(&self) -> Option<u128> {
if self.ty() < 2 {
return None;
}
Some(Transaction::max_fee_per_gas(self))
}
fn transaction_type(&self) -> Option<u8> {
match self.ty() {
0 => None,
ty => Some(ty),
}
}
}
pub trait HeaderResponse: BlockHeader {
fn hash(&self) -> BlockHash;
}
pub trait BlockResponse {
type Header;
type Transaction: TransactionResponse;
fn header(&self) -> &Self::Header;
fn transactions(&self) -> &BlockTransactions<Self::Transaction>;
fn transactions_mut(&mut self) -> &mut BlockTransactions<Self::Transaction>;
fn other_fields(&self) -> Option<&alloy_serde::OtherFields> {
None
}
}
impl<T: TransactionResponse> TransactionResponse for WithOtherFields<T> {
fn tx_hash(&self) -> TxHash {
self.inner.tx_hash()
}
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()
}
}
impl<T: ReceiptResponse> ReceiptResponse for WithOtherFields<T> {
fn contract_address(&self) -> Option<Address> {
self.inner.contract_address()
}
fn status(&self) -> bool {
self.inner.status()
}
fn block_hash(&self) -> Option<BlockHash> {
self.inner.block_hash()
}
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()
}
fn cumulative_gas_used(&self) -> u128 {
self.inner.cumulative_gas_used()
}
fn state_root(&self) -> Option<B256> {
self.inner.state_root()
}
}
impl<T: BlockResponse> BlockResponse for WithOtherFields<T> {
type Header = T::Header;
type Transaction = T::Transaction;
fn header(&self) -> &Self::Header {
self.inner.header()
}
fn transactions(&self) -> &BlockTransactions<Self::Transaction> {
self.inner.transactions()
}
fn transactions_mut(&mut self) -> &mut BlockTransactions<Self::Transaction> {
self.inner.transactions_mut()
}
fn other_fields(&self) -> Option<&alloy_serde::OtherFields> {
Some(&self.other)
}
}
impl<T: HeaderResponse> HeaderResponse for WithOtherFields<T> {
fn hash(&self) -> BlockHash {
self.inner.hash()
}
}