1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#![allow(unknown_lints, non_local_definitions)] // TODO: remove when proptest-derive updates

use crate::Log;
use alloy_consensus::{AnyReceiptEnvelope, ReceiptEnvelope, TxType};
use alloy_primitives::{Address, BlockHash, TxHash, B256};
use alloy_serde::WithOtherFields;
use serde::{Deserialize, Serialize};

/// Transaction receipt
///
/// This type is generic over an inner [`ReceiptEnvelope`] which contains
/// consensus data and metadata.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(
    any(test, feature = "arbitrary"),
    derive(proptest_derive::Arbitrary, arbitrary::Arbitrary)
)]
#[serde(rename_all = "camelCase")]
#[doc(alias = "TxReceipt")]
pub struct TransactionReceipt<T = ReceiptEnvelope<Log>> {
    /// The receipt envelope, which contains the consensus receipt data..
    #[serde(flatten)]
    pub inner: T,
    /// Transaction Hash.
    #[doc(alias = "tx_hash")]
    pub transaction_hash: TxHash,
    /// Index within the block.
    #[serde(default, with = "alloy_serde::quantity::opt")]
    #[doc(alias = "tx_index")]
    pub transaction_index: Option<u64>,
    /// Hash of the block this transaction was included within.
    #[serde(default)]
    pub block_hash: Option<BlockHash>,
    /// Number of the block this transaction was included within.
    #[serde(default, with = "alloy_serde::quantity::opt")]
    pub block_number: Option<u64>,
    /// Gas used by this transaction alone.
    #[serde(with = "alloy_serde::quantity")]
    pub gas_used: u128,
    /// The price paid post-execution by the transaction (i.e. base fee + priority fee). Both
    /// fields in 1559-style transactions are maximums (max fee + max priority fee), the amount
    /// that's actually paid by users can only be determined post-execution
    #[serde(with = "alloy_serde::quantity")]
    pub effective_gas_price: u128,
    /// Blob gas used by the eip-4844 transaction
    ///
    /// This is None for non eip-4844 transactions
    #[serde(skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt", default)]
    pub blob_gas_used: Option<u128>,
    /// The price paid by the eip-4844 transaction per blob gas.
    #[serde(skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt", default)]
    pub blob_gas_price: Option<u128>,
    /// Address of the sender
    pub from: Address,
    /// Address of the receiver. None when its a contract creation transaction.
    pub to: Option<Address>,
    /// Contract address created, or None if not a deployment.
    pub contract_address: Option<Address>,
    /// The post-transaction stateroot (pre Byzantium)
    ///
    /// EIP98 makes this optional field, if it's missing then skip serializing it
    #[serde(skip_serializing_if = "Option::is_none", rename = "root")]
    pub state_root: Option<B256>,
}

impl AsRef<ReceiptEnvelope<Log>> for TransactionReceipt {
    fn as_ref(&self) -> &ReceiptEnvelope<Log> {
        &self.inner
    }
}

impl TransactionReceipt {
    /// Returns the status of the transaction.
    pub const fn status(&self) -> bool {
        match &self.inner {
            ReceiptEnvelope::Eip1559(receipt)
            | ReceiptEnvelope::Eip2930(receipt)
            | ReceiptEnvelope::Eip4844(receipt)
            | ReceiptEnvelope::Legacy(receipt) => receipt.receipt.status.coerce_status(),
            _ => false,
        }
    }

    /// Returns the transaction type.
    #[doc(alias = "tx_type")]
    pub const fn transaction_type(&self) -> TxType {
        self.inner.tx_type()
    }

    /// Calculates the address that will be created by the transaction, if any.
    ///
    /// Returns `None` if the transaction is not a contract creation (the `to` field is set), or if
    /// the `from` field is not set.
    pub fn calculate_create_address(&self, nonce: u64) -> Option<Address> {
        if self.to.is_some() {
            return None;
        }
        Some(self.from.create(nonce))
    }
}

impl<T> TransactionReceipt<T> {
    /// Maps the inner receipt value of this receipt.
    pub fn map_inner<U, F>(self, f: F) -> TransactionReceipt<U>
    where
        F: FnOnce(T) -> U,
    {
        TransactionReceipt {
            inner: f(self.inner),
            transaction_hash: self.transaction_hash,
            transaction_index: self.transaction_index,
            block_hash: self.block_hash,
            block_number: self.block_number,
            gas_used: self.gas_used,
            effective_gas_price: self.effective_gas_price,
            blob_gas_used: self.blob_gas_used,
            blob_gas_price: self.blob_gas_price,
            from: self.from,
            to: self.to,
            contract_address: self.contract_address,
            state_root: self.state_root,
        }
    }
}

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

#[cfg(test)]
mod test {
    use super::*;
    use crate::TransactionReceipt;
    use alloy_consensus::{Eip658Value, Receipt, ReceiptWithBloom};
    use alloy_primitives::{address, b256, bloom, Bloom};
    use arbitrary::Arbitrary;
    use rand::Rng;

    #[test]
    fn transaction_receipt_arbitrary() {
        let mut bytes = [0u8; 1024];
        rand::thread_rng().fill(bytes.as_mut_slice());

        let _: TransactionReceipt =
            TransactionReceipt::arbitrary(&mut arbitrary::Unstructured::new(&bytes)).unwrap();
    }

    #[test]
    fn test_sanity() {
        let json_str = r#"{"transactionHash":"0x21f6554c28453a01e7276c1db2fc1695bb512b170818bfa98fa8136433100616","blockHash":"0x4acbdefb861ef4adedb135ca52865f6743451bfbfa35db78076f881a40401a5e","blockNumber":"0x129f4b9","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000200000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000800000000000000000000000000000000004000000000000000000800000000100000020000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000010000000000000000000000000000","gasUsed":"0xbde1","contractAddress":null,"cumulativeGasUsed":"0xa42aec","transactionIndex":"0x7f","from":"0x9a53bfba35269414f3b2d20b52ca01b15932c7b2","to":"0xdac17f958d2ee523a2206206994597c13d831ec7","type":"0x2","effectiveGasPrice":"0xfb0f6e8c9","logs":[{"blockHash":"0x4acbdefb861ef4adedb135ca52865f6743451bfbfa35db78076f881a40401a5e","address":"0xdac17f958d2ee523a2206206994597c13d831ec7","logIndex":"0x118","data":"0x00000000000000000000000000000000000000000052b7d2dcc80cd2e4000000","removed":false,"topics":["0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925","0x0000000000000000000000009a53bfba35269414f3b2d20b52ca01b15932c7b2","0x00000000000000000000000039e5dbb9d2fead31234d7c647d6ce77d85826f76"],"blockNumber":"0x129f4b9","transactionIndex":"0x7f","transactionHash":"0x21f6554c28453a01e7276c1db2fc1695bb512b170818bfa98fa8136433100616"}],"status":"0x1"}"#;

        let receipt: TransactionReceipt = serde_json::from_str(json_str).unwrap();
        assert_eq!(
            receipt.transaction_hash,
            b256!("21f6554c28453a01e7276c1db2fc1695bb512b170818bfa98fa8136433100616")
        );

        const EXPECTED_BLOOM: Bloom = bloom!("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000200000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000800000000000000000000000000000000004000000000000000000800000000100000020000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000010000000000000000000000000000");
        const EXPECTED_CGU: u128 = 0xa42aec;

        assert!(matches!(
            receipt.inner,
            ReceiptEnvelope::Eip1559(ReceiptWithBloom {
                receipt: Receipt {
                    status: Eip658Value::Eip658(true),
                    cumulative_gas_used: EXPECTED_CGU,
                    ..
                },
                logs_bloom: EXPECTED_BLOOM
            })
        ));

        let log = receipt.inner.as_receipt().unwrap().logs.first().unwrap();
        assert_eq!(log.address(), address!("dac17f958d2ee523a2206206994597c13d831ec7"));
        assert_eq!(log.log_index, Some(0x118));
        assert_eq!(
            log.topics(),
            vec![
                b256!("8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925"),
                b256!("0000000000000000000000009a53bfba35269414f3b2d20b52ca01b15932c7b2"),
                b256!("00000000000000000000000039e5dbb9d2fead31234d7c647d6ce77d85826f76")
            ],
        );

        assert_eq!(
            serde_json::to_value(&receipt).unwrap(),
            serde_json::from_str::<serde_json::Value>(json_str).unwrap()
        );
    }

    #[test]
    fn deserialize_tx_receipt_op() {
        // OtherFields for Optimism
        #[derive(Debug, 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]
    fn deserialize_tx_receipt_arb() {
        // OtherFields for Arbitrum
        #[derive(Debug, 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");
    }
}