|
| 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 | +}); |
0 commit comments