-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(calldata): add initial test for callDataDecoder
- Loading branch information
1 parent
14fb955
commit 00e104c
Showing
2 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters