9
9
StructAbi ,
10
10
} from '../../types' ;
11
11
import { CairoUint256 } from '../cairoDataTypes/uint256' ;
12
- import { byteArrayFromString } from './byteArray' ;
13
12
import {
14
13
isTypeFelt ,
15
14
getArrayType ,
@@ -30,7 +29,7 @@ import {
30
29
} from './enum' ;
31
30
import extractTupleMemberTypes from './tuple' ;
32
31
import { decodeShortString } from '../shortString' ;
33
- import { hexToBytes } from '../num ' ;
32
+ import assert from '../assert ' ;
34
33
35
34
/**
36
35
* Decode a base type from calldata.
@@ -42,18 +41,14 @@ import { hexToBytes } from '../num';
42
41
function decodeBaseType ( type : string , calldata : string | string [ ] ) : BigNumberish | CairoUint256 {
43
42
switch ( true ) {
44
43
case CairoUint256 . isAbiType ( type ) :
45
- if ( ! Array . isArray ( calldata ) || calldata . length !== 2 ) {
46
- throw new Error ( 'Expected calldata for CairoUint256 as an array of two strings.' ) ;
47
- }
44
+ assert ( Array . isArray ( calldata ) && calldata . length === 2 , 'Expected calldata for CairoUint256 as an array of two strings.' )
48
45
return CairoUint256 . fromCalldata ( [ calldata [ 0 ] , calldata [ 1 ] ] ) ;
49
46
50
47
case isTypeBytes31 ( type ) :
51
48
return decodeShortString ( calldata as string ) ;
52
49
53
50
case isTypeFelt ( type ) :
54
- if ( typeof calldata !== 'string' ) {
55
- throw new Error ( 'Expected string calldata for base type decoding.' ) ;
56
- }
51
+ assert ( typeof calldata === 'string' , 'Expected string calldata for base type decoding.' )
57
52
return BigInt ( calldata ) ;
58
53
59
54
default :
@@ -198,10 +193,9 @@ function decodeCalldataValue(
198
193
// CairoOption decoding
199
194
if ( isTypeOption ( type ) ) {
200
195
const match = type . match ( / O p t i o n < ( .* ) > / ) ;
201
- if ( ! match ) {
202
- throw new Error ( `Type "${ type } " is not a valid Option type.` ) ;
203
- }
204
- const innerType = match [ 1 ] ;
196
+ assert ( match !== null , `Type "${ type } " is not a valid Option type.` ) ;
197
+
198
+ const innerType = match ! [ 1 ] ;
205
199
return decodeCairoOption (
206
200
Array . isArray ( calldata ) ? calldata : [ calldata ] ,
207
201
innerType ,
@@ -213,9 +207,7 @@ function decodeCalldataValue(
213
207
// CairoResult decoding
214
208
if ( isTypeResult ( type ) ) {
215
209
const matches = type . match ( / R e s u l t < ( .+ ) , \s * ( .+ ) > / ) ;
216
- if ( ! matches || matches . length < 3 ) {
217
- throw new Error ( `Type "${ type } " is not a valid Option type.` ) ;
218
- }
210
+ assert ( matches !== null && matches . length > 2 , `Type "${ type } " is not a valid Option type.` )
219
211
220
212
const okType = matches [ 1 ] ;
221
213
const errType = matches [ 2 ] ;
@@ -273,9 +265,7 @@ function decodeStruct(
273
265
enums : AbiEnums
274
266
) : ParsedStruct {
275
267
const structAbi : StructAbi = structs [ structName ] ;
276
- if ( ! structAbi ) {
277
- throw new Error ( `Struct with name ${ structName } not found.` ) ;
278
- }
268
+ assert ( structAbi !== null , `Struct with name ${ structName } not found.` ) ;
279
269
280
270
let index = 0 ;
281
271
const result : ParsedStruct = { } ;
@@ -300,14 +290,10 @@ function decodeStruct(
300
290
*/
301
291
function decodeEnum ( calldataValues : string [ ] , enumName : string , enums : AbiEnums ) : CairoEnum {
302
292
const enumDefinition = enums [ enumName ] ;
303
- if ( ! enumDefinition ) {
304
- throw new Error ( `Enum with name ${ enumName } not found.` ) ;
305
- }
293
+ assert ( enumDefinition !== null , `Enum with name ${ enumName } not found.` ) ;
306
294
307
295
const variantIndex = parseInt ( calldataValues [ 0 ] , 10 ) ;
308
- if ( variantIndex < 0 || variantIndex >= enumDefinition . variants . length ) {
309
- throw new Error ( `Variant index ${ variantIndex } out of range for enum ${ enumName } .` ) ;
310
- }
296
+ assert ( variantIndex >= 0 && variantIndex < enumDefinition . variants . length , `Variant index ${ variantIndex } out of range for enum ${ enumName } .` ) ;
311
297
312
298
const variant = enumDefinition . variants [ variantIndex ] ;
313
299
@@ -405,7 +391,7 @@ function getExpectedCalldataLengthForEnum(
405
391
enums : AbiEnums
406
392
) : number {
407
393
const enumDefinition = enums [ enumName ] ;
408
- if ( ! enumDefinition ) throw new Error ( `Enum with name ${ enumName } not found.` ) ;
394
+ assert ( enumDefinition , `Enum with name ${ enumName } not found.` ) ;
409
395
410
396
const variantIndex = parseInt ( variantIndexCalldata , 10 ) ;
411
397
const variant = enumDefinition . variants [ variantIndex ] ;
0 commit comments