From 00e104c8cd7be25659d6ab2a7b9366714de9e1a1 Mon Sep 17 00:00:00 2001 From: Aryan Godara Date: Tue, 26 Mar 2024 18:00:31 +0530 Subject: [PATCH] test(calldata): add initial test for callDataDecoder --- __tests__/utils/calldataDecode.test.ts | 62 ++++++++++++++++++++++++++ src/utils/calldata/calldataDecoder.ts | 10 ++--- 2 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 __tests__/utils/calldataDecode.test.ts diff --git a/__tests__/utils/calldataDecode.test.ts b/__tests__/utils/calldataDecode.test.ts new file mode 100644 index 000000000..e4bb68c14 --- /dev/null +++ b/__tests__/utils/calldataDecode.test.ts @@ -0,0 +1,62 @@ +import { parseCalldataField } from '../../src/utils/calldata/requestParser'; +import { decodeCalldataField } from '../../src/utils/calldata/calldataDecoder'; +import assert from '../../src/utils/assert'; +import { CairoUint256 } from '../../src/utils/cairoDataTypes/uint256'; + +import { AbiEnums, AbiStructs } from '../../src/types'; + +describe('Encode-Decode CalldataField Flow', () => { + it('correctly encodes and decodes various types', () => { + // Setup + const structs: AbiStructs = { + SimpleStruct: { + type: 'struct', + name: 'SimpleStruct', + size: 2, + members: [ + { name: 'id', type: 'felt', offset: 0 }, + { name: 'value', type: 'core::integer::u256', offset: 0 }, + ], + }, + }; + const enums: AbiEnums = {}; // Assuming no enums for this test + const simpleStructValue = { id: 123, value: new CairoUint256('0x1') }; + + // Create a simple iterator for each value + function* createIterator(value: any): Iterator { + yield value; + } + + // Encode + const encodedId = parseCalldataField( + createIterator(simpleStructValue.id), + { name: 'id', type: 'felt' }, + structs, + enums + ); + const encodedValue = parseCalldataField( + createIterator(simpleStructValue.value.toApiRequest()), + { name: 'value', type: 'core::integer::u256' }, + structs, + enums + ); + + // Decode + const decodedId = decodeCalldataField( + typeof encodedId === 'string' ? [encodedId] : encodedId, + { name: 'id', type: 'felt' }, + structs, + enums + ); + const decodedValue = decodeCalldataField( + typeof encodedValue === 'string' ? [encodedValue] : encodedValue, + { name: 'value', type: 'core::integer::u256' }, + structs, + enums + ); + + // Assertions + assert(decodedId.toEqual(simpleStructValue.id)); + assert(decodedValue.toBigInt().toEqual(simpleStructValue.value.toBigInt())); + }); +}); diff --git a/src/utils/calldata/calldataDecoder.ts b/src/utils/calldata/calldataDecoder.ts index 4d0117253..46500633b 100644 --- a/src/utils/calldata/calldataDecoder.ts +++ b/src/utils/calldata/calldataDecoder.ts @@ -38,7 +38,7 @@ import assert from '../assert'; * @returns The decoded value. * @throws An error if the type is not recognized. */ -function decodeBaseType(type: string, calldata: string | string[]): BigNumberish | CairoUint256 { +function decodeBaseTypes(type: string, calldata: string | string[]): BigNumberish | CairoUint256 { switch (true) { case CairoUint256.isAbiType(type): assert( @@ -116,7 +116,7 @@ function decodeTuple( break; } default: { - const result = decodeBaseType(type, calldata[calldataIndex]); + const result = decodeBaseTypes(type, calldata[calldataIndex]); decodedElements.push(result); calldataIndex += 1; } @@ -170,7 +170,7 @@ function decodeCalldataValue( ): any { // Felt type decoding if (isTypeFelt(type)) { - return decodeBaseType(type, Array.isArray(calldata) ? calldata[0] : calldata); + return decodeBaseTypes(type, Array.isArray(calldata) ? calldata[0] : calldata); } // Bytes31 decoding @@ -180,7 +180,7 @@ function decodeCalldataValue( // CairoUint256 if (CairoUint256.isAbiType(type)) { - return decodeBaseType(type, Array.isArray(calldata) ? calldata[0] : calldata); + return decodeBaseTypes(type, Array.isArray(calldata) ? calldata[0] : calldata); } // Struct decoding @@ -485,7 +485,7 @@ export function decodeCalldataField( } default: { - return decodeBaseType(calldata[0], type); + return decodeBaseTypes(calldata[0], type); } } }