Skip to content

Commit

Permalink
Move AnyTransactionReceipt to rpc-types-any
Browse files Browse the repository at this point in the history
  • Loading branch information
moricho committed Nov 4, 2024
1 parent 2eac1b6 commit 5b5d2ac
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 146 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ alloy-pubsub = { version = "0.5", path = "crates/pubsub", default-features = fal
alloy-rpc-client = { version = "0.5", path = "crates/rpc-client", default-features = false }
alloy-rpc-types-admin = { version = "0.5", path = "crates/rpc-types-admin", default-features = false }
alloy-rpc-types-anvil = { version = "0.5", path = "crates/rpc-types-anvil", default-features = false }
alloy-rpc-types-any = { version = "0.5", path = "crates/rpc-types-any", default-features = false }
alloy-rpc-types-beacon = { version = "0.5", path = "crates/rpc-types-beacon", default-features = false }
alloy-rpc-types-debug = { version = "0.5", path = "crates/rpc-types-debug", default-features = false }
alloy-rpc-types-engine = { version = "0.5", path = "crates/rpc-types-engine", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions crates/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ alloy-eips = { workspace = true, features = ["serde"] }
alloy-json-rpc.workspace = true
alloy-network-primitives.workspace = true
alloy-primitives = { workspace = true, features = ["map"] }
alloy-rpc-types-any = { workspace = true, features = ["serde"] }
alloy-rpc-types-eth = { workspace = true, features = ["std", "serde"] }
alloy-signer.workspace = true
alloy-serde.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/network/src/any/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pub use unknowns::{AnyTxType, UnknownTxEnvelope, UnknownTypedTransaction};
pub use alloy_consensus_any::{AnyHeader, AnyReceiptEnvelope};

use crate::Network;
use alloy_rpc_types_eth::{AnyTransactionReceipt, Block, Transaction, TransactionRequest};
use alloy_rpc_types_any::AnyTransactionReceipt;
use alloy_rpc_types_eth::{Block, Transaction, TransactionRequest};
use alloy_serde::WithOtherFields;

/// A catch-all header type for handling headers on multiple networks.
Expand Down
33 changes: 33 additions & 0 deletions crates/rpc-types-any/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "alloy-rpc-types-any"
description = "Types for any network"

version.workspace = true
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
exclude.workspace = true

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[lints]
workspace = true

[dependencies]
alloy-consensus-any = { workspace = true, optional = true }
alloy-rpc-types-eth.workspace = true
alloy-serde = { workspace = true, optional = true }

[dev-dependencies]
alloy-primitives.workspace = true

serde.workspace = true
serde_json.workspace = true

[features]
serde = ["dep:alloy-serde", "alloy-consensus-any/serde"]
3 changes: 3 additions & 0 deletions crates/rpc-types-any/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# alloy-rpc-types-any

Types for any network.
11 changes: 11 additions & 0 deletions crates/rpc-types-any/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/alloy.jpg",
html_favicon_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/favicon.ico"
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

mod transaction;
#[cfg(feature = "serde")]
pub use transaction::AnyTransactionReceipt;
3 changes: 3 additions & 0 deletions crates/rpc-types-any/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod receipt;
#[cfg(feature = "serde")]
pub use receipt::AnyTransactionReceipt;
149 changes: 149 additions & 0 deletions crates/rpc-types-any/src/transaction/receipt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
use alloy_rpc_types_eth::{Log, TransactionReceipt};

/// Alias for a catch-all receipt type.
#[doc(alias = "AnyTxReceipt")]
#[cfg(feature = "serde")]
pub type AnyTransactionReceipt =
alloy_serde::WithOtherFields<TransactionReceipt<alloy_consensus_any::AnyReceiptEnvelope<Log>>>;

#[cfg(test)]
mod test {
use super::*;
use alloy_primitives::b256;

#[test]
#[cfg(feature = "serde")]
fn deserialize_tx_receipt_op() {
// OtherFields for Optimism
#[derive(Debug, serde::Deserialize)]
struct OpOtherFields {
#[serde(rename = "l1BaseFeeScalar")]
l1_base_fee_scalar: String,
#[serde(rename = "l1BlobBaseFee")]
l1_blob_base_fee: String,
#[serde(rename = "l1BlobBaseFeeScalar")]
l1_blob_base_fee_scalar: String,
#[serde(rename = "l1Fee")]
l1_fee: String,
#[serde(rename = "l1GasPrice")]
l1_gas_price: String,
#[serde(rename = "l1GasUsed")]
l1_gas_used: String,
}

let receipt_json = r#"
{
"status": "0x1",
"cumulativeGasUsed": "0xf1740",
"logs": [
{
"address": "0x4200000000000000000000000000000000000006",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x0000000000000000000000005112996d3ae99f0b5360cea1a620ffcd78e8ff83",
"0x00000000000000000000000077e7c5cbeaad915cf5462064b02984e16a902e67"
],
"data": "0x000000000000000000000000000000000000000000000000001c66f6e8b40c00",
"blockHash": "0x88e07a0d797b84bd122d6993a6faf5a59ada7f40c181c553c191dd400d3d1583",
"blockNumber": "0x73a43e1",
"transactionHash": "0x2bc7cb4648e847712e39abd42178e35214a70bb15c568d604687661b9539b4c2",
"transactionIndex": "0x9",
"logIndex": "0x16",
"removed": false
}
],
"logsBloom": "0x00000000000000000000000000000000000000000000000000040000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000008000000100000000000000000100000000000000000000010000020000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000002000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"type": "0x0",
"transactionHash": "0x2bc7cb4648e847712e39abd42178e35214a70bb15c568d604687661b9539b4c2",
"transactionIndex": "0x9",
"blockHash": "0x88e07a0d797b84bd122d6993a6faf5a59ada7f40c181c553c191dd400d3d1583",
"blockNumber": "0x73a43e1",
"gasUsed": "0x85b9",
"effectiveGasPrice": "0x3ac9e84",
"from": "0x5112996d3ae99f0b5360cea1a620ffcd78e8ff83",
"to": "0x4200000000000000000000000000000000000006",
"contractAddress": null,
"l1BaseFeeScalar": "0x558",
"l1BlobBaseFee": "0x1",
"l1BlobBaseFeeScalar": "0xc5fc5",
"l1Fee": "0x105d4b2024",
"l1GasPrice": "0x5d749a07e",
"l1GasUsed": "0x800"
}
"#;
let receipt = serde_json::from_str::<AnyTransactionReceipt>(receipt_json).unwrap();

assert_eq!(
receipt.transaction_hash,
b256!("2bc7cb4648e847712e39abd42178e35214a70bb15c568d604687661b9539b4c2")
);

let other: OpOtherFields = receipt.other.deserialize_into().unwrap();
assert_eq!(other.l1_base_fee_scalar, "0x558");
assert_eq!(other.l1_blob_base_fee, "0x1");
assert_eq!(other.l1_blob_base_fee_scalar, "0xc5fc5");
assert_eq!(other.l1_fee, "0x105d4b2024");
assert_eq!(other.l1_gas_price, "0x5d749a07e");
assert_eq!(other.l1_gas_used, "0x800");
}

#[test]
#[cfg(feature = "serde")]
fn deserialize_tx_receipt_arb() {
// OtherFields for Arbitrum
#[derive(Debug, serde::Deserialize)]
struct ArbOtherFields {
#[serde(rename = "gasUsedForL1")]
gas_used_for_l1: String,
#[serde(rename = "l1BlockNumber")]
l1_block_number: String,
}

let receipt_json = r#"
{
"status": "0x1",
"cumulativeGasUsed": "0x27ebb8",
"logs": [
{
"address": "0x912ce59144191c1204e64559fe8253a0e49e6548",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x000000000000000000000000e487d95426e55a29f2266e6788ab55608ebb829b",
"0x0000000000000000000000009855134ed0c8b71266d9f3e15c0a518c07be5baf"
],
"data": "0x00000000000000000000000000000000000000000000000009d40825d5ee8000",
"blockHash": "0x83ddb8850803238bd58615680bc3718686ec1e3deaea0bc5f67c07c8577547f5",
"blockNumber": "0xd288ac5",
"transactionHash": "0x5aeca744e0c1f6d7f68641aedd394ac4b6e18cbeac3f8b3c81056c0e51a61cf3",
"transactionIndex": "0x7",
"logIndex": "0x7",
"removed": false
}
],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000005000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000100000000000001000000000000000000000000000000000000000000020000000000000000000004400000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"type": "0x0",
"transactionHash": "0x5aeca744e0c1f6d7f68641aedd394ac4b6e18cbeac3f8b3c81056c0e51a61cf3",
"transactionIndex": "0x7",
"blockHash": "0x83ddb8850803238bd58615680bc3718686ec1e3deaea0bc5f67c07c8577547f5",
"blockNumber": "0xd288ac5",
"gasUsed": "0x3ad89",
"effectiveGasPrice": "0x989680",
"from": "0xe487d95426e55a29f2266e6788ab55608ebb829b",
"to": "0x912ce59144191c1204e64559fe8253a0e49e6548",
"contractAddress": null,
"gasUsedForL1": "0x2c906",
"l1BlockNumber": "0x1323b96"
}
"#;
let receipt = serde_json::from_str::<AnyTransactionReceipt>(receipt_json).unwrap();

assert_eq!(
receipt.transaction_hash,
b256!("5aeca744e0c1f6d7f68641aedd394ac4b6e18cbeac3f8b3c81056c0e51a61cf3")
);

let other: ArbOtherFields = receipt.other.deserialize_into().unwrap();
assert_eq!(other.gas_used_for_l1, "0x2c906");
assert_eq!(other.l1_block_number, "0x1323b96");
}
}
3 changes: 0 additions & 3 deletions crates/rpc-types-eth/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ pub use error::ConversionError;
mod receipt;
pub use receipt::TransactionReceipt;

#[cfg(feature = "serde")]
pub use receipt::AnyTransactionReceipt;

pub mod request;
pub use request::{TransactionInput, TransactionRequest};

Expand Down
142 changes: 0 additions & 142 deletions crates/rpc-types-eth/src/transaction/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ impl<T> TransactionReceipt<T> {
}
}

/// Alias for a catch-all receipt type.
#[doc(alias = "AnyTxReceipt")]
#[cfg(feature = "serde")]
pub type AnyTransactionReceipt =
alloy_serde::WithOtherFields<TransactionReceipt<alloy_consensus_any::AnyReceiptEnvelope<Log>>>;

impl<T: TxReceipt<Log>> ReceiptResponse for TransactionReceipt<T> {
fn contract_address(&self) -> Option<Address> {
self.contract_address
Expand Down Expand Up @@ -265,142 +259,6 @@ mod test {
);
}

#[test]
#[cfg(feature = "serde")]
fn deserialize_tx_receipt_op() {
// OtherFields for Optimism
#[derive(Debug, serde::Deserialize)]
struct OpOtherFields {
#[serde(rename = "l1BaseFeeScalar")]
l1_base_fee_scalar: String,
#[serde(rename = "l1BlobBaseFee")]
l1_blob_base_fee: String,
#[serde(rename = "l1BlobBaseFeeScalar")]
l1_blob_base_fee_scalar: String,
#[serde(rename = "l1Fee")]
l1_fee: String,
#[serde(rename = "l1GasPrice")]
l1_gas_price: String,
#[serde(rename = "l1GasUsed")]
l1_gas_used: String,
}

let receipt_json = r#"
{
"status": "0x1",
"cumulativeGasUsed": "0xf1740",
"logs": [
{
"address": "0x4200000000000000000000000000000000000006",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x0000000000000000000000005112996d3ae99f0b5360cea1a620ffcd78e8ff83",
"0x00000000000000000000000077e7c5cbeaad915cf5462064b02984e16a902e67"
],
"data": "0x000000000000000000000000000000000000000000000000001c66f6e8b40c00",
"blockHash": "0x88e07a0d797b84bd122d6993a6faf5a59ada7f40c181c553c191dd400d3d1583",
"blockNumber": "0x73a43e1",
"transactionHash": "0x2bc7cb4648e847712e39abd42178e35214a70bb15c568d604687661b9539b4c2",
"transactionIndex": "0x9",
"logIndex": "0x16",
"removed": false
}
],
"logsBloom": "0x00000000000000000000000000000000000000000000000000040000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000008000000100000000000000000100000000000000000000010000020000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000002000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"type": "0x0",
"transactionHash": "0x2bc7cb4648e847712e39abd42178e35214a70bb15c568d604687661b9539b4c2",
"transactionIndex": "0x9",
"blockHash": "0x88e07a0d797b84bd122d6993a6faf5a59ada7f40c181c553c191dd400d3d1583",
"blockNumber": "0x73a43e1",
"gasUsed": "0x85b9",
"effectiveGasPrice": "0x3ac9e84",
"from": "0x5112996d3ae99f0b5360cea1a620ffcd78e8ff83",
"to": "0x4200000000000000000000000000000000000006",
"contractAddress": null,
"l1BaseFeeScalar": "0x558",
"l1BlobBaseFee": "0x1",
"l1BlobBaseFeeScalar": "0xc5fc5",
"l1Fee": "0x105d4b2024",
"l1GasPrice": "0x5d749a07e",
"l1GasUsed": "0x800"
}
"#;
let receipt = serde_json::from_str::<AnyTransactionReceipt>(receipt_json).unwrap();

assert_eq!(
receipt.transaction_hash,
b256!("2bc7cb4648e847712e39abd42178e35214a70bb15c568d604687661b9539b4c2")
);

let other: OpOtherFields = receipt.other.deserialize_into().unwrap();
assert_eq!(other.l1_base_fee_scalar, "0x558");
assert_eq!(other.l1_blob_base_fee, "0x1");
assert_eq!(other.l1_blob_base_fee_scalar, "0xc5fc5");
assert_eq!(other.l1_fee, "0x105d4b2024");
assert_eq!(other.l1_gas_price, "0x5d749a07e");
assert_eq!(other.l1_gas_used, "0x800");
}

#[test]
#[cfg(feature = "serde")]
fn deserialize_tx_receipt_arb() {
// OtherFields for Arbitrum
#[derive(Debug, serde::Deserialize)]
struct ArbOtherFields {
#[serde(rename = "gasUsedForL1")]
gas_used_for_l1: String,
#[serde(rename = "l1BlockNumber")]
l1_block_number: String,
}

let receipt_json = r#"
{
"status": "0x1",
"cumulativeGasUsed": "0x27ebb8",
"logs": [
{
"address": "0x912ce59144191c1204e64559fe8253a0e49e6548",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x000000000000000000000000e487d95426e55a29f2266e6788ab55608ebb829b",
"0x0000000000000000000000009855134ed0c8b71266d9f3e15c0a518c07be5baf"
],
"data": "0x00000000000000000000000000000000000000000000000009d40825d5ee8000",
"blockHash": "0x83ddb8850803238bd58615680bc3718686ec1e3deaea0bc5f67c07c8577547f5",
"blockNumber": "0xd288ac5",
"transactionHash": "0x5aeca744e0c1f6d7f68641aedd394ac4b6e18cbeac3f8b3c81056c0e51a61cf3",
"transactionIndex": "0x7",
"logIndex": "0x7",
"removed": false
}
],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000005000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000100000000000001000000000000000000000000000000000000000000020000000000000000000004400000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"type": "0x0",
"transactionHash": "0x5aeca744e0c1f6d7f68641aedd394ac4b6e18cbeac3f8b3c81056c0e51a61cf3",
"transactionIndex": "0x7",
"blockHash": "0x83ddb8850803238bd58615680bc3718686ec1e3deaea0bc5f67c07c8577547f5",
"blockNumber": "0xd288ac5",
"gasUsed": "0x3ad89",
"effectiveGasPrice": "0x989680",
"from": "0xe487d95426e55a29f2266e6788ab55608ebb829b",
"to": "0x912ce59144191c1204e64559fe8253a0e49e6548",
"contractAddress": null,
"gasUsedForL1": "0x2c906",
"l1BlockNumber": "0x1323b96"
}
"#;
let receipt = serde_json::from_str::<AnyTransactionReceipt>(receipt_json).unwrap();

assert_eq!(
receipt.transaction_hash,
b256!("5aeca744e0c1f6d7f68641aedd394ac4b6e18cbeac3f8b3c81056c0e51a61cf3")
);

let other: ArbOtherFields = receipt.other.deserialize_into().unwrap();
assert_eq!(other.gas_used_for_l1, "0x2c906");
assert_eq!(other.l1_block_number, "0x1323b96");
}

#[test]
#[cfg(feature = "serde")]
fn deserialize_pre_eip658_receipt() {
Expand Down

0 comments on commit 5b5d2ac

Please sign in to comment.