1
- use serde:: Deserialize ;
1
+ use serde:: { de :: Visitor , Deserialize , Serialize } ;
2
2
use serde_with:: serde_as;
3
3
use starknet_core:: {
4
4
serde:: unsigned_field_element:: { UfeHex , UfePendingBlockHash } ,
5
5
types:: FieldElement ,
6
6
} ;
7
7
8
8
use super :: {
9
+ serde_impls:: { u128_hex, u64_hex, u64_hex_opt} ,
9
10
transaction_receipt:: { TransactionExecutionStatus , TransactionFinalityStatus } ,
10
11
TransactionStatus ,
11
12
} ;
@@ -102,12 +103,11 @@ pub struct DeclareTransaction {
102
103
pub transaction_hash : FieldElement ,
103
104
#[ serde_as( deserialize_as = "Vec<UfeHex>" ) ]
104
105
pub signature : Vec < FieldElement > ,
105
- pub nonce_data_availability_mode : Option < u32 > ,
106
- pub fee_data_availability_mode : Option < u32 > ,
106
+ pub nonce_data_availability_mode : Option < DataAvailabilityMode > ,
107
+ pub fee_data_availability_mode : Option < DataAvailabilityMode > ,
107
108
pub resource_bounds : Option < ResourceBoundsMapping > ,
108
- #[ serde( default ) ]
109
- #[ serde_as( as = "Option<UfeHex>" ) ]
110
- pub tip : Option < FieldElement > ,
109
+ #[ serde( default , with = "u64_hex_opt" ) ]
110
+ pub tip : Option < u64 > ,
111
111
#[ serde_as( as = "Option<Vec<UfeHex>>" ) ]
112
112
pub paymaster_data : Option < Vec < FieldElement > > ,
113
113
#[ serde_as( deserialize_as = "Option<Vec<UfeHex>>" ) ]
@@ -156,12 +156,11 @@ pub struct DeployAccountTransaction {
156
156
#[ serde( default ) ]
157
157
#[ serde_as( as = "Option<UfeHex>" ) ]
158
158
pub max_fee : Option < FieldElement > ,
159
- pub nonce_data_availability_mode : Option < u32 > ,
160
- pub fee_data_availability_mode : Option < u32 > ,
159
+ pub nonce_data_availability_mode : Option < DataAvailabilityMode > ,
160
+ pub fee_data_availability_mode : Option < DataAvailabilityMode > ,
161
161
pub resource_bounds : Option < ResourceBoundsMapping > ,
162
- #[ serde( default ) ]
163
- #[ serde_as( as = "Option<UfeHex>" ) ]
164
- pub tip : Option < FieldElement > ,
162
+ #[ serde( default , with = "u64_hex_opt" ) ]
163
+ pub tip : Option < u64 > ,
165
164
#[ serde_as( as = "Option<Vec<UfeHex>>" ) ]
166
165
pub paymaster_data : Option < Vec < FieldElement > > ,
167
166
#[ serde( default ) ]
@@ -190,12 +189,11 @@ pub struct InvokeFunctionTransaction {
190
189
pub max_fee : Option < FieldElement > ,
191
190
#[ serde_as( as = "Option<UfeHex>" ) ]
192
191
pub nonce : Option < FieldElement > ,
193
- pub nonce_data_availability_mode : Option < u32 > ,
194
- pub fee_data_availability_mode : Option < u32 > ,
192
+ pub nonce_data_availability_mode : Option < DataAvailabilityMode > ,
193
+ pub fee_data_availability_mode : Option < DataAvailabilityMode > ,
195
194
pub resource_bounds : Option < ResourceBoundsMapping > ,
196
- #[ serde( default ) ]
197
- #[ serde_as( as = "Option<UfeHex>" ) ]
198
- pub tip : Option < FieldElement > ,
195
+ #[ serde( default , with = "u64_hex_opt" ) ]
196
+ pub tip : Option < u64 > ,
199
197
#[ serde_as( as = "Option<Vec<UfeHex>>" ) ]
200
198
pub paymaster_data : Option < Vec < FieldElement > > ,
201
199
#[ serde_as( deserialize_as = "Option<Vec<UfeHex>>" ) ]
@@ -222,24 +220,31 @@ pub struct L1HandlerTransaction {
222
220
pub version : FieldElement ,
223
221
}
224
222
225
- #[ derive( Debug , Deserialize ) ]
223
+ #[ derive( Debug , Serialize , Deserialize ) ]
226
224
#[ cfg_attr( feature = "no_unknown_fields" , serde( deny_unknown_fields) ) ]
227
225
#[ serde( rename_all = "SCREAMING_SNAKE_CASE" ) ]
228
226
pub struct ResourceBoundsMapping {
229
- l1_gas : ResourceBounds ,
230
- l2_gas : ResourceBounds ,
227
+ pub l1_gas : ResourceBounds ,
228
+ pub l2_gas : ResourceBounds ,
231
229
}
232
230
233
- #[ serde_as]
234
- #[ derive( Debug , Deserialize ) ]
231
+ #[ derive( Debug , Serialize , Deserialize ) ]
235
232
#[ cfg_attr( feature = "no_unknown_fields" , serde( deny_unknown_fields) ) ]
236
233
pub struct ResourceBounds {
237
- #[ serde_as ( as = "UfeHex " ) ]
238
- pub max_amount : FieldElement ,
239
- #[ serde_as ( as = "UfeHex " ) ]
240
- pub max_price_per_unit : FieldElement ,
234
+ #[ serde ( with = "u64_hex " ) ]
235
+ pub max_amount : u64 ,
236
+ #[ serde ( with = "u128_hex " ) ]
237
+ pub max_price_per_unit : u128 ,
241
238
}
242
239
240
+ #[ derive( Debug ) ]
241
+ pub enum DataAvailabilityMode {
242
+ L1 ,
243
+ L2 ,
244
+ }
245
+
246
+ struct DataAvailabilityModeVisitor ;
247
+
243
248
impl TransactionType {
244
249
pub fn transaction_hash ( & self ) -> FieldElement {
245
250
match self {
@@ -252,6 +257,49 @@ impl TransactionType {
252
257
}
253
258
}
254
259
260
+ impl Serialize for DataAvailabilityMode {
261
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
262
+ where
263
+ S : serde:: Serializer ,
264
+ {
265
+ serializer. serialize_u32 ( match self {
266
+ Self :: L1 => 0 ,
267
+ Self :: L2 => 1 ,
268
+ } )
269
+ }
270
+ }
271
+
272
+ impl < ' de > Deserialize < ' de > for DataAvailabilityMode {
273
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
274
+ where
275
+ D : serde:: Deserializer < ' de > ,
276
+ {
277
+ deserializer. deserialize_any ( DataAvailabilityModeVisitor )
278
+ }
279
+ }
280
+
281
+ impl < ' de > Visitor < ' de > for DataAvailabilityModeVisitor {
282
+ type Value = DataAvailabilityMode ;
283
+
284
+ fn expecting ( & self , formatter : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
285
+ write ! ( formatter, "integer" )
286
+ }
287
+
288
+ fn visit_u64 < E > ( self , v : u64 ) -> Result < Self :: Value , E >
289
+ where
290
+ E : serde:: de:: Error ,
291
+ {
292
+ match v {
293
+ 0 => Ok ( DataAvailabilityMode :: L1 ) ,
294
+ 1 => Ok ( DataAvailabilityMode :: L2 ) ,
295
+ _ => Err ( serde:: de:: Error :: invalid_value (
296
+ serde:: de:: Unexpected :: Unsigned ( v) ,
297
+ & "0 or 1" ,
298
+ ) ) ,
299
+ }
300
+ }
301
+ }
302
+
255
303
#[ cfg( test) ]
256
304
mod tests {
257
305
use super :: * ;
0 commit comments