Skip to content

Commit

Permalink
test(calldata): add initial test for callDataDecoder
Browse files Browse the repository at this point in the history
  • Loading branch information
AryanGodara committed Apr 23, 2024
1 parent 14fb955 commit 00e104c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
62 changes: 62 additions & 0 deletions __tests__/utils/calldataDecode.test.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
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()));
});
});
10 changes: 5 additions & 5 deletions src/utils/calldata/calldataDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -485,7 +485,7 @@ export function decodeCalldataField(
}

default: {
return decodeBaseType(calldata[0], type);
return decodeBaseTypes(calldata[0], type);
}
}
}

0 comments on commit 00e104c

Please sign in to comment.