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
use crate::{
    bytes::Bytes, transaction::AccessListItem, Action, SignedTransaction,
    Transaction, TransactionWithSignature,
    TransactionWithSignatureSerializePart,
};
use cfx_types::{AddressWithSpace, H256, U256};
use cfxkey::Address;
use rlp_derive::{RlpDecodable, RlpEncodable};
use serde_derive::{Deserialize, Serialize};

use super::{AccessList, AuthorizationListItem};

#[derive(
    Default,
    Debug,
    Clone,
    Eq,
    PartialEq,
    RlpEncodable,
    RlpDecodable,
    Serialize,
    Deserialize,
)]
pub struct NativeTransaction {
    /// Nonce.
    pub nonce: U256,
    /// Gas price.
    pub gas_price: U256,
    /// Gas paid up front for transaction execution.
    pub gas: U256,
    /// Action, can be either call or contract create.
    pub action: Action,
    /// Transferred value.
    pub value: U256,
    /// Maximum storage increasement in this execution.
    pub storage_limit: u64,
    /// The epoch height of the transaction. A transaction
    /// can only be packed between the epochs of [epoch_height -
    /// TRANSACTION_EPOCH_BOUND, epoch_height + TRANSACTION_EPOCH_BOUND]
    pub epoch_height: u64,
    /// The chain id of the transaction
    pub chain_id: u32,
    /// Transaction data.
    pub data: Bytes,
}

impl NativeTransaction {
    /// Specify the sender; this won't survive the serialize/deserialize
    /// process, but can be cloned.
    pub fn fake_sign(self, from: AddressWithSpace) -> SignedTransaction {
        SignedTransaction {
            transaction: TransactionWithSignature {
                transaction: TransactionWithSignatureSerializePart {
                    unsigned: Transaction::Native(
                        TypedNativeTransaction::Cip155(self),
                    ),
                    r: U256::one(),
                    s: U256::one(),
                    v: 0,
                },
                hash: H256::zero(),
                rlp_size: None,
            }
            .compute_hash(),
            sender: from.address,
            public: None,
        }
    }
}

#[derive(
    Default,
    Debug,
    Clone,
    Eq,
    PartialEq,
    RlpEncodable,
    RlpDecodable,
    Serialize,
    Deserialize,
)]
pub struct Cip2930Transaction {
    pub nonce: U256,
    pub gas_price: U256,
    pub gas: U256,
    pub action: Action,
    pub value: U256,
    pub storage_limit: u64,
    pub epoch_height: u64,
    pub chain_id: u32,
    pub data: Bytes,
    pub access_list: Vec<AccessListItem>,
}

#[derive(
    Default,
    Debug,
    Clone,
    Eq,
    PartialEq,
    RlpEncodable,
    RlpDecodable,
    Serialize,
    Deserialize,
)]
pub struct Cip1559Transaction {
    pub nonce: U256,
    pub max_priority_fee_per_gas: U256,
    pub max_fee_per_gas: U256,
    pub gas: U256,
    pub action: Action,
    pub value: U256,
    pub storage_limit: u64,
    pub epoch_height: u64,
    pub chain_id: u32,
    pub data: Bytes,
    pub access_list: Vec<AccessListItem>,
}

#[derive(
    Default,
    Debug,
    Clone,
    Eq,
    PartialEq,
    RlpEncodable,
    RlpDecodable,
    Serialize,
    Deserialize,
)]
pub struct Cip7702Transaction {
    pub nonce: U256,
    pub max_priority_fee_per_gas: U256,
    pub max_fee_per_gas: U256,
    pub gas: U256,
    pub destination: Address,
    pub value: U256,
    pub storage_limit: u64,
    pub epoch_height: u64,
    pub chain_id: u32,
    pub data: Bytes,
    pub access_list: Vec<AccessListItem>,
    pub authorization_list: Vec<AuthorizationListItem>,
}

macro_rules! access_common_ref {
    ($field:ident, $ty:ty) => {
        pub fn $field(&self) -> &$ty {
            match self {
                TypedNativeTransaction::Cip155(tx) => &tx.$field,
                TypedNativeTransaction::Cip2930(tx) => &tx.$field,
                TypedNativeTransaction::Cip1559(tx) => &tx.$field,
            }
        }
    };
}

impl TypedNativeTransaction {
    access_common_ref!(gas, U256);

    access_common_ref!(data, Bytes);

    access_common_ref!(nonce, U256);

    access_common_ref!(value, U256);

    access_common_ref!(chain_id, u32);

    access_common_ref!(epoch_height, u64);

    access_common_ref!(storage_limit, u64);

    pub fn action(&self) -> Action {
        match self {
            TypedNativeTransaction::Cip155(tx) => tx.action,
            TypedNativeTransaction::Cip2930(tx) => tx.action,
            TypedNativeTransaction::Cip1559(tx) => tx.action,
        }
    }

    pub fn gas_price(&self) -> &U256 {
        match self {
            Cip155(tx) => &tx.gas_price,
            Cip2930(tx) => &tx.gas_price,
            Cip1559(tx) => &tx.max_fee_per_gas,
        }
    }

    pub fn max_priority_gas_price(&self) -> &U256 {
        match self {
            Cip155(tx) => &tx.gas_price,
            Cip2930(tx) => &tx.gas_price,
            Cip1559(tx) => &tx.max_priority_fee_per_gas,
        }
    }

    pub fn nonce_mut(&mut self) -> &mut U256 {
        match self {
            Cip155(tx) => &mut tx.nonce,
            Cip2930(tx) => &mut tx.nonce,
            Cip1559(tx) => &mut tx.nonce,
        }
    }

    pub fn data_mut(&mut self) -> &mut Vec<u8> {
        match self {
            Cip155(tx) => &mut tx.data,
            Cip2930(tx) => &mut tx.data,
            Cip1559(tx) => &mut tx.data,
        }
    }

    pub fn access_list(&self) -> Option<&AccessList> {
        match self {
            Cip155(_tx) => None,
            Cip2930(tx) => Some(&tx.access_list),
            Cip1559(tx) => Some(&tx.access_list),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TypedNativeTransaction {
    Cip155(NativeTransaction),
    Cip2930(Cip2930Transaction),
    Cip1559(Cip1559Transaction),
}

impl TypedNativeTransaction {
    pub fn fake_sign_rpc(self, from: AddressWithSpace) -> SignedTransaction {
        SignedTransaction {
            transaction: TransactionWithSignature {
                transaction: TransactionWithSignatureSerializePart {
                    unsigned: Transaction::Native(self),
                    r: U256::one(),
                    s: U256::one(),
                    v: 0,
                },
                hash: H256::zero(),
                rlp_size: None,
            }
            .compute_hash(),
            sender: from.address,
            public: None,
        }
    }
}

use TypedNativeTransaction::*;