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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
//! Alloy basic Transaction Request type.

use crate::{transaction::AccessList, BlobTransactionSidecar, Transaction};
use alloy_consensus::{
    TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxEnvelope, TxLegacy,
    TxType, TypedTransaction,
};
use alloy_primitives::{Address, Bytes, ChainId, TxKind, B256, U256};
use serde::{Deserialize, Serialize};
use std::hash::Hash;

/// Represents _all_ transaction requests to/from RPC.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[doc(alias = "TxRequest")]
pub struct TransactionRequest {
    /// The address of the transaction author.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub from: Option<Address>,
    /// The destination address of the transaction.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub to: Option<TxKind>,
    /// The legacy gas price.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt")]
    pub gas_price: Option<u128>,
    /// The max base fee per gas the sender is willing to pay.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt")]
    pub max_fee_per_gas: Option<u128>,
    /// The max priority fee per gas the sender is willing to pay, also called the miner tip.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt")]
    pub max_priority_fee_per_gas: Option<u128>,
    /// The max fee per blob gas for EIP-4844 blob transactions.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt")]
    pub max_fee_per_blob_gas: Option<u128>,
    /// The gas limit for the transaction.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt")]
    pub gas: Option<u128>,
    /// The value transferred in the transaction, in wei.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub value: Option<U256>,
    /// Transaction data.
    #[serde(default, flatten)]
    pub input: TransactionInput,
    /// The nonce of the transaction.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt")]
    pub nonce: Option<u64>,
    /// The chain ID for the transaction.
    #[serde(default, skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt")]
    pub chain_id: Option<ChainId>,
    /// An EIP-2930 access list, which lowers cost for accessing accounts and storages in the list. See [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930) for more information.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub access_list: Option<AccessList>,
    /// The EIP-2718 transaction type. See [EIP-2718](https://eips.ethereum.org/EIPS/eip-2718) for more information.
    #[serde(
        default,
        rename = "type",
        skip_serializing_if = "Option::is_none",
        with = "alloy_serde::quantity::opt"
    )]
    #[doc(alias = "tx_type")]
    pub transaction_type: Option<u8>,
    /// Blob versioned hashes for EIP-4844 transactions.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub blob_versioned_hashes: Option<Vec<B256>>,
    /// Blob sidecar for EIP-4844 transactions.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sidecar: Option<BlobTransactionSidecar>,
}

impl TransactionRequest {
    /// Sets the `from` field in the call to the provided address
    #[inline]
    pub const fn from(mut self, from: Address) -> Self {
        self.from = Some(from);
        self
    }

    /// Sets the transactions type for the transactions.
    #[doc(alias = "tx_type")]
    pub const fn transaction_type(mut self, transaction_type: u8) -> Self {
        self.transaction_type = Some(transaction_type);
        self
    }

    /// Sets the gas limit for the transaction.
    pub const fn gas_limit(mut self, gas_limit: u128) -> Self {
        self.gas = Some(gas_limit);
        self
    }

    /// Sets the nonce for the transaction.
    pub const fn nonce(mut self, nonce: u64) -> Self {
        self.nonce = Some(nonce);
        self
    }

    /// Sets the maximum fee per gas for the transaction.
    pub const fn max_fee_per_gas(mut self, max_fee_per_gas: u128) -> Self {
        self.max_fee_per_gas = Some(max_fee_per_gas);
        self
    }

    /// Sets the maximum priority fee per gas for the transaction.
    pub const fn max_priority_fee_per_gas(mut self, max_priority_fee_per_gas: u128) -> Self {
        self.max_priority_fee_per_gas = Some(max_priority_fee_per_gas);
        self
    }

    /// Sets the recipient address for the transaction.
    #[inline]
    pub const fn to(mut self, to: Address) -> Self {
        self.to = Some(TxKind::Call(to));
        self
    }

    /// Sets the value (amount) for the transaction.
    pub const fn value(mut self, value: U256) -> Self {
        self.value = Some(value);
        self
    }

    /// Sets the access list for the transaction.
    pub fn access_list(mut self, access_list: AccessList) -> Self {
        self.access_list = Some(access_list);
        self
    }

    /// Sets the input data for the transaction.
    pub fn input(mut self, input: TransactionInput) -> Self {
        self.input = input;
        self
    }

    /// Returns the configured fee cap, if any.
    ///
    /// The returns `gas_price` (legacy) if set or `max_fee_per_gas` (EIP1559)
    #[inline]
    pub fn fee_cap(&self) -> Option<u128> {
        self.gas_price.or(self.max_fee_per_gas)
    }

    /// Populate the `blob_versioned_hashes` key, if a sidecar exists. No
    /// effect otherwise.
    pub fn populate_blob_hashes(&mut self) {
        if let Some(sidecar) = self.sidecar.as_ref() {
            self.blob_versioned_hashes = Some(sidecar.versioned_hashes().collect())
        }
    }

    /// Gets invalid fields for all transaction types
    pub fn get_invalid_common_fields(&self) -> Vec<&'static str> {
        let mut errors = vec![];

        if self.nonce.is_none() {
            errors.push("nonce");
        }

        if self.gas.is_none() {
            errors.push("gas_limit");
        }

        errors
    }

    /// Gets invalid fields for EIP-1559 transaction type
    pub fn get_invalid_1559_fields(&self) -> Vec<&'static str> {
        let mut errors = vec![];

        if self.max_priority_fee_per_gas.is_none() {
            errors.push("max_priority_fee_per_gas");
        }

        if self.max_fee_per_gas.is_none() {
            errors.push("max_fee_per_gas");
        }

        errors
    }

    /// Build a legacy transaction.
    ///
    /// # Panics
    ///
    /// If required fields are missing. Use `complete_legacy` to check if the
    /// request can be built.
    fn build_legacy(self) -> TxLegacy {
        let checked_to = self.to.expect("checked in complete_legacy.");

        TxLegacy {
            chain_id: self.chain_id,
            nonce: self.nonce.expect("checked in complete_legacy"),
            gas_price: self.gas_price.expect("checked in complete_legacy"),
            gas_limit: self.gas.expect("checked in complete_legacy"),
            to: checked_to,
            value: self.value.unwrap_or_default(),
            input: self.input.into_input().unwrap_or_default(),
        }
    }

    /// Build an EIP-1559 transaction.
    ///
    /// # Panics
    ///
    /// If required fields are missing. Use `complete_1559` to check if the
    /// request can be built.
    fn build_1559(self) -> TxEip1559 {
        let checked_to = self.to.expect("checked in complete_1559.");

        TxEip1559 {
            chain_id: self.chain_id.unwrap_or(1),
            nonce: self.nonce.expect("checked in invalid_common_fields"),
            max_priority_fee_per_gas: self
                .max_priority_fee_per_gas
                .expect("checked in invalid_1559_fields"),
            max_fee_per_gas: self.max_fee_per_gas.expect("checked in invalid_1559_fields"),
            gas_limit: self.gas.expect("checked in invalid_common_fields"),
            to: checked_to,
            value: self.value.unwrap_or_default(),
            input: self.input.into_input().unwrap_or_default(),
            access_list: self.access_list.unwrap_or_default(),
        }
    }

    /// Build an EIP-2930 transaction.
    ///
    /// # Panics
    ///
    /// If required fields are missing. Use `complete_2930` to check if the
    /// request can be built.
    fn build_2930(self) -> TxEip2930 {
        let checked_to = self.to.expect("checked in complete_2930.");

        TxEip2930 {
            chain_id: self.chain_id.unwrap_or(1),
            nonce: self.nonce.expect("checked in complete_2930"),
            gas_price: self.gas_price.expect("checked in complete_2930"),
            gas_limit: self.gas.expect("checked in complete_2930"),
            to: checked_to,
            value: self.value.unwrap_or_default(),
            input: self.input.into_input().unwrap_or_default(),
            access_list: self.access_list.unwrap_or_default(),
        }
    }

    /// Build an EIP-4844 transaction.
    ///
    /// # Panics
    ///
    /// If required fields are missing. Use `complete_4844` to check if the
    /// request can be built.
    fn build_4844(mut self) -> TxEip4844WithSidecar {
        self.populate_blob_hashes();

        let checked_to = self.to.expect("checked in complete_4844.");
        let to_address = match checked_to {
            TxKind::Create => panic!("the field `to` can only be of type TxKind::Call(Account). Please change it accordingly."),
            TxKind::Call(to) => to,
        };

        TxEip4844WithSidecar {
            sidecar: self.sidecar.expect("checked in complete_4844"),
            tx: TxEip4844 {
                chain_id: self.chain_id.unwrap_or(1),
                nonce: self.nonce.expect("checked in complete_4844"),
                gas_limit: self.gas.expect("checked in complete_4844"),
                max_fee_per_gas: self.max_fee_per_gas.expect("checked in complete_4844"),
                max_priority_fee_per_gas: self
                    .max_priority_fee_per_gas
                    .expect("checked in complete_4844"),
                to: to_address,
                value: self.value.unwrap_or_default(),
                access_list: self.access_list.unwrap_or_default(),
                blob_versioned_hashes: self
                    .blob_versioned_hashes
                    .expect("populated at top of block"),
                max_fee_per_blob_gas: self.max_fee_per_blob_gas.expect("checked in complete_4844"),
                input: self.input.into_input().unwrap_or_default(),
            },
        }
    }

    fn check_reqd_fields(&self) -> Vec<&'static str> {
        let mut missing = Vec::with_capacity(12);
        if self.nonce.is_none() {
            missing.push("nonce");
        }
        if self.gas.is_none() {
            missing.push("gas_limit");
        }
        if self.to.is_none() {
            missing.push("to");
        }
        missing
    }

    fn check_legacy_fields(&self, missing: &mut Vec<&'static str>) {
        if self.gas_price.is_none() {
            missing.push("gas_price");
        }
    }

    fn check_1559_fields(&self, missing: &mut Vec<&'static str>) {
        if self.max_fee_per_gas.is_none() {
            missing.push("max_fee_per_gas");
        }
        if self.max_priority_fee_per_gas.is_none() {
            missing.push("max_priority_fee_per_gas");
        }
    }

    /// Trim field conflicts, based on the preferred type
    ///
    /// This is used to ensure that the request will not be rejected by the
    /// server due to conflicting keys, and should only be called before
    /// submission via rpc.
    pub fn trim_conflicting_keys(&mut self) {
        match self.preferred_type() {
            TxType::Legacy => {
                self.max_fee_per_gas = None;
                self.max_priority_fee_per_gas = None;
                self.max_fee_per_blob_gas = None;
                self.blob_versioned_hashes = None;
                self.sidecar = None;
                self.access_list = None;
            }
            TxType::Eip2930 => {
                self.max_fee_per_gas = None;
                self.max_priority_fee_per_gas = None;
                self.max_fee_per_blob_gas = None;
                self.blob_versioned_hashes = None;
                self.sidecar = None;
            }
            TxType::Eip1559 => {
                self.gas_price = None;
                self.blob_versioned_hashes = None;
                self.sidecar = None;
            }
            TxType::Eip4844 => {
                self.gas_price = None;
            }
        }
    }

    /// Check this builder's preferred type, based on the fields that are set.
    ///
    /// Types are preferred as follows:
    /// - EIP-4844 if sidecar or max_blob_fee_per_gas is set
    /// - EIP-2930 if access_list is set
    /// - Legacy if gas_price is set and access_list is unset
    /// - EIP-1559 in all other cases
    pub const fn preferred_type(&self) -> TxType {
        if self.sidecar.is_some() || self.max_fee_per_blob_gas.is_some() {
            TxType::Eip4844
        } else if self.access_list.is_some() && self.gas_price.is_some() {
            TxType::Eip2930
        } else if self.gas_price.is_some() {
            TxType::Legacy
        } else {
            TxType::Eip1559
        }
    }

    /// Check if all necessary keys are present to build a transaction.
    ///
    /// # Returns
    ///
    /// - Ok(type) if all necessary keys are present to build the preferred
    /// type.
    /// - Err((type, missing)) if some keys are missing to build the preferred
    /// type.
    pub fn missing_keys(&self) -> Result<TxType, (TxType, Vec<&'static str>)> {
        let pref = self.preferred_type();
        if let Err(missing) = match pref {
            TxType::Legacy => self.complete_legacy(),
            TxType::Eip2930 => self.complete_2930(),
            TxType::Eip1559 => self.complete_1559(),
            TxType::Eip4844 => self.complete_4844(),
        } {
            Err((pref, missing))
        } else {
            Ok(pref)
        }
    }

    /// Check if all necessary keys are present to build a 4844 transaction,
    /// returning a list of keys that are missing.
    pub fn complete_4844(&self) -> Result<(), Vec<&'static str>> {
        let mut missing = self.check_reqd_fields();
        self.check_1559_fields(&mut missing);

        if self.to.is_none() {
            missing.push("to");
        }

        if self.sidecar.is_none() {
            missing.push("sidecar");
        }

        if self.max_fee_per_blob_gas.is_none() {
            missing.push("max_fee_per_blob_gas");
        }

        if missing.is_empty() {
            Ok(())
        } else {
            Err(missing)
        }
    }

    /// Check if all necessary keys are present to build a 1559 transaction,
    /// returning a list of keys that are missing.
    pub fn complete_1559(&self) -> Result<(), Vec<&'static str>> {
        let mut missing = self.check_reqd_fields();
        self.check_1559_fields(&mut missing);
        if missing.is_empty() {
            Ok(())
        } else {
            Err(missing)
        }
    }

    /// Check if all necessary keys are present to build a 2930 transaction,
    /// returning a list of keys that are missing.
    pub fn complete_2930(&self) -> Result<(), Vec<&'static str>> {
        let mut missing = self.check_reqd_fields();
        self.check_legacy_fields(&mut missing);

        if self.access_list.is_none() {
            missing.push("access_list");
        }

        if missing.is_empty() {
            Ok(())
        } else {
            Err(missing)
        }
    }

    /// Check if all necessary keys are present to build a legacy transaction,
    /// returning a list of keys that are missing.
    pub fn complete_legacy(&self) -> Result<(), Vec<&'static str>> {
        let mut missing = self.check_reqd_fields();
        self.check_legacy_fields(&mut missing);

        if missing.is_empty() {
            Ok(())
        } else {
            Err(missing)
        }
    }

    /// Return the tx type this request can be built as. Computed by checking
    /// the preferred type, and then checking for completeness.
    pub fn buildable_type(&self) -> Option<TxType> {
        let pref = self.preferred_type();
        match pref {
            TxType::Legacy => self.complete_legacy().ok(),
            TxType::Eip2930 => self.complete_2930().ok(),
            TxType::Eip1559 => self.complete_1559().ok(),
            TxType::Eip4844 => self.complete_4844().ok(),
        }?;
        Some(pref)
    }

    /// Build an [`TypedTransaction`]
    pub fn build_typed_tx(self) -> Result<TypedTransaction, Self> {
        let tx_type = self.buildable_type();

        if tx_type.is_none() {
            return Err(self);
        }

        Ok(match tx_type.expect("checked") {
            TxType::Legacy => self.build_legacy().into(),
            TxType::Eip2930 => self.build_2930().into(),
            TxType::Eip1559 => self.build_1559().into(),
            TxType::Eip4844 => self.build_4844().into(),
        })
    }
}

/// Helper type that supports both `data` and `input` fields that map to transaction input data.
///
/// This is done for compatibility reasons where older implementations used `data` instead of the
/// newer, recommended `input` field.
///
/// If both fields are set, it is expected that they contain the same value, otherwise an error is
/// returned.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[doc(alias = "TxInput")]
pub struct TransactionInput {
    /// Transaction data
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub input: Option<Bytes>,
    /// Transaction data
    ///
    /// This is the same as `input` but is used for backwards compatibility: <https://github.com/ethereum/go-ethereum/issues/15628>
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub data: Option<Bytes>,
}

impl TransactionInput {
    /// Creates a new instance with the given input data.
    pub const fn new(data: Bytes) -> Self {
        Self::maybe_input(Some(data))
    }

    /// Creates a new instance with the given input data.
    pub const fn maybe_input(input: Option<Bytes>) -> Self {
        Self { input, data: None }
    }

    /// Consumes the type and returns the optional input data.
    #[inline]
    pub fn into_input(self) -> Option<Bytes> {
        self.input.or(self.data)
    }

    /// Consumes the type and returns the optional input data.
    ///
    /// Returns an error if both `data` and `input` fields are set and not equal.
    #[inline]
    pub fn try_into_unique_input(self) -> Result<Option<Bytes>, TransactionInputError> {
        self.check_unique_input().map(|()| self.into_input())
    }

    /// Returns the optional input data.
    #[inline]
    pub fn input(&self) -> Option<&Bytes> {
        self.input.as_ref().or(self.data.as_ref())
    }

    /// Returns the optional input data.
    ///
    /// Returns an error if both `data` and `input` fields are set and not equal.
    #[inline]
    pub fn unique_input(&self) -> Result<Option<&Bytes>, TransactionInputError> {
        self.check_unique_input().map(|()| self.input())
    }

    fn check_unique_input(&self) -> Result<(), TransactionInputError> {
        if let (Some(input), Some(data)) = (&self.input, &self.data) {
            if input != data {
                return Err(TransactionInputError::default());
            }
        }
        Ok(())
    }
}

impl From<Vec<u8>> for TransactionInput {
    fn from(input: Vec<u8>) -> Self {
        Self { input: Some(input.into()), data: None }
    }
}

impl From<Bytes> for TransactionInput {
    fn from(input: Bytes) -> Self {
        Self { input: Some(input), data: None }
    }
}

impl From<Option<Bytes>> for TransactionInput {
    fn from(input: Option<Bytes>) -> Self {
        Self { input, data: None }
    }
}

impl From<Transaction> for TransactionRequest {
    fn from(tx: Transaction) -> Self {
        tx.into_request()
    }
}

impl From<TxLegacy> for TransactionRequest {
    fn from(tx: TxLegacy) -> Self {
        Self {
            to: if let TxKind::Call(to) = tx.to { Some(to.into()) } else { None },
            gas_price: Some(tx.gas_price),
            gas: Some(tx.gas_limit),
            value: Some(tx.value),
            input: tx.input.into(),
            nonce: Some(tx.nonce),
            chain_id: tx.chain_id,
            transaction_type: Some(0),
            ..Default::default()
        }
    }
}

impl From<TxEip2930> for TransactionRequest {
    fn from(tx: TxEip2930) -> Self {
        Self {
            to: if let TxKind::Call(to) = tx.to { Some(to.into()) } else { None },
            gas_price: Some(tx.gas_price),
            gas: Some(tx.gas_limit),
            value: Some(tx.value),
            input: tx.input.into(),
            nonce: Some(tx.nonce),
            chain_id: Some(tx.chain_id),
            access_list: Some(tx.access_list),
            transaction_type: Some(1),
            ..Default::default()
        }
    }
}

impl From<TxEip1559> for TransactionRequest {
    fn from(tx: TxEip1559) -> Self {
        Self {
            to: if let TxKind::Call(to) = tx.to { Some(to.into()) } else { None },
            max_fee_per_gas: Some(tx.max_fee_per_gas),
            max_priority_fee_per_gas: Some(tx.max_priority_fee_per_gas),
            gas: Some(tx.gas_limit),
            value: Some(tx.value),
            input: tx.input.into(),
            nonce: Some(tx.nonce),
            chain_id: Some(tx.chain_id),
            access_list: Some(tx.access_list),
            transaction_type: Some(2),
            ..Default::default()
        }
    }
}

impl From<TxEip4844> for TransactionRequest {
    fn from(tx: TxEip4844) -> Self {
        Self {
            to: Some(tx.to.into()),
            max_fee_per_blob_gas: Some(tx.max_fee_per_blob_gas),
            gas: Some(tx.gas_limit),
            max_fee_per_gas: Some(tx.max_fee_per_gas),
            max_priority_fee_per_gas: Some(tx.max_priority_fee_per_gas),
            value: Some(tx.value),
            input: tx.input.into(),
            nonce: Some(tx.nonce),
            chain_id: Some(tx.chain_id),
            access_list: Some(tx.access_list),
            blob_versioned_hashes: Some(tx.blob_versioned_hashes),
            transaction_type: Some(3),
            ..Default::default()
        }
    }
}

impl From<TxEip4844WithSidecar> for TransactionRequest {
    fn from(tx: TxEip4844WithSidecar) -> Self {
        let sidecar = tx.sidecar;
        let tx = tx.tx;
        Self {
            to: Some(tx.to.into()),
            max_fee_per_blob_gas: Some(tx.max_fee_per_blob_gas),
            gas: Some(tx.gas_limit),
            max_fee_per_gas: Some(tx.max_fee_per_gas),
            max_priority_fee_per_gas: Some(tx.max_priority_fee_per_gas),
            value: Some(tx.value),
            input: tx.input.into(),
            nonce: Some(tx.nonce),
            chain_id: Some(tx.chain_id),
            access_list: Some(tx.access_list),
            blob_versioned_hashes: Some(tx.blob_versioned_hashes),
            sidecar: Some(sidecar),
            transaction_type: Some(3),
            ..Default::default()
        }
    }
}

impl From<TxEip4844Variant> for TransactionRequest {
    fn from(tx: TxEip4844Variant) -> Self {
        match tx {
            TxEip4844Variant::TxEip4844(tx) => tx.into(),
            TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.into(),
        }
    }
}

impl From<TypedTransaction> for TransactionRequest {
    fn from(tx: TypedTransaction) -> Self {
        match tx {
            TypedTransaction::Legacy(tx) => tx.into(),
            TypedTransaction::Eip2930(tx) => tx.into(),
            TypedTransaction::Eip1559(tx) => tx.into(),
            TypedTransaction::Eip4844(tx) => tx.into(),
        }
    }
}

impl From<TxEnvelope> for TransactionRequest {
    fn from(envelope: TxEnvelope) -> Self {
        match envelope {
            TxEnvelope::Legacy(tx) => {
                #[cfg(feature = "k256")]
                {
                    let from = tx.recover_signer().ok();
                    let tx: Self = tx.strip_signature().into();
                    if let Some(from) = from {
                        tx.from(from)
                    } else {
                        tx
                    }
                }

                #[cfg(not(feature = "k256"))]
                {
                    tx.strip_signature().into()
                }
            }
            TxEnvelope::Eip2930(tx) => {
                #[cfg(feature = "k256")]
                {
                    let from = tx.recover_signer().ok();
                    let tx: Self = tx.strip_signature().into();
                    if let Some(from) = from {
                        tx.from(from)
                    } else {
                        tx
                    }
                }

                #[cfg(not(feature = "k256"))]
                {
                    tx.strip_signature().into()
                }
            }
            TxEnvelope::Eip1559(tx) => {
                #[cfg(feature = "k256")]
                {
                    let from = tx.recover_signer().ok();
                    let tx: Self = tx.strip_signature().into();
                    if let Some(from) = from {
                        tx.from(from)
                    } else {
                        tx
                    }
                }

                #[cfg(not(feature = "k256"))]
                {
                    tx.strip_signature().into()
                }
            }
            TxEnvelope::Eip4844(tx) => {
                #[cfg(feature = "k256")]
                {
                    let from = tx.recover_signer().ok();
                    let tx: Self = tx.strip_signature().into();
                    if let Some(from) = from {
                        tx.from(from)
                    } else {
                        tx
                    }
                }

                #[cfg(not(feature = "k256"))]
                {
                    tx.strip_signature().into()
                }
            }
            _ => Default::default(),
        }
    }
}

/// Error thrown when both `data` and `input` fields are set and not equal.
#[derive(Debug, Default, thiserror::Error)]
#[error("both \"data\" and \"input\" are set and not equal. Please use \"input\" to pass transaction call data")]
#[doc(alias = "TxInputError")]
#[non_exhaustive]
pub struct TransactionInputError;

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

    // <https://github.com/paradigmxyz/reth/issues/6670>
    #[test]
    fn serde_from_to() {
        let s = r#"{"from":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", "to":"0x70997970C51812dc3A010C7d01b50e0d17dc79C8" }"#;
        let req = serde_json::from_str::<TransactionRequest>(s).unwrap();
        assert!(req.input.check_unique_input().is_ok())
    }

    #[test]
    fn serde_tx_request() {
        let s = r#"{"accessList":[],"data":"0x0902f1ac","to":"0xa478c2975ab1ea89e8196811f51a7b7ade33eb11","type":"0x02"}"#;
        let _req = serde_json::from_str::<TransactionRequest>(s).unwrap();
    }

    #[test]
    fn serde_unique_call_input() {
        let s = r#"{"accessList":[],"data":"0x0902f1ac", "input":"0x0902f1ac","to":"0xa478c2975ab1ea89e8196811f51a7b7ade33eb11","type":"0x02"}"#;
        let req = serde_json::from_str::<TransactionRequest>(s).unwrap();
        assert!(req.input.try_into_unique_input().unwrap().is_some());

        let s = r#"{"accessList":[],"data":"0x0902f1ac","to":"0xa478c2975ab1ea89e8196811f51a7b7ade33eb11","type":"0x02"}"#;
        let req = serde_json::from_str::<TransactionRequest>(s).unwrap();
        assert!(req.input.try_into_unique_input().unwrap().is_some());

        let s = r#"{"accessList":[],"input":"0x0902f1ac","to":"0xa478c2975ab1ea89e8196811f51a7b7ade33eb11","type":"0x02"}"#;
        let req = serde_json::from_str::<TransactionRequest>(s).unwrap();
        assert!(req.input.try_into_unique_input().unwrap().is_some());

        let s = r#"{"accessList":[],"data":"0x0902f1ac", "input":"0x0902f1","to":"0xa478c2975ab1ea89e8196811f51a7b7ade33eb11","type":"0x02"}"#;
        let req = serde_json::from_str::<TransactionRequest>(s).unwrap();
        assert!(req.input.try_into_unique_input().is_err());
    }

    #[test]
    fn serde_tx_request_additional_fields() {
        let s = r#"{"accessList":[],"data":"0x0902f1ac","to":"0xa478c2975ab1ea89e8196811f51a7b7ade33eb11","type":"0x02","sourceHash":"0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a"}"#;
        let req = serde_json::from_str::<WithOtherFields<TransactionRequest>>(s).unwrap();
        assert_eq!(
            req.other.get_deserialized::<B256>("sourceHash").unwrap().unwrap(),
            b256!("bf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a")
        );
    }

    #[test]
    fn serde_tx_chain_id_field() {
        let chain_id: ChainId = 12345678;

        let chain_id_as_num = format!(r#"{{"chainId": {} }}"#, chain_id);
        let req1 = serde_json::from_str::<TransactionRequest>(&chain_id_as_num).unwrap();
        assert_eq!(req1.chain_id.unwrap(), chain_id);

        let chain_id_as_hex = format!(r#"{{"chainId": "0x{:x}" }}"#, chain_id);
        let req2 = serde_json::from_str::<TransactionRequest>(&chain_id_as_hex).unwrap();
        assert_eq!(req2.chain_id.unwrap(), chain_id);
    }

    #[test]
    fn serde_empty() {
        let tx = TransactionRequest::default();
        let serialized = serde_json::to_string(&tx).unwrap();
        assert_eq!(serialized, "{}");
    }
}