Skip to content

Commit 4b6c6e7

Browse files
committed
initial commit after branch change
1 parent 23ae319 commit 4b6c6e7

File tree

3 files changed

+573
-0
lines changed

3 files changed

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

src/utils/cairoDataTypes/uint256.ts

+20
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,24 @@ export class CairoUint256 {
133133
toApiRequest() {
134134
return [CairoFelt(this.low), CairoFelt(this.high)];
135135
}
136+
137+
/**
138+
* Construct CairoUint256 from calldata
139+
* @param calldata Array of two strings representing the low and high parts of a uint256.
140+
*/
141+
static fromCalldata(calldata: [string, string]): CairoUint256 {
142+
if (calldata.length !== 2) {
143+
throw new Error(
144+
'Calldata must contain exactly two elements for low and high parts of uint256.'
145+
);
146+
}
147+
148+
// Validate each part to ensure they are within the acceptable range.
149+
const [low, high] = calldata;
150+
const validatedLow = CairoUint256.validateProps(low, UINT_256_LOW_MIN.toString());
151+
const validatedHigh = CairoUint256.validateProps(high, UINT_256_HIGH_MIN.toString());
152+
153+
// Construct a new instance based on the validated low and high parts.
154+
return new CairoUint256(validatedLow.low, validatedHigh.high);
155+
}
136156
}

0 commit comments

Comments
 (0)