Skip to content

Commit 00e104c

Browse files
committed
test(calldata): add initial test for callDataDecoder
1 parent 14fb955 commit 00e104c

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { parseCalldataField } from '../../src/utils/calldata/requestParser';
2+
import { decodeCalldataField } from '../../src/utils/calldata/calldataDecoder';
3+
import assert from '../../src/utils/assert';
4+
import { CairoUint256 } from '../../src/utils/cairoDataTypes/uint256';
5+
6+
import { AbiEnums, AbiStructs } from '../../src/types';
7+
8+
describe('Encode-Decode CalldataField Flow', () => {
9+
it('correctly encodes and decodes various types', () => {
10+
// Setup
11+
const structs: AbiStructs = {
12+
SimpleStruct: {
13+
type: 'struct',
14+
name: 'SimpleStruct',
15+
size: 2,
16+
members: [
17+
{ name: 'id', type: 'felt', offset: 0 },
18+
{ name: 'value', type: 'core::integer::u256', offset: 0 },
19+
],
20+
},
21+
};
22+
const enums: AbiEnums = {}; // Assuming no enums for this test
23+
const simpleStructValue = { id: 123, value: new CairoUint256('0x1') };
24+
25+
// Create a simple iterator for each value
26+
function* createIterator(value: any): Iterator<any> {
27+
yield value;
28+
}
29+
30+
// Encode
31+
const encodedId = parseCalldataField(
32+
createIterator(simpleStructValue.id),
33+
{ name: 'id', type: 'felt' },
34+
structs,
35+
enums
36+
);
37+
const encodedValue = parseCalldataField(
38+
createIterator(simpleStructValue.value.toApiRequest()),
39+
{ name: 'value', type: 'core::integer::u256' },
40+
structs,
41+
enums
42+
);
43+
44+
// Decode
45+
const decodedId = decodeCalldataField(
46+
typeof encodedId === 'string' ? [encodedId] : encodedId,
47+
{ name: 'id', type: 'felt' },
48+
structs,
49+
enums
50+
);
51+
const decodedValue = decodeCalldataField(
52+
typeof encodedValue === 'string' ? [encodedValue] : encodedValue,
53+
{ name: 'value', type: 'core::integer::u256' },
54+
structs,
55+
enums
56+
);
57+
58+
// Assertions
59+
assert(decodedId.toEqual(simpleStructValue.id));
60+
assert(decodedValue.toBigInt().toEqual(simpleStructValue.value.toBigInt()));
61+
});
62+
});

src/utils/calldata/calldataDecoder.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import assert from '../assert';
3838
* @returns The decoded value.
3939
* @throws An error if the type is not recognized.
4040
*/
41-
function decodeBaseType(type: string, calldata: string | string[]): BigNumberish | CairoUint256 {
41+
function decodeBaseTypes(type: string, calldata: string | string[]): BigNumberish | CairoUint256 {
4242
switch (true) {
4343
case CairoUint256.isAbiType(type):
4444
assert(
@@ -116,7 +116,7 @@ function decodeTuple(
116116
break;
117117
}
118118
default: {
119-
const result = decodeBaseType(type, calldata[calldataIndex]);
119+
const result = decodeBaseTypes(type, calldata[calldataIndex]);
120120
decodedElements.push(result);
121121
calldataIndex += 1;
122122
}
@@ -170,7 +170,7 @@ function decodeCalldataValue(
170170
): any {
171171
// Felt type decoding
172172
if (isTypeFelt(type)) {
173-
return decodeBaseType(type, Array.isArray(calldata) ? calldata[0] : calldata);
173+
return decodeBaseTypes(type, Array.isArray(calldata) ? calldata[0] : calldata);
174174
}
175175

176176
// Bytes31 decoding
@@ -180,7 +180,7 @@ function decodeCalldataValue(
180180

181181
// CairoUint256
182182
if (CairoUint256.isAbiType(type)) {
183-
return decodeBaseType(type, Array.isArray(calldata) ? calldata[0] : calldata);
183+
return decodeBaseTypes(type, Array.isArray(calldata) ? calldata[0] : calldata);
184184
}
185185

186186
// Struct decoding
@@ -485,7 +485,7 @@ export function decodeCalldataField(
485485
}
486486

487487
default: {
488-
return decodeBaseType(calldata[0], type);
488+
return decodeBaseTypes(calldata[0], type);
489489
}
490490
}
491491
}

0 commit comments

Comments
 (0)