@@ -41,14 +41,17 @@ import assert from '../assert';
41
41
function decodeBaseType ( type : string , calldata : string | string [ ] ) : BigNumberish | CairoUint256 {
42
42
switch ( true ) {
43
43
case CairoUint256 . isAbiType ( type ) :
44
- assert ( Array . isArray ( calldata ) && calldata . length === 2 , 'Expected calldata for CairoUint256 as an array of two strings.' )
44
+ assert (
45
+ Array . isArray ( calldata ) && calldata . length === 2 ,
46
+ 'Expected calldata for CairoUint256 as an array of two strings.'
47
+ ) ;
45
48
return CairoUint256 . fromCalldata ( [ calldata [ 0 ] , calldata [ 1 ] ] ) ;
46
49
47
50
case isTypeBytes31 ( type ) :
48
51
return decodeShortString ( calldata as string ) ;
49
52
50
53
case isTypeFelt ( type ) :
51
- assert ( typeof calldata === 'string' , 'Expected string calldata for base type decoding.' )
54
+ assert ( typeof calldata === 'string' , 'Expected string calldata for base type decoding.' ) ;
52
55
return BigInt ( calldata ) ;
53
56
54
57
default :
@@ -64,19 +67,24 @@ function decodeBaseType(type: string, calldata: string | string[]): BigNumberish
64
67
* @param enums The ABI enums.
65
68
* @returns An array of decoded tuple elements.
66
69
*/
67
- function decodeTuple ( calldata : string [ ] , typeStr : string , structs : AbiStructs , enums : AbiEnums ) {
70
+ function decodeTuple (
71
+ calldata : string [ ] ,
72
+ typeStr : string ,
73
+ structs : AbiStructs ,
74
+ enums : AbiEnums
75
+ ) : any [ ] {
68
76
// Parse typeStr to understand the tuple structure, e.g., "('felt', 'struct', 'enum')"
69
77
const types : string [ ] = extractTupleMemberTypes ( typeStr ) . map ( ( type : string | object ) =>
70
78
String ( type )
71
79
) ;
72
80
73
81
// Assuming we now have an array of types, ['felt', 'YourStructName', 'YourEnumName'], etc.
74
- const decodedElements = [ ] ;
82
+ const decodedElements : any = [ ] ;
75
83
let calldataIndex = 0 ;
76
84
77
- for ( const type of types ) {
85
+ types . forEach ( ( type ) => {
78
86
switch ( true ) {
79
- case isTypeStruct ( type , structs ) :
87
+ case isTypeStruct ( type , structs ) : {
80
88
const structRes = decodeStruct (
81
89
calldata . slice ( calldataIndex , calldataIndex + structs [ type ] . size ) ,
82
90
type ,
@@ -86,7 +94,8 @@ function decodeTuple(calldata: string[], typeStr: string, structs: AbiStructs, e
86
94
decodedElements . push ( structRes ) ;
87
95
calldataIndex += structs [ type ] . size ; // Assuming size is defined for structs.
88
96
break ;
89
- case isTypeEnum ( type , enums ) :
97
+ }
98
+ case isTypeEnum ( type , enums ) : {
90
99
// Determine the expected calldata consumption for the current enum. (e.g., 1 or 2 elements for CairoOption, 2 elements for CairoResult, etc.)
91
100
const expectedCalldataLength = getExpectedCalldataLengthForEnum (
92
101
calldata [ calldataIndex ] ,
@@ -98,18 +107,21 @@ function decodeTuple(calldata: string[], typeStr: string, structs: AbiStructs, e
98
107
decodedElements . push ( enumRes ) ;
99
108
calldataIndex += expectedCalldataLength ; // Move past the consumed calldata.
100
109
break ;
101
- case isTypeArray ( type ) :
110
+ }
111
+ case isTypeArray ( type ) : {
102
112
const arrayType = getArrayType ( type ) ;
103
113
const arrayRes = decodeCalldataValue ( [ calldata [ calldataIndex ] ] , arrayType , structs , enums ) ;
104
114
decodedElements . push ( arrayRes ) ;
105
115
calldataIndex += 1 ;
106
116
break ;
107
- default :
117
+ }
118
+ default : {
108
119
const result = decodeBaseType ( type , calldata [ calldataIndex ] ) ;
109
120
decodedElements . push ( result ) ;
110
121
calldataIndex += 1 ;
122
+ }
111
123
}
112
- }
124
+ } ) ;
113
125
114
126
return decodedElements ;
115
127
}
@@ -119,19 +131,20 @@ function decodeTuple(calldata: string[], typeStr: string, structs: AbiStructs, e
119
131
* @param calldata The calldata array.
120
132
* @returns The decoded byte array.
121
133
*/
134
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
122
135
function decodeByteArray ( calldata : string [ ] ) : ByteArray {
123
136
// Extract the length of the data array from the first element.
124
137
const dataLength = parseInt ( calldata [ 0 ] , 10 ) ;
125
-
138
+
126
139
// Extract the data array elements based on the extracted length.
127
140
const data = calldata . slice ( 1 , 1 + dataLength ) . map ( ( str ) => parseInt ( str , 10 ) ) ;
128
-
141
+
129
142
// The pending_word is the second-to-last element in the original array.
130
143
const pending_word = parseInt ( calldata [ 1 + dataLength ] , 10 ) ;
131
-
144
+
132
145
// The pending_word_len is the last element in the original array.
133
146
const pending_word_len = parseInt ( calldata [ 2 + dataLength ] , 10 ) ;
134
-
147
+
135
148
// Construct and return the ByteArray object.
136
149
return {
137
150
data,
@@ -207,7 +220,7 @@ function decodeCalldataValue(
207
220
// CairoResult decoding
208
221
if ( isTypeResult ( type ) ) {
209
222
const matches = type . match ( / R e s u l t < ( .+ ) , \s * ( .+ ) > / ) ;
210
- assert ( matches !== null && matches . length > 2 , `Type "${ type } " is not a valid Option type.` )
223
+ assert ( matches !== null && matches . length > 2 , `Type "${ type } " is not a valid Option type.` ) ;
211
224
212
225
const okType = matches [ 1 ] ;
213
226
const errType = matches [ 2 ] ;
@@ -242,7 +255,7 @@ function decodeArray(
242
255
const elementType = getArrayType ( arrayType ) ;
243
256
const elements = [ ] ;
244
257
245
- for ( let i = 0 ; i < calldata . length ; i ++ ) {
258
+ for ( let i = 0 ; i < calldata . length ; i += 1 ) {
246
259
elements . push ( decodeCalldataValue ( [ calldata [ i ] ] , elementType , structs , enums ) ) ;
247
260
}
248
261
@@ -270,12 +283,12 @@ function decodeStruct(
270
283
let index = 0 ;
271
284
const result : ParsedStruct = { } ;
272
285
273
- for ( const field of structAbi . members ) {
286
+ structAbi . members . forEach ( ( field ) => {
274
287
const fieldType = field . type ;
275
288
const fieldCalldata = calldataSegment . slice ( index , index + 1 ) ;
276
289
result [ field . name ] = decodeCalldataValue ( fieldCalldata [ 0 ] , fieldType , structs , enums ) ;
277
290
index += 1 ;
278
- }
291
+ } ) ;
279
292
280
293
return result ;
281
294
}
@@ -293,22 +306,28 @@ function decodeEnum(calldataValues: string[], enumName: string, enums: AbiEnums)
293
306
assert ( enumDefinition !== null , `Enum with name ${ enumName } not found.` ) ;
294
307
295
308
const variantIndex = parseInt ( calldataValues [ 0 ] , 10 ) ;
296
- assert ( variantIndex >= 0 && variantIndex < enumDefinition . variants . length , `Variant index ${ variantIndex } out of range for enum ${ enumName } .` ) ;
309
+ assert (
310
+ variantIndex >= 0 && variantIndex < enumDefinition . variants . length ,
311
+ `Variant index ${ variantIndex } out of range for enum ${ enumName } .`
312
+ ) ;
297
313
298
314
const variant = enumDefinition . variants [ variantIndex ] ;
299
315
300
316
// Determine the enum type and decode accordingly
301
317
switch ( enumName ) {
302
318
case 'CairoOption' :
303
319
switch ( variant . name ) {
304
- case 'None' :
320
+ case 'None' : {
305
321
return new CairoOption ( CairoOptionVariant . None ) ;
306
- default : // "Some"
322
+ }
323
+ default : {
324
+ // "Some"
307
325
// const someValue = calldataValues[1]; // Placeholder logic.
308
326
const someValue = decodeCalldataValue ( calldataValues . slice ( 1 ) , variant . type , { } , enums ) ;
309
327
return new CairoOption ( CairoOptionVariant . Some , someValue ) ;
328
+ }
310
329
}
311
- case 'CairoResult' :
330
+ case 'CairoResult' : {
312
331
// const resultValue = calldataValues[1]; // Placeholder logic.
313
332
const resultValue = decodeCalldataValue ( calldataValues . slice ( 1 ) , variant . type , { } , enums ) ;
314
333
@@ -318,9 +337,11 @@ function decodeEnum(calldataValues: string[], enumName: string, enums: AbiEnums)
318
337
default : // "Err"
319
338
return new CairoResult ( CairoResultVariant . Err , resultValue ) ;
320
339
}
321
-
322
- default : // Handling CairoCustomEnum or simple enum types without associated data.
340
+ }
341
+ default : {
342
+ // Handling CairoCustomEnum or simple enum types without associated data.
323
343
return new CairoCustomEnum ( { activeVariant : variant . name , variant : variant . name } ) ;
344
+ }
324
345
}
325
346
}
326
347
@@ -341,12 +362,15 @@ function decodeCairoOption(
341
362
const optionIndicator = parseInt ( calldata [ 0 ] , 10 ) ;
342
363
343
364
switch ( optionIndicator ) {
344
- case 0 : // None
345
- return CairoOptionVariant . None ;
346
- default :
347
- // Assuming the value is directly after the indicator
348
- const valueCalldata = calldata . slice ( 1 ) ;
349
- return decodeCalldataValue ( valueCalldata , innerType , structs , enums ) ;
365
+ case 0 : {
366
+ // None
367
+ return CairoOptionVariant . None ;
368
+ }
369
+ default : {
370
+ // Assuming the value is directly after the indicator
371
+ const valueCalldata = calldata . slice ( 1 ) ;
372
+ return decodeCalldataValue ( valueCalldata , innerType , structs , enums ) ;
373
+ }
350
374
}
351
375
}
352
376
@@ -369,12 +393,16 @@ function decodeCairoResult(
369
393
const resultIndicator = parseInt ( calldata [ 0 ] , 10 ) ;
370
394
371
395
switch ( resultIndicator ) {
372
- case 0 : // Code 0 indicates "Ok"
396
+ case 0 : {
397
+ // Code 0 indicates "Ok"
373
398
const okValueCalldata = calldata . slice ( 1 ) ;
374
- return { ok : decodeCalldataValue ( okValueCalldata , okType , structs , enums ) } ;
375
- default : // Non-zero code indicates "Err"
399
+ return { ok : decodeCalldataValue ( okValueCalldata , okType , structs , enums ) } ;
400
+ }
401
+ default : {
402
+ // Non-zero code indicates "Err"
376
403
const errValueCalldata = calldata . slice ( 1 ) ;
377
404
return { err : decodeCalldataValue ( errValueCalldata , errType , structs , enums ) } ;
405
+ }
378
406
}
379
407
}
380
408
@@ -420,14 +448,15 @@ export function decodeCalldataField(
420
448
structs : AbiStructs ,
421
449
enums : AbiEnums
422
450
) : any {
423
- const { name , type } = input ;
451
+ const { type } = input ;
424
452
425
453
switch ( true ) {
426
-
427
454
// Handling Array types
428
455
case isTypeArray ( type ) : {
429
456
const elementType = getArrayType ( type ) ;
430
- return calldata . map ( elementCalldata => decodeCalldataValue ( [ elementCalldata ] , elementType , structs , enums ) ) ;
457
+ return calldata . map ( ( elementCalldata ) =>
458
+ decodeCalldataValue ( [ elementCalldata ] , elementType , structs , enums )
459
+ ) ;
431
460
}
432
461
433
462
// Handling StarkNet addresses
0 commit comments