1+ import { ABI as StringABI } from '../__mocks__/cairo/cairo240/string' ;
12import { tAbi } from '../__mocks__/hellov2' ;
23import {
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' ;
2731import {
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