foundry_primitives/network/
receipt.rs1use alloy_consensus::{Receipt, TxReceipt};
2use alloy_network::{AnyReceiptEnvelope, AnyTransactionReceipt, ReceiptResponse};
3use alloy_primitives::{Address, B256, BlockHash, TxHash, U64};
4use alloy_rpc_types::{ConversionError, Log, TransactionReceipt};
5use alloy_serde::WithOtherFields;
6use derive_more::AsRef;
7use op_alloy_consensus::{OpDepositReceipt, OpDepositReceiptWithBloom};
8use serde::{Deserialize, Serialize};
9use tempo_primitives::TEMPO_TX_TYPE_ID;
10
11use crate::FoundryReceiptEnvelope;
12
13#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, AsRef)]
14pub struct FoundryTxReceipt(pub WithOtherFields<TransactionReceipt<FoundryReceiptEnvelope<Log>>>);
15
16impl FoundryTxReceipt {
17 pub fn new(inner: TransactionReceipt<FoundryReceiptEnvelope<Log>>) -> Self {
18 Self(WithOtherFields::new(inner))
19 }
20
21 pub fn with_timestamp(
24 inner: TransactionReceipt<FoundryReceiptEnvelope<Log>>,
25 timestamp: u64,
26 ) -> Self {
27 let mut receipt = WithOtherFields::new(inner);
28 receipt
29 .other
30 .insert("blockTimestamp".to_string(), serde_json::to_value(timestamp).unwrap());
31 Self(receipt)
32 }
33
34 pub fn block_timestamp(&self) -> Option<u64> {
36 self.0.other.get_deserialized::<u64>("blockTimestamp").transpose().ok().flatten()
37 }
38}
39
40impl ReceiptResponse for FoundryTxReceipt {
41 fn contract_address(&self) -> Option<Address> {
42 self.0.contract_address
43 }
44
45 fn status(&self) -> bool {
46 self.0.inner.status()
47 }
48
49 fn block_hash(&self) -> Option<BlockHash> {
50 self.0.block_hash
51 }
52
53 fn block_number(&self) -> Option<u64> {
54 self.0.block_number
55 }
56
57 fn transaction_hash(&self) -> TxHash {
58 self.0.transaction_hash
59 }
60
61 fn transaction_index(&self) -> Option<u64> {
62 self.0.transaction_index()
63 }
64
65 fn gas_used(&self) -> u64 {
66 self.0.gas_used()
67 }
68
69 fn effective_gas_price(&self) -> u128 {
70 self.0.effective_gas_price()
71 }
72
73 fn blob_gas_used(&self) -> Option<u64> {
74 self.0.blob_gas_used()
75 }
76
77 fn blob_gas_price(&self) -> Option<u128> {
78 self.0.blob_gas_price()
79 }
80
81 fn from(&self) -> Address {
82 self.0.from()
83 }
84
85 fn to(&self) -> Option<Address> {
86 self.0.to()
87 }
88
89 fn cumulative_gas_used(&self) -> u64 {
90 self.0.cumulative_gas_used()
91 }
92
93 fn state_root(&self) -> Option<B256> {
94 self.0.state_root()
95 }
96}
97
98impl TryFrom<AnyTransactionReceipt> for FoundryTxReceipt {
99 type Error = ConversionError;
100
101 fn try_from(receipt: AnyTransactionReceipt) -> Result<Self, Self::Error> {
102 let WithOtherFields {
103 inner:
104 TransactionReceipt {
105 transaction_hash,
106 transaction_index,
107 block_hash,
108 block_number,
109 gas_used,
110 contract_address,
111 effective_gas_price,
112 from,
113 to,
114 blob_gas_price,
115 blob_gas_used,
116 inner: AnyReceiptEnvelope { inner: receipt_with_bloom, r#type },
117 },
118 other,
119 } = receipt;
120
121 Ok(Self(WithOtherFields {
122 inner: TransactionReceipt {
123 transaction_hash,
124 transaction_index,
125 block_hash,
126 block_number,
127 gas_used,
128 contract_address,
129 effective_gas_price,
130 from,
131 to,
132 blob_gas_price,
133 blob_gas_used,
134 inner: match r#type {
135 0x00 => FoundryReceiptEnvelope::Legacy(receipt_with_bloom),
136 0x01 => FoundryReceiptEnvelope::Eip2930(receipt_with_bloom),
137 0x02 => FoundryReceiptEnvelope::Eip1559(receipt_with_bloom),
138 0x03 => FoundryReceiptEnvelope::Eip4844(receipt_with_bloom),
139 0x04 => FoundryReceiptEnvelope::Eip7702(receipt_with_bloom),
140 TEMPO_TX_TYPE_ID => FoundryReceiptEnvelope::Tempo(receipt_with_bloom),
141 0x7E => {
142 let deposit_nonce = other
146 .get_deserialized::<U64>("depositNonce")
147 .transpose()
148 .ok()
149 .flatten()
150 .map(|v| v.to::<u64>());
151 let deposit_receipt_version = other
152 .get_deserialized::<U64>("depositReceiptVersion")
153 .transpose()
154 .ok()
155 .flatten()
156 .map(|v| v.to::<u64>());
157
158 FoundryReceiptEnvelope::Deposit(OpDepositReceiptWithBloom {
159 receipt: OpDepositReceipt {
160 inner: Receipt {
161 status: alloy_consensus::Eip658Value::Eip658(
162 receipt_with_bloom.status(),
163 ),
164 cumulative_gas_used: receipt_with_bloom.cumulative_gas_used(),
165 logs: receipt_with_bloom.receipt.logs,
166 },
167 deposit_nonce,
168 deposit_receipt_version,
169 },
170 logs_bloom: receipt_with_bloom.logs_bloom,
171 })
172 }
173 _ => {
174 let tx_type = r#type;
175 return Err(ConversionError::Custom(format!(
176 "Unknown transaction receipt type: 0x{tx_type:02X}"
177 )));
178 }
179 },
180 },
181 other,
182 }))
183 }
184}
185
186#[cfg(test)]
187mod tests {
188 use super::*;
189
190 #[test]
192 fn test_receipt_convert() {
193 let s = r#"{"type":"0x4","status":"0x1","cumulativeGasUsed":"0x903fd1","logs":[{"address":"0x0000d9fcd47bf761e7287d8ee09917d7e2100000","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x0000000000000000000000000000000000000000000000000000000000000000","0x000000000000000000000000234ce51365b9c417171b6dad280f49143e1b0547"],"data":"0x00000000000000000000000000000000000000000000032139b42c3431700000","blockHash":"0xd26b59c1d8b5bfa9362d19eb0da3819dfe0b367987a71f6d30908dd45e0d7a60","blockNumber":"0x159663e","blockTimestamp":"0x68411f7b","transactionHash":"0x17a6af73d1317e69cfc3cac9221bd98261d40f24815850a44dbfbf96652ae52a","transactionIndex":"0x22","logIndex":"0x158","removed":false}],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000008100000000000000000000000000000000000000000000000020000200000000000000800000000800000000000000010000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000","transactionHash":"0x17a6af73d1317e69cfc3cac9221bd98261d40f24815850a44dbfbf96652ae52a","transactionIndex":"0x22","blockHash":"0xd26b59c1d8b5bfa9362d19eb0da3819dfe0b367987a71f6d30908dd45e0d7a60","blockNumber":"0x159663e","gasUsed":"0x28ee7","effectiveGasPrice":"0x4bf02090","from":"0x234ce51365b9c417171b6dad280f49143e1b0547","to":"0x234ce51365b9c417171b6dad280f49143e1b0547","contractAddress":null}"#;
194 let receipt: AnyTransactionReceipt = serde_json::from_str(s).unwrap();
195 let _converted = FoundryTxReceipt::try_from(receipt).unwrap();
196 }
197}