Skip to content

Commit f1744b8

Browse files
authored
Merge pull request #936 from haroune-mohammedi/test/cairo240-typed
Test: Cairo 2.4 typed
2 parents a673265 + 2840fe0 commit f1744b8

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

__mocks__/cairo/cairo240/string.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
export const ABI = [
2+
{
3+
type: 'impl',
4+
name: 'TestReject',
5+
interface_name: 'string::string::ITestReject',
6+
},
7+
{
8+
type: 'struct',
9+
name: 'core::byte_array::ByteArray',
10+
members: [
11+
{
12+
name: 'data',
13+
type: 'core::array::Array::<core::bytes_31::bytes31>',
14+
},
15+
{
16+
name: 'pending_word',
17+
type: 'core::felt252',
18+
},
19+
{
20+
name: 'pending_word_len',
21+
type: 'core::integer::u32',
22+
},
23+
],
24+
},
25+
{
26+
type: 'interface',
27+
name: 'string::string::ITestReject',
28+
items: [
29+
{
30+
type: 'function',
31+
name: 'proceed_bytes31',
32+
inputs: [
33+
{
34+
name: 'str',
35+
type: 'core::bytes_31::bytes31',
36+
},
37+
],
38+
outputs: [
39+
{
40+
type: 'core::bytes_31::bytes31',
41+
},
42+
],
43+
state_mutability: 'view',
44+
},
45+
{
46+
type: 'function',
47+
name: 'get_string',
48+
inputs: [],
49+
outputs: [
50+
{
51+
type: 'core::byte_array::ByteArray',
52+
},
53+
],
54+
state_mutability: 'view',
55+
},
56+
{
57+
type: 'function',
58+
name: 'proceed_string',
59+
inputs: [
60+
{
61+
name: 'mess',
62+
type: 'core::byte_array::ByteArray',
63+
},
64+
],
65+
outputs: [
66+
{
67+
type: 'core::byte_array::ByteArray',
68+
},
69+
],
70+
state_mutability: 'view',
71+
},
72+
],
73+
},
74+
{
75+
type: 'event',
76+
name: 'string::string::MyTestReject::Event',
77+
kind: 'enum',
78+
variants: [],
79+
},
80+
] as const;

__tests__/cairo1v2_typed.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ABI as StringABI } from '../__mocks__/cairo/cairo240/string';
12
import { tAbi } from '../__mocks__/hellov2';
23
import {
34
Account,
@@ -15,6 +16,7 @@ import {
1516
RawArgsArray,
1617
RawArgsObject,
1718
TypedContractV2,
19+
byteArray,
1820
cairo,
1921
ec,
2022
hash,
@@ -24,6 +26,8 @@ import {
2426
stark,
2527
types,
2628
} from '../src';
29+
import { hexToDecimalString } from '../src/utils/num';
30+
import { encodeShortString } from '../src/utils/shortString';
2731
import {
2832
TEST_TX_VERSION,
2933
compiledC1Account,
@@ -32,6 +36,8 @@ import {
3236
compiledC1v2Casm,
3337
compiledC210,
3438
compiledC210Casm,
39+
compiledC240,
40+
compiledC240Casm,
3541
compiledComplexSierra,
3642
getTestAccount,
3743
getTestProvider,
@@ -912,4 +918,75 @@ describe('Cairo 1', () => {
912918
return expect(events).toStrictEqual(shouldBe);
913919
});
914920
});
921+
922+
describe('cairo v2.4.0 new types', () => {
923+
let stringContract: TypedContractV2<typeof StringABI>;
924+
925+
beforeAll(async () => {
926+
const { deploy } = await account.declareAndDeploy({
927+
contract: compiledC240,
928+
casm: compiledC240Casm,
929+
});
930+
931+
stringContract = new Contract(compiledC240.abi, deploy.contract_address, account).typedv2(
932+
StringABI
933+
);
934+
});
935+
936+
test('bytes31', async () => {
937+
const resp = await stringContract.call('proceed_bytes31', ['AZERTY']);
938+
expect(resp).toBe('AZERTY');
939+
const resp2 = await stringContract.proceed_bytes31('Some String');
940+
expect(resp2).toBe('Some String');
941+
const str = 'TokenName';
942+
const callD1 = CallData.compile([str]);
943+
expect(callD1).toEqual([hexToDecimalString(encodeShortString(str))]);
944+
const callD2 = CallData.compile({ str });
945+
expect(callD2).toEqual([hexToDecimalString(encodeShortString(str))]);
946+
const myCallData = new CallData(compiledC240.abi);
947+
const myCalldata1 = myCallData.compile('proceed_bytes31', [str]);
948+
expect(myCalldata1).toEqual([encodeShortString(str)]);
949+
const myCalldata2 = myCallData.compile('proceed_bytes31', { str });
950+
expect(myCalldata2).toEqual([encodeShortString(str)]);
951+
const myCall1 = stringContract.populate('proceed_bytes31', [str]);
952+
expect(myCall1.calldata).toEqual([encodeShortString(str)]);
953+
const myCall2 = stringContract.populate('proceed_bytes31', { str });
954+
expect(myCall2.calldata).toEqual([encodeShortString(str)]);
955+
});
956+
957+
test('bytes31 too long', async () => {
958+
await expect(stringContract.call('proceed_bytes31', ['ABCDEFGHIJKLMNOPQRSTUVWXYZ12345A'])) // more than 31 characters
959+
.rejects.toThrow();
960+
});
961+
962+
test('ByteArray', async () => {
963+
const message = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ12345AAADEFGHIJKLMNOPQRSTUVWXYZ12345A';
964+
const callD = CallData.compile([message]);
965+
const expectedResult = [
966+
'2',
967+
hexToDecimalString('0x4142434445464748494a4b4c4d4e4f505152535455565758595a3132333435'),
968+
hexToDecimalString('0x4141414445464748494a4b4c4d4e4f505152535455565758595a3132333435'),
969+
hexToDecimalString('0x41'),
970+
'1',
971+
];
972+
expect(callD).toEqual(expectedResult);
973+
const callD2 = CallData.compile({ mess: message });
974+
expect(callD2).toEqual(expectedResult);
975+
const callD3 = CallData.compile({ mess: byteArray.byteArrayFromString('Take care.') });
976+
expect(callD3).toEqual(['1', '0', '398475857363345939260718', '10']);
977+
const str1 = await stringContract.get_string();
978+
expect(str1).toBe(
979+
"Cairo has become the most popular language for developers + charizards !@#$%^&*_+|:'<>?~`"
980+
);
981+
const myCallData = new CallData(stringContract.abi);
982+
const expectedString = 'Take care. Zorg is back';
983+
const resp3 = await stringContract.proceed_string('Take care.');
984+
expect(resp3).toBe(expectedString);
985+
const resp4 = await stringContract.call('proceed_string', ['Take care.']);
986+
expect(resp4).toBe(expectedString);
987+
const calldata1 = myCallData.compile('proceed_string', ['Take care.']);
988+
const resp5 = await stringContract.call('proceed_string', calldata1);
989+
expect(resp5).toBe(expectedString);
990+
});
991+
});
915992
});

0 commit comments

Comments
 (0)