Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13,873 changes: 13,873 additions & 0 deletions __mocks__/cairo/cairo2120/enums_test_enums.casm

Large diffs are not rendered by default.

6,810 changes: 6,810 additions & 0 deletions __mocks__/cairo/cairo2120/enums_test_enums.sierra.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions __tests__/cairo1v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,21 +696,21 @@ describe('Cairo 1', () => {
{ address: 1193046n, is_claimed: true },
{ address: 624485n, is_claimed: false },
]);
const res4 = c1v2CallData.decodeParameters('core::integer::u8', ['0x123456']);
expect(res4).toBe(1193046n);
const res4 = c1v2CallData.decodeParameters('core::integer::u8', ['0x12']);
expect(res4).toBe(18n);
const res5 = c1v2CallData.decodeParameters('core::bool', ['0x1']);
expect(res5).toBe(true);
const res6 = c1v2CallData.decodeParameters('core::felt252', ['0x123456']);
expect(res6).toBe(1193046n);
const res7 = c1v2CallData.decodeParameters('core::integer::u256', ['0x123456', '0x789']);
expect(num.toHex(res7.toString())).toBe('0x78900000000000000000000000000123456');
const res8 = c1v2CallData.decodeParameters('core::array::Array::<core::integer::u16>', [
const res8 = c1v2CallData.decodeParameters('core::array::Array::<core::integer::u32>', [
'2',
'0x123456',
'0x789',
]);
expect(res8).toEqual([1193046n, 1929n]);
const res9 = c1v2CallData.decodeParameters('core::array::Span::<core::integer::u16>', [
const res9 = c1v2CallData.decodeParameters('core::array::Span::<core::integer::u32>', [
'2',
'0x123456',
'0x789',
Expand Down
8 changes: 4 additions & 4 deletions __tests__/cairo1v2_typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,13 +964,13 @@ describe('Cairo 1', () => {
expect(callD2).toEqual([hexToDecimalString(encodeShortString(str))]);
const myCallData = new CallData(contracts.C240.sierra.abi);
const myCalldata1 = myCallData.compile('proceed_bytes31', [str]);
expect(myCalldata1).toEqual([encodeShortString(str)]);
expect(myCalldata1).toEqual([hexToDecimalString(encodeShortString(str))]);
const myCalldata2 = myCallData.compile('proceed_bytes31', { str });
expect(myCalldata2).toEqual([encodeShortString(str)]);
expect(myCalldata2).toEqual([hexToDecimalString(encodeShortString(str))]);
const myCall1 = stringContract.populate('proceed_bytes31', [str]);
expect(myCall1.calldata).toEqual([encodeShortString(str)]);
expect(myCall1.calldata).toEqual([hexToDecimalString(encodeShortString(str))]);
const myCall2 = stringContract.populate('proceed_bytes31', { str });
expect(myCall2.calldata).toEqual([encodeShortString(str)]);
expect(myCall2.calldata).toEqual([hexToDecimalString(encodeShortString(str))]);
});

test('bytes31 too long', async () => {
Expand Down
10 changes: 5 additions & 5 deletions __tests__/cairov24onward.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ describe('Cairo v2.4 onwards', () => {

const myCallData = new CallData(contracts.C240.sierra.abi);
const myCalldata1 = myCallData.compile('proceed_bytes31', [str]);
expect(myCalldata1).toEqual([encodeShortString(str)]);
expect(myCalldata1).toEqual([hexToDecimalString(encodeShortString(str))]);

const myCalldata2 = myCallData.compile('proceed_bytes31', { str });
expect(myCalldata2).toEqual([encodeShortString(str)]);
expect(myCalldata2).toEqual([hexToDecimalString(encodeShortString(str))]);
const myCall1 = stringContract.populate('proceed_bytes31', [str]);
expect(myCall1.calldata).toEqual([encodeShortString(str)]);
expect(myCall1.calldata).toEqual([hexToDecimalString(encodeShortString(str))]);
const myCall2 = stringContract.populate('proceed_bytes31', { str });
expect(myCall2.calldata).toEqual([encodeShortString(str)]);
expect(myCall2.calldata).toEqual([hexToDecimalString(encodeShortString(str))]);
});

test('bytes31 too long', async () => {
Expand Down Expand Up @@ -439,7 +439,7 @@ describe('Cairo v2.4 onwards', () => {
describe('Cairo v2.9.2 fixed-array', () => {
const myArray: number[] = [1, 2, 3, 4, 5, 6, 7, 8];
const myWrongArray = [...myArray, 9];
const expectedCalldata = myArray.map((val) => `0x${val.toString(16)}`);
const expectedCalldata = myArray.map((val) => val.toString(10));
let fixedArrayContract: Contract;

beforeAll(async () => {
Expand Down
1 change: 1 addition & 0 deletions __tests__/config/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const compiledContracts = {
deployer: 'cairo2100/deployer',
CairoByteArray: 'byteArray/target/dev/test_ByteArrayStorage',
IntegerTypes: 'integerTypes/target/dev/test_IntegerTypesStorage',
TestCairoType: 'cairo2120/enums_test_enums',
};
export const contracts = mapContractSets(compiledContracts);

Expand Down
14 changes: 4 additions & 10 deletions __tests__/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -930,21 +930,15 @@ describe('Complex interaction', () => {
const res0 = myCallData.decodeParameters('core::felt252', ['474107654995566025798705']);
expect(res0).toBe(474107654995566025798705n);
const res1 = myCallData.decodeParameters('echo::StructY', [
'474107654995566025798705',
'1844674407370955',
'3534634645645',
]);
expect(res1).toEqual({ y1: 474107654995566025798705n, y2: 3534634645645n });
expect(res1).toEqual({ y1: 1844674407370955n, y2: 3534634645645n });

const res2 = myCallData.decodeParameters('core::integer::u256', ['47410765', '35346645']);
expect(res2).toBe(12027840023314154934885372750905072692667575885n);
const res3 = myCallData.decodeParameters('echo::Struct32', [
'47410765',
'35346645',
'1',
'2',
'3',
]);
expect(res3).toEqual({ b: 47410765n, c: { '0': 35346645n, '1': 1n, '2': 2n, '3': 3n } });
const res3 = myCallData.decodeParameters('echo::Struct32', ['47410765', '45', '1', '2', '3']);
expect(res3).toEqual({ b: 47410765n, c: { '0': 45n, '1': 1n, '2': 2n, '3': 3n } });

const res4 = myCallData.decodeParameters(
'(core::felt252, core::felt252, core::felt252, core::felt252)',
Expand Down
8 changes: 4 additions & 4 deletions __tests__/utils/cairoDataTypes/CairoArray.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('CairoArray Integration Tests', () => {
const requestParser = parser.getRequestParser('core::array::Array::<core::integer::u8>');

const result = requestParser([1, 2, 3], 'core::array::Array::<core::integer::u8>');
expect(result).toEqual(['3', '0x1', '0x2', '0x3']);
expect(result).toEqual(['3', '1', '2', '3']);
});

test('should work with AbiParser2 response parsing', () => {
Expand All @@ -90,7 +90,7 @@ describe('CairoArray Integration Tests', () => {
[[1, 2], [3]],
'core::array::Array::<core::array::Array::<core::integer::u8>>'
);
expect(result).toEqual(['2', '2', '0x1', '0x2', '1', '0x3']);
expect(result).toEqual(['2', '2', '1', '2', '1', '3']);
});

test('should handle empty arrays in AbiParser2', () => {
Expand Down Expand Up @@ -223,7 +223,7 @@ describe('CairoArray Integration Tests', () => {
});

test('should handle mixed input types with parsing strategy', () => {
const mixedData = [1, '2', 3n, 0x4];
const mixedData = [1, '2', 3n, '0x4'];
const array = new CairoArray(
mixedData,
'core::array::Array::<core::felt252>',
Expand Down Expand Up @@ -254,7 +254,7 @@ describe('CairoArray Integration Tests', () => {

// Both should serialize the same way
expect(arraySerialized).toEqual(spanSerialized);
expect(arraySerialized).toEqual(['3', '0x1', '0x2', '0x3']);
expect(arraySerialized).toEqual(['3', '1', '2', '3']);
});
});

Expand Down
55 changes: 26 additions & 29 deletions __tests__/utils/cairoDataTypes/CairoArray.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ describe('CairoArray class Unit test', () => {
hdParsingStrategy
);
const result = array.toApiRequest();
// Should have length prefix: ['3', '0x1', '0x2', '0x3']
expect(result).toEqual(['3', '0x1', '0x2', '0x3']);
expect(result).toEqual(['3', '1', '2', '3']);
});

test('should create and serialize from object input', () => {
Expand All @@ -231,8 +230,7 @@ describe('CairoArray class Unit test', () => {
hdParsingStrategy
);
const result = array.toApiRequest();
// Should have length prefix: ['3', '0x1', '0x2', '0x3']
expect(result).toEqual(['3', '0x1', '0x2', '0x3']);
expect(result).toEqual(['3', '1', '2', '3']);
});

test('should work with parsing strategy', () => {
Expand All @@ -251,8 +249,8 @@ describe('CairoArray class Unit test', () => {
const result2 = array2.toApiRequest();

// Unified parsing strategy approach for API serialization with length prefix
expect(result1).toEqual(['2', '0x1', '0x2']);
expect(result2).toEqual(['2', '0x1', '0x2']);
expect(result1).toEqual(['2', '1', '2']);
expect(result2).toEqual(['2', '1', '2']);
});

test('should throw for invalid inputs', () => {
Expand All @@ -277,7 +275,7 @@ describe('CairoArray class Unit test', () => {
);
const result = array.toApiRequest();
// Outer length=2, first inner [length=2, 1, 2], second inner [length=2, 3, 4]
expect(result).toEqual(['2', '2', '0x1', '0x2', '2', '0x3', '0x4']);
expect(result).toEqual(['2', '2', '1', '2', '2', '3', '4']);
});

test('should handle edge cases', () => {
Expand All @@ -288,7 +286,7 @@ describe('CairoArray class Unit test', () => {
hdParsingStrategy
);
const emptyResult = emptyArray.toApiRequest();
// Just the length prefix: ['0']
// Just the length prefix: ['0x0']
expect(emptyResult).toEqual(['0']);

// Single element
Expand All @@ -298,7 +296,7 @@ describe('CairoArray class Unit test', () => {
hdParsingStrategy
);
const singleResult = singleArray.toApiRequest();
expect(singleResult).toEqual(['1', '0x2a']);
expect(singleResult).toEqual(['1', '42']);
});
});

Expand All @@ -311,7 +309,7 @@ describe('CairoArray class Unit test', () => {
);
const result = array.toApiRequest();
// Length prefix + elements
expect(result).toEqual(['3', '0x1', '0x2', '0x3']);
expect(result).toEqual(['3', '1', '2', '3']);
});

test('should work with hdParsingStrategy', () => {
Expand All @@ -329,8 +327,8 @@ describe('CairoArray class Unit test', () => {
const result1 = array1.toApiRequest();
const result2 = array2.toApiRequest();

expect(result1).toEqual(['2', '0x64', '0xc8']);
expect(result2).toEqual(['2', '0x64', '0xc8']);
expect(result1).toEqual(['2', '100', '200']);
expect(result2).toEqual(['2', '100', '200']);
});

test('should handle nested arrays with proper length prefixes', () => {
Expand All @@ -344,17 +342,16 @@ describe('CairoArray class Unit test', () => {
);
const result = nestedArray.toApiRequest();
// Outer array: length=2, then two inner arrays each with their own length prefixes
expect(result).toEqual(['2', '2', '0x1', '0x2', '2', '0x3', '0x4']);
expect(result).toEqual(['2', '2', '1', '2', '2', '3', '4']);
});

test('should throw for unsupported element types', () => {
const array = new CairoArray(
[1, 2],
'core::array::Array::<unsupported::type>',
hdParsingStrategy
);
expect(() => {
array.toApiRequest();
new CairoArray(
[1, 2],
'core::array::Array::<unsupported::type>',
hdParsingStrategy
).toApiRequest();
}).toThrow();
});
});
Expand Down Expand Up @@ -387,7 +384,7 @@ describe('CairoArray class Unit test', () => {
expect(largeArray.content.length).toBe(100);
expect(CairoArray.getArrayElementType(largeArray.arrayType)).toBe('core::integer::u8');
const result = largeArray.toApiRequest();
expect(result[0]).toBe('100'); // Length prefix should be '100'
expect(result[0]).toBe('100'); // Length prefix
expect(result.length).toBe(101); // 100 elements + 1 length prefix
});

Expand Down Expand Up @@ -415,23 +412,23 @@ describe('CairoArray class Unit test', () => {
'2',
'2',
'2',
'0x1',
'0x2',
'1',
'2',
'2',
'0x3',
'0x4',
'3',
'4',
'2',
'2',
'0x5',
'0x6',
'5',
'6',
'2',
'0x7',
'0x8',
'7',
'8',
]);
});

test('should handle mixed data types in content', () => {
const mixedContent = [1, '2', 3n, 4];
const mixedContent = [1, '2', 3n, '0x04'];
const mixedArray = new CairoArray(
mixedContent,
'core::array::Array::<core::felt252>',
Expand Down
Loading