@@ -12,6 +12,7 @@ import {
1212 StructAbi ,
1313} from '../../types' ;
1414import { CairoUint256 } from '../cairoDataTypes/uint256' ;
15+ import { CairoUint512 } from '../cairoDataTypes/uint512' ;
1516import {
1617 isTypeFelt ,
1718 getArrayType ,
@@ -44,18 +45,32 @@ import assert from '../assert';
4445function decodeBaseTypes ( type : string , calldata : string | string [ ] ) : BigNumberish | CairoUint256 {
4546 switch ( true ) {
4647 case CairoUint256 . isAbiType ( type ) :
47- assert (
48- Array . isArray ( calldata ) && calldata . length === 2 ,
49- 'Expected calldata for CairoUint256 as an array of two strings.'
50- ) ;
51- return CairoUint256 . fromCalldata ( [ calldata [ 0 ] , calldata [ 1 ] ] ) ;
48+ if ( Array . isArray ( calldata ) && calldata . length === 2 ) {
49+ return new CairoUint256 ( calldata [ 0 ] , calldata [ 1 ] ) . toBigInt ( ) ;
50+ } else {
51+ throw new Error ( 'Expected calldata for CairoUint256 as an array of two strings.' ) ;
52+ }
53+
54+ case CairoUint512 . isAbiType ( type ) :
55+ if ( Array . isArray ( calldata ) && calldata . length === 2 ) {
56+ return new CairoUint512 ( calldata [ 0 ] , calldata [ 1 ] , calldata [ 2 ] , calldata [ 3 ] ) . toBigInt ( ) ;
57+ } else {
58+ throw new Error ( 'Expected calldata for CairoUint256 as an array of two strings.' ) ;
59+ }
5260
5361 case isTypeBytes31 ( type ) :
54- return decodeShortString ( calldata as string ) ;
62+ if ( typeof calldata === 'string' ) {
63+ return decodeShortString ( calldata ) ;
64+ } else {
65+ throw new Error ( 'Expected single string calldata for type `bytes31`.' ) ;
66+ }
5567
5668 case isTypeFelt ( type ) :
57- assert ( typeof calldata === 'string' , 'Expected string calldata for base type decoding.' ) ;
58- return BigInt ( calldata ) ;
69+ if ( typeof calldata === 'string' ) {
70+ return BigInt ( calldata ) ;
71+ } else {
72+ throw new Error ( 'Expected single string calldata for type `felt`.' ) ;
73+ }
5974
6075 default :
6176 throw new Error ( `Unrecognized base type ${ type } for calldata decoding.` ) ;
@@ -156,91 +171,6 @@ function decodeByteArray(calldata: string[]): ByteArray {
156171 } ;
157172}
158173
159- // /**
160- // * Decode calldata for a given type.
161- // * @param calldata The calldata array.
162- // * @param type The type string.
163- // * @param structs The ABI structs.
164- // * @param enums The ABI enums.
165- // * @returns The decoded value.
166- // * @throws An error if the type is not recognized.
167- // */
168- // function decodeCalldataValue(
169- // calldata: string | string[],
170- // type: string,
171- // structs: AbiStructs,
172- // enums: AbiEnums
173- // ): any {
174- // // Felt type decoding
175- // if (isTypeFelt(type)) {
176- // return decodeBaseTypes(type, Array.isArray(calldata) ? calldata[0] : calldata);
177- // }
178-
179- // // Bytes31 decoding
180- // if (isTypeBytes31(type)) {
181- // return decodeShortString(calldata as string);
182- // }
183-
184- // // CairoUint256
185- // if (CairoUint256.isAbiType(type)) {
186- // return decodeBaseTypes(type, Array.isArray(calldata) ? calldata[0] : calldata);
187- // }
188-
189- // // Struct decoding
190- // if (isTypeStruct(type, structs)) {
191- // return decodeStruct(Array.isArray(calldata) ? calldata : [calldata], type, structs, enums);
192- // }
193-
194- // // Enum decoding
195- // if (isTypeEnum(type, enums)) {
196- // return decodeEnum(Array.isArray(calldata) ? calldata : [calldata], type, enums);
197- // }
198-
199- // // Array decoding
200- // if (isTypeArray(type)) {
201- // return decodeArray(Array.isArray(calldata) ? calldata : [calldata], type, structs, enums);
202- // }
203-
204- // // Tuple decoding
205- // if (isTypeTuple(type)) {
206- // return decodeTuple(Array.isArray(calldata) ? calldata : [calldata], type, structs, enums);
207- // }
208-
209- // // CairoOption decoding
210- // if (isTypeOption(type)) {
211- // const match = type.match(/Option<(.*)>/);
212- // assert(match !== null, `Type "${type}" is not a valid Option type.`);
213-
214- // const innerType = match![1];
215- // return decodeCairoOption(
216- // Array.isArray(calldata) ? calldata : [calldata],
217- // innerType,
218- // structs,
219- // enums
220- // );
221- // }
222-
223- // // CairoResult decoding
224- // if (isTypeResult(type)) {
225- // const matches = type.match(/Result<(.+),\s*(.+)>/);
226- // assert(matches !== null && matches.length > 2, `Type "${type}" is not a valid Option type.`);
227-
228- // const okType = matches[1];
229- // const errType = matches[2];
230-
231- // return decodeCairoResult(
232- // Array.isArray(calldata) ? calldata : [calldata],
233- // okType,
234- // errType,
235- // structs,
236- // enums
237- // );
238- // }
239-
240- // // Fallback for unrecognized types
241- // throw new Error(`Unrecognized type ${type} for calldata decoding.`);
242- // }
243-
244174/**
245175 * Decode an array from calldata.
246176 * @param calldata The calldata array.
@@ -361,19 +291,17 @@ function decodeCairoOption(
361291 innerType : string ,
362292 structs : AbiStructs ,
363293 enums : AbiEnums
364- ) : any {
294+ ) : CairoOption < any > {
365295 const optionIndicator = parseInt ( calldata [ 0 ] , 10 ) ;
366296
367- switch ( optionIndicator ) {
368- case 0 : {
369- // None
370- return CairoOptionVariant . None ;
371- }
372- default : {
373- // Assuming the value is directly after the indicator
374- const valueCalldata = calldata . slice ( 1 ) ;
375- return decodeCalldataValue ( valueCalldata , innerType , structs , enums ) ;
376- }
297+ if ( optionIndicator === CairoOptionVariant . Some ) {
298+ // Decode the "Some" value content if the indicator shows "Some"
299+ const someValueCalldata = calldata . slice ( 1 ) ;
300+ const someValue = decodeCalldataValue ( someValueCalldata , innerType , structs , enums ) ;
301+ return new CairoOption ( CairoOptionVariant . Some , someValue ) ;
302+ } else {
303+ // Return a CairoOption instance indicating "None" without content
304+ return new CairoOption ( CairoOptionVariant . None ) ;
377305 }
378306}
379307
@@ -392,20 +320,19 @@ function decodeCairoResult(
392320 errType : string ,
393321 structs : AbiStructs ,
394322 enums : AbiEnums
395- ) : any {
323+ ) : CairoResult < any , any > {
396324 const resultIndicator = parseInt ( calldata [ 0 ] , 10 ) ;
397325
398- switch ( resultIndicator ) {
399- case 0 : {
400- // Code 0 indicates "Ok"
401- const okValueCalldata = calldata . slice ( 1 ) ;
402- return { ok : decodeCalldataValue ( okValueCalldata , okType , structs , enums ) } ;
403- }
404- default : {
405- // Non-zero code indicates "Err"
406- const errValueCalldata = calldata . slice ( 1 ) ;
407- return { err : decodeCalldataValue ( errValueCalldata , errType , structs , enums ) } ;
408- }
326+ if ( resultIndicator === CairoResultVariant . Ok ) {
327+ // Handle the "Ok" variant
328+ const okValueCalldata = calldata . slice ( 1 ) ;
329+ const okValue = decodeCalldataValue ( okValueCalldata , okType , structs , enums ) ;
330+ return new CairoResult ( CairoResultVariant . Ok , okValue ) ;
331+ } else {
332+ // Handle the "Err" variant
333+ const errValueCalldata = calldata . slice ( 1 ) ;
334+ const errValue = decodeCalldataValue ( errValueCalldata , errType , structs , enums ) ;
335+ return new CairoResult ( CairoResultVariant . Err , errValue ) ;
409336 }
410337}
411338
@@ -503,30 +430,13 @@ function decodeCalldataValue(
503430 structs : AbiStructs ,
504431 enums : AbiEnums
505432) : any {
506- let singleValue = Array . isArray ( calldata ) && calldata . length === 1 ? calldata [ 0 ] : calldata ;
433+ // Handling for base types directly
434+ if ( CairoUint256 . isAbiType ( type ) || CairoUint512 . isAbiType ( type ) || isTypeFelt ( type ) || isTypeBytes31 ( type ) ) {
435+ return decodeBaseTypes ( type , calldata ) ;
436+ }
507437
438+ // Handling complex types
508439 switch ( true ) {
509- case CairoUint256 . isAbiType ( type ) :
510- assert (
511- Array . isArray ( calldata ) && calldata . length === 2 ,
512- 'Expected calldata for CairoUint256 as an array of two strings.'
513- ) ;
514- return new CairoUint256 ( calldata [ 0 ] , calldata [ 1 ] ) . toBigInt ( ) ;
515-
516- case isTypeFelt ( type ) :
517- assert (
518- typeof singleValue === 'string' ,
519- 'Expected single string calldata for type `felt`.'
520- ) ;
521- return BigInt ( singleValue ) ;
522-
523- case isTypeBytes31 ( type ) :
524- assert (
525- typeof singleValue === 'string' ,
526- 'Expected single string calldata for type `bytes31`.'
527- ) ;
528- return decodeShortString ( singleValue ) ;
529-
530440 case isTypeEnum ( type , enums ) :
531441 return decodeEnum ( calldata as string [ ] , type , enums ) ;
532442
@@ -548,6 +458,7 @@ function decodeCalldataValue(
548458 }
549459}
550460
461+
551462function decodeComplexType (
552463 calldata : Calldata ,
553464 type : string ,
@@ -570,59 +481,3 @@ function decodeComplexType(
570481 throw new Error ( `Unsupported container type: ${ containerType } ` ) ;
571482 }
572483}
573-
574- // /**
575- // * Decode a calldata field.
576- // * @param calldata The calldata array.
577- // * @param input The ABI entry for the field.
578- // * @param structs The ABI structs.
579- // * @param enums The ABI enums.
580- // * @returns The decoded field value.
581- // */
582- // function _decodeCalldataField(
583- // calldata: Calldata,
584- // element: { name: string; type: string },
585- // structs: AbiStructs,
586- // enums: AbiEnums
587- // ): any {
588- // const { name, type } = element;
589-
590- // switch (true) {
591- // // Handling Array types
592- // case isTypeArray(type): {
593- // const elementType = getArrayType(type);
594- // return calldata.map((elementCalldata) =>
595- // decodeCalldataValue([elementCalldata], elementType, structs, enums)
596- // );
597- // }
598-
599- // // Handling StarkNet addresses
600- // case type === 'core::starknet::eth_address::EthAddress': {
601- // // Directly returning the value, assuming it's already in the desired format
602- // return calldata[0];
603- // }
604-
605- // // Handling Struct or Tuple types
606- // case isTypeStruct(type, structs): {
607- // return decodeStruct(calldata, type, structs, enums);
608- // }
609-
610- // case isTypeTuple(type): {
611- // return decodeTuple(calldata, type, structs, enums);
612- // }
613-
614- // // Handling CairoUint256 types
615- // case CairoUint256.isAbiType(type): {
616- // return CairoUint256.fromCalldata([calldata[0], calldata[1]]);
617- // }
618-
619- // // Handling Enums
620- // case isTypeEnum(type, enums): {
621- // return decodeEnum(calldata, type, enums);
622- // }
623-
624- // default: {
625- // return decodeBaseTypes(calldata[0], type);
626- // }
627- // }
628- // }
0 commit comments