From 6bd26a1f31a80cb9b74f695dd6885994a6855673 Mon Sep 17 00:00:00 2001 From: edwin <102215563+taco-paco@users.noreply.github.com> Date: Mon, 7 Oct 2024 17:44:39 +0900 Subject: [PATCH] Fix detection of Abi version in contract with only constructor (#1243) * fix: getAbiVersion doesn't work on contracts without any function * fix: getAbiVersion doesn't work on contracts without any function --- .../onlyConstructor/onlyConstructor.cairo | 15 + .../onlyConstructor/onlyConstructor.casm | 1 + .../onlyConstructor.sierra.json | 399 ++++++++++++++++++ __tests__/cairo1.test.ts | 24 +- __tests__/config/fixtures.ts | 6 + __tests__/factories/abi.ts | 7 + __tests__/utils/calldata/cairo.test.ts | 12 +- src/utils/calldata/cairo.ts | 10 +- 8 files changed, 467 insertions(+), 7 deletions(-) create mode 100644 __mocks__/cairo/onlyConstructor/onlyConstructor.cairo create mode 100644 __mocks__/cairo/onlyConstructor/onlyConstructor.casm create mode 100644 __mocks__/cairo/onlyConstructor/onlyConstructor.sierra.json diff --git a/__mocks__/cairo/onlyConstructor/onlyConstructor.cairo b/__mocks__/cairo/onlyConstructor/onlyConstructor.cairo new file mode 100644 index 000000000..bb40042e5 --- /dev/null +++ b/__mocks__/cairo/onlyConstructor/onlyConstructor.cairo @@ -0,0 +1,15 @@ +#[starknet::contract] +pub mod OnlyConstructor { + use starknet::ContractAddress; + use starknet::storage::Map; + + #[storage] + struct Storage { + names: Map::, + } + + #[constructor] + fn constructor(ref self: ContractState, name: felt252, address: ContractAddress) { + self.names.write(address, name); + } +} diff --git a/__mocks__/cairo/onlyConstructor/onlyConstructor.casm b/__mocks__/cairo/onlyConstructor/onlyConstructor.casm new file mode 100644 index 000000000..2438f2104 --- /dev/null +++ b/__mocks__/cairo/onlyConstructor/onlyConstructor.casm @@ -0,0 +1 @@ +{"prime":"0x800000000000011000000000000000000000000000000000000000000000001","compiler_version":"2.8.2","bytecode":["0xa0680017fff8000","0x7","0x482680017ffa8000","0x100000000000000000000000000000000","0x400280007ff97fff","0x10780017fff7fff","0xec","0x4825800180007ffa","0x0","0x400280007ff97fff","0x482680017ff98000","0x1","0x48297ffc80007ffd","0x20680017fff7fff","0x4","0x10780017fff7fff","0xa","0x482680017ffc8000","0x1","0x480a7ffd7fff8000","0x480680017fff8000","0x0","0x480280007ffc8000","0x10780017fff7fff","0x8","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x20680017fff7ffe","0xc3","0x48307ffc80007ffd","0x20680017fff7fff","0x4","0x10780017fff7fff","0xa","0x482480017ffb8000","0x1","0x48127ffb7fff8000","0x480680017fff8000","0x0","0x480080007ff88000","0x10780017fff7fff","0x8","0x48127ffb7fff8000","0x48127ffb7fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x20680017fff7ffe","0x9c","0xa0680017fff8004","0xe","0x4824800180047ffe","0x800000000000000000000000000000000000000000000000000000000000000","0x484480017ffe8000","0x110000000000000000","0x48307ffe7fff8002","0x480080007ff17ffc","0x480080017ff07ffc","0x402480017ffb7ffd","0xffffffffffffffeeffffffffffffffff","0x400080027fef7ffd","0x10780017fff7fff","0x8a","0x484480017fff8001","0x8000000000000000000000000000000","0x48307fff80007ffd","0x480080007ff27ffd","0x480080017ff17ffd","0x402480017ffc7ffe","0xf8000000000000000000000000000000","0x400080027ff07ffe","0x482480017ff08000","0x3","0x48307ff680007ff7","0x20680017fff7fff","0x4","0x10780017fff7fff","0x11","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473","0x400080007ffe7fff","0x480a7ff87fff8000","0x48127ffb7fff8000","0x48127fe97fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ff97fff8000","0x482480017ff88000","0x1","0x208b7fff7fff7ffe","0x1104800180018000","0x9f","0x482480017fff8000","0x9e","0x480080007fff8000","0x480080007fff8000","0x482480017fff8000","0x1eb4","0xa0680017fff8000","0x8","0x48307ffe80007fe6","0x482480017fff8000","0x100000000000000000000000000000000","0x400080007ff57fff","0x10780017fff7fff","0x4c","0x48307ffe80007fe6","0x400080007ff67fff","0x480680017fff8000","0x968a09a4841848cf6a616f8edef20d474b416f4e8fa338d2c6ff1c1b7cda16","0x400280007ff87fff","0x400280017ff87fef","0x480280027ff88000","0xa0680017fff8005","0xe","0x4824800180057ffe","0x7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00","0x484480017ffe8000","0x110000000000000000","0x48307ffe7fff8003","0x480080017ff07ffc","0x480080027fef7ffc","0x482480017ffb7ffd","0xffffffffffffffeefffffffffffffeff","0x400080037fed7ffc","0x10780017fff7fff","0x11","0x48127ffe7fff8005","0x484480017ffe8000","0x8000000000000000000000000000000","0x48307ffe7fff8003","0x480080017ff07ffd","0x482480017ffc7ffe","0xf0000000000000000000000000000100","0x480080027fee7ffd","0x400080037fed7ff9","0x402480017ffd7ff9","0xffffffffffffffffffffffffffffffff","0x20680017fff7ffd","0x4","0x402780017fff7fff","0x1","0x480680017fff8000","0x0","0x482680017ff88000","0x3","0x482480017feb8000","0x4","0x480680017fff8000","0x53746f726167655772697465","0x400280007ffb7fff","0x400280017ffb7ff2","0x400280027ffb7ffc","0x400280037ffb7ffb","0x400280047ffb7fde","0x480280067ffb8000","0x20680017fff7fff","0xe","0x40780017fff7fff","0x1","0x48127ffb7fff8000","0x48127ffb7fff8000","0x480280057ffb8000","0x482680017ffb8000","0x7","0x480680017fff8000","0x0","0x48127ffa7fff8000","0x48127ff97fff8000","0x208b7fff7fff7ffe","0x48127ffc7fff8000","0x48127ffc7fff8000","0x480280057ffb8000","0x482680017ffb8000","0x9","0x480680017fff8000","0x1","0x480280077ffb8000","0x480280087ffb8000","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4f7574206f6620676173","0x400080007ffe7fff","0x480a7ff87fff8000","0x482480017ff28000","0x1","0x48127fe07fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ff97fff8000","0x482480017ff88000","0x1","0x208b7fff7fff7ffe","0x482480017fef8000","0x3","0x10780017fff7fff","0x5","0x40780017fff7fff","0x6","0x48127fef7fff8000","0x40780017fff7fff","0x1","0x480680017fff8000","0x4661696c656420746f20646573657269616c697a6520706172616d202332","0x400080007ffe7fff","0x480a7ff87fff8000","0x48127ffc7fff8000","0x48127fe97fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ff97fff8000","0x482480017ff88000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4661696c656420746f20646573657269616c697a6520706172616d202331","0x400080007ffe7fff","0x480a7ff87fff8000","0x48127ff77fff8000","0x48127ff57fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ff97fff8000","0x482480017ff88000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4f7574206f6620676173","0x400080007ffe7fff","0x480a7ff87fff8000","0x482680017ff98000","0x1","0x480a7ffa7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ff97fff8000","0x482480017ff88000","0x1","0x208b7fff7fff7ffe"],"bytecode_segment_lengths":[257],"hints":[[0,[{"TestLessThanOrEqual":{"lhs":{"Immediate":"0x0"},"rhs":{"Deref":{"register":"FP","offset":-6}},"dst":{"register":"AP","offset":0}}}]],[54,[{"TestLessThan":{"lhs":{"Deref":{"register":"AP","offset":-1}},"rhs":{"Immediate":"0x800000000000000000000000000000000000000000000000000000000000000"},"dst":{"register":"AP","offset":4}}}]],[58,[{"LinearSplit":{"value":{"Deref":{"register":"AP","offset":3}},"scalar":{"Immediate":"0x110000000000000000"},"max_x":{"Immediate":"0xffffffffffffffffffffffffffffffff"},"x":{"register":"AP","offset":-2},"y":{"register":"AP","offset":-1}}}]],[68,[{"LinearSplit":{"value":{"Deref":{"register":"AP","offset":-2}},"scalar":{"Immediate":"0x8000000000000000000000000000000"},"max_x":{"Immediate":"0xffffffffffffffffffffffffffffffff"},"x":{"register":"AP","offset":-1},"y":{"register":"AP","offset":0}}}]],[83,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[106,[{"TestLessThanOrEqual":{"lhs":{"Deref":{"register":"AP","offset":-1}},"rhs":{"Deref":{"register":"AP","offset":-25}},"dst":{"register":"AP","offset":0}}}]],[121,[{"TestLessThan":{"lhs":{"Deref":{"register":"AP","offset":-1}},"rhs":{"Immediate":"0x7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00"},"dst":{"register":"AP","offset":5}}}]],[125,[{"LinearSplit":{"value":{"Deref":{"register":"AP","offset":4}},"scalar":{"Immediate":"0x110000000000000000"},"max_x":{"Immediate":"0xffffffffffffffffffffffffffffffff"},"x":{"register":"AP","offset":-2},"y":{"register":"AP","offset":-1}}}]],[136,[{"LinearSplit":{"value":{"Deref":{"register":"AP","offset":4}},"scalar":{"Immediate":"0x8000000000000000000000000000000"},"max_x":{"Immediate":"0xfffffffffffffffffffffffffffffffe"},"x":{"register":"AP","offset":-2},"y":{"register":"AP","offset":-1}}}]],[163,[{"SystemCall":{"system":{"Deref":{"register":"FP","offset":-5}}}}]],[166,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[188,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[211,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[226,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[241,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]]],"entry_points_by_type":{"EXTERNAL":[],"L1_HANDLER":[],"CONSTRUCTOR":[{"selector":"0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194","offset":0,"builtins":["pedersen","range_check"]}]}} \ No newline at end of file diff --git a/__mocks__/cairo/onlyConstructor/onlyConstructor.sierra.json b/__mocks__/cairo/onlyConstructor/onlyConstructor.sierra.json new file mode 100644 index 000000000..b275b8728 --- /dev/null +++ b/__mocks__/cairo/onlyConstructor/onlyConstructor.sierra.json @@ -0,0 +1,399 @@ +{ + "sierra_program": [ + "0x1", + "0x6", + "0x0", + "0x2", + "0x8", + "0x2", + "0xae", + "0x52", + "0x1b", + "0x52616e6765436865636b", + "0x800000000000000100000000000000000000000000000000", + "0x436f6e7374", + "0x800000000000000000000000000000000000000000000002", + "0x1", + "0xc", + "0x2", + "0x4661696c656420746f20646573657269616c697a6520706172616d202331", + "0x4661696c656420746f20646573657269616c697a6520706172616d202332", + "0x4f7574206f6620676173", + "0x4172726179", + "0x800000000000000300000000000000000000000000000001", + "0x536e617073686f74", + "0x800000000000000700000000000000000000000000000001", + "0x4", + "0x537472756374", + "0x800000000000000700000000000000000000000000000002", + "0x0", + "0x1baeba72e79e9db2587cf44fedb2f3700b2075a5e8e39a562584862c4b71f62", + "0x5", + "0x2ee1e2b1b89f8c495f200e4956278a4d47395fe262f27b52e5865c9524c08c3", + "0x6", + "0x9", + "0x753332", + "0x800000000000000700000000000000000000000000000000", + "0x53746f7261676541646472657373", + "0x53746f726167654261736541646472657373", + "0x66656c74323532", + "0x3f3c5f6678e8adc54edf446ed6e5633dd551707921221d95b17c3569cb79a", + "0x968a09a4841848cf6a616f8edef20d474b416f4e8fa338d2c6ff1c1b7cda16", + "0x4275696c74696e436f737473", + "0x53797374656d", + "0x506564657273656e", + "0x800000000000000f00000000000000000000000000000001", + "0x16a4c8d7c05909052238a862d8cc3e7975bf05a07b3a69c6b28951083a6d672", + "0x800000000000000300000000000000000000000000000003", + "0x12", + "0x456e756d", + "0x9931c641b913035ae674b400b61a51476d506bbe8bba2ff8a6272790aba9e6", + "0x7", + "0x13", + "0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473", + "0x436f6e747261637441646472657373", + "0x800000000000000700000000000000000000000000000003", + "0x11c6d8087e00642489f92d2821ad6ebd6532ad1a3b6d12833da6d6810391511", + "0x17", + "0x426f78", + "0x4761734275696c74696e", + "0x3a", + "0x7265766f6b655f61705f747261636b696e67", + "0x77697468647261775f676173", + "0x6272616e63685f616c69676e", + "0x7374727563745f6465636f6e737472756374", + "0x656e61626c655f61705f747261636b696e67", + "0x73746f72655f74656d70", + "0x61727261795f736e617073686f745f706f705f66726f6e74", + "0x756e626f78", + "0x72656e616d65", + "0x656e756d5f696e6974", + "0x18", + "0x6a756d70", + "0x7374727563745f636f6e737472756374", + "0x656e756d5f6d61746368", + "0x21adb5788e32c84f69a1863d85ef9394b7bf761a0ce1190f826984e5075c371", + "0x64697361626c655f61705f747261636b696e67", + "0x64726f70", + "0x19", + "0x16", + "0x61727261795f6e6577", + "0x636f6e73745f61735f696d6d656469617465", + "0x15", + "0x61727261795f617070656e64", + "0x14", + "0x11", + "0x1a", + "0x10", + "0x6765745f6275696c74696e5f636f737473", + "0xf", + "0x77697468647261775f6761735f616c6c", + "0x636f6e74726163745f616464726573735f746f5f66656c74323532", + "0xe", + "0xd", + "0x736e617073686f745f74616b65", + "0x706564657273656e", + "0xad292db4ff05a993c318438c1b6c8a8303266af2da151aa28ccece6726f1f1", + "0x73746f726167655f616464726573735f66726f6d5f62617365", + "0x8", + "0x73746f726167655f77726974655f73797363616c6c", + "0x3", + "0xb3", + "0xffffffffffffffff", + "0xa4", + "0xa", + "0xb", + "0x93", + "0x1d", + "0x1c", + "0x22", + "0x1e", + "0x1f", + "0x20", + "0x80", + "0x21", + "0x23", + "0x7b", + "0x24", + "0x25", + "0x26", + "0x3b", + "0x27", + "0x28", + "0x29", + "0x2a", + "0x2b", + "0x2c", + "0x2d", + "0x2e", + "0x2f", + "0x30", + "0x6a", + "0x31", + "0x32", + "0x33", + "0x34", + "0x35", + "0x36", + "0x37", + "0x38", + "0x39", + "0x3c", + "0x3d", + "0x3e", + "0x3f", + "0x40", + "0x41", + "0x60", + "0x42", + "0x43", + "0x44", + "0x45", + "0x46", + "0x47", + "0x48", + "0x49", + "0x4a", + "0x4b", + "0x4c", + "0x4d", + "0x4e", + "0x4f", + "0x50", + "0x51", + "0x52", + "0x53", + "0x54", + "0x85", + "0x55", + "0x56", + "0x57", + "0x58", + "0x59", + "0x5a", + "0x5b", + "0x5c", + "0x5d", + "0x5e", + "0x5f", + "0x61", + "0x62", + "0x63", + "0x64", + "0x65", + "0x66", + "0x6d2", + "0x11100f050e0d06050c0b0a0706050403090706050403080706050403020100", + "0x306051d121110191c191b191a191812071705040316051512111014051312", + "0x29052805271224260f052505151224102312221002210220191f1e07060504", + "0x1233123231023006050e2f2e0506052d122c2615122210192b2a0706050403", + "0x3712073c05073b0605053a0605053906050538120505371236160505351234", + "0x14050542124112403c05053f05073c05073b2e05053e123d3c050537140505", + "0x5053e06050548060505374705054606050545060505424405054243050542", + "0x537124d490505374c0505374b0505374a05053705074905073b2905053e25", + "0x56125512545205053552050542520505535205053e510505461250124f4e05", + "0x4905073b2805053e1605053e0f0505420f0505531258170505375705054612", + "0x512125b051212125a1605054205050546070505462e050542590505461207", + "0x5b05120f1217055b050f055912125b051207125728075c1614075b07070507", + "0x5e052812125b051207120605515e5d075b071705161214055b051405141212", + "0x55e124c055b055d055d124e055b055105171251055b055205571252055b05", + "0x55b052505511225055b05125212125b051207121229051206124a055b054e", + "0x12071247055f49055b074a054e124a055b0529055e124c055b0506055d1229", + "0x571243055b052e052812125b051207123c05602e44075b074c051612125b05", + "0x1260055b0500055e1261055b0544055d1200055b054b0517124b055b054305", + "0x53c055d1264055b056305511263055b05125212125b051207121262051206", + "0x74c12125b0512071267056665055b0760054e1260055b0564055e1261055b", + "0x75b076105161262055b0562051412125b051207126a05696862075b076514", + "0x125b056c052912125b056b052512125b05124a12125b051207126e056d6c6b", + "0x53c1270055b05122e126f055b05124412125b0549054712125b0568054912", + "0x1273055b05717207001272055b05124b1271055b05706f07431270055b0570", + "0x16055b051605631262055b056205141212055b051205601274055b05730561", + "0x2512125b051207127459166212140574055b057405651259055b0559056412", + "0x7776075b0775166259681275055b057505621275055b05126712125b056e05", + "0x7c055b05126b127b055b0568056a12125b05124a12125b051207127a790778", + "0x55b057f057012125b057e056f127f7e075b057d056e127d055b057c056c12", + "0x53c126d31075b057b811259711281055b0581053c1281055b058005571280", + "0x1285055b0512741284055b05830573128382075b056d760772126d055b056d", + "0x498485597714761282055b058205141231055b053105601285055b05850575", + "0x8d075b058c0577128c055b05124412125b051207128b8a8959888786075b07", + "0x55b0590057c1290055b058f057b128f055b058e057a12125b058d0579128e", + "0x5b058705641286055b058605631282055b058205141231055b053105601291", + "0x1292055b05124b12125b051207129187868231140591055b05910565128705", + "0x55b058205141231055b053105601294055b059305611293055b058b920700", + "0x8a898231140594055b05940565128a055b058a05641289055b058905631282", + "0x5124412125b0549054712125b0568054912125b05124a12125b0512071294", + "0x5124b1297055b05969507431296055b0596053c1296055b05127d1295055b", + "0x5141212055b05120560129a055b059905611299055b05979807001298055b", + "0x14059a055b059a05651259055b05590564127a055b057a05631279055b0579", + "0x5b056a051412125b0549054712125b0561052512125b051207129a597a7912", + "0x54712125b0561052512125b0567057e12125b05120712129c051206129b05", + "0x55b05127f129d055b05124412125b05124a129b055b0514051412125b0549", + "0x59fa0070012a0055b05124b129f055b059e9d0743129e055b059e053c129e", + "0x160563129b055b059b05141212055b0512056012a2055b05a1056112a1055b", + "0x5120712a259169b121405a2055b05a205651259055b055905641216055b05", + "0x8012a3055b05124412125b054c052512125b0547057e12125b05124a12125b", + "0x12a6055b05124b12a5055b05a4a3074312a4055b05a4053c12a4055b0512", + "0x14055b051405141212055b051205601288055b05a7056112a7055b05a5a607", + "0x8859161412140588055b058805651259055b055905641216055b0516056312", + "0xa9053c12a9055b05127d12a8055b05124412125b050f058112125b05120712", + "0x6112ac055b05aaab070012ab055b05124b12aa055b05a9a8074312a9055b05", + "0x1257055b055705631228055b052805141212055b0512056012ad055b05ac05", + "0x4a14164c4b124a1405ad595728121405ad055b05ad05651259055b05590564", + "0x120f59070512494c4b12" + ], + "sierra_program_debug_info": { + "type_names": [ + [0, "RangeCheck"], + [ + 1, + "Const" + ], + [ + 2, + "Const" + ], + [3, "Const"], + [4, "Array"], + [5, "Snapshot>"], + [6, "core::array::Span::"], + [7, "Tuple>"], + [8, "Const"], + [9, "u32"], + [10, "StorageAddress"], + [11, "StorageBaseAddress"], + [12, "felt252"], + [ + 13, + "core::starknet::storage::storage_base::StorageBase::>>" + ], + [ + 14, + "Const" + ], + [15, "BuiltinCosts"], + [16, "System"], + [17, "Pedersen"], + [18, "core::panics::Panic"], + [19, "Tuple>"], + [20, "core::panics::PanicResult::<(core::array::Span::,)>"], + [21, "Const"], + [22, "ContractAddress"], + [23, "Unit"], + [24, "core::option::Option::"], + [25, "Box"], + [26, "GasBuiltin"] + ], + "libfunc_names": [ + [0, "revoke_ap_tracking"], + [1, "withdraw_gas"], + [2, "branch_align"], + [3, "struct_deconstruct>"], + [4, "enable_ap_tracking"], + [5, "store_temp"], + [6, "array_snapshot_pop_front"], + [7, "unbox"], + [8, "rename"], + [9, "enum_init, 0>"], + [10, "store_temp>>"], + [11, "store_temp>"], + [12, "jump"], + [13, "struct_construct"], + [14, "enum_init, 1>"], + [15, "enum_match>"], + [16, "contract_address_try_from_felt252"], + [17, "disable_ap_tracking"], + [18, "drop>>"], + [19, "drop>"], + [20, "drop"], + [21, "drop"], + [22, "array_new"], + [ + 23, + "const_as_immediate>" + ], + [24, "store_temp"], + [25, "array_append"], + [26, "struct_construct"], + [27, "struct_construct>>"], + [28, "enum_init,)>, 1>"], + [29, "store_temp"], + [30, "store_temp"], + [31, "store_temp"], + [32, "store_temp,)>>"], + [33, "get_builtin_costs"], + [34, "store_temp"], + [35, "withdraw_gas_all"], + [36, "contract_address_to_felt252"], + [ + 37, + "const_as_immediate>" + ], + [ + 38, + "struct_construct>>>" + ], + [ + 39, + "snapshot_take>>>" + ], + [ + 40, + "drop>>>" + ], + [ + 41, + "struct_deconstruct>>>" + ], + [42, "pedersen"], + [43, "storage_base_address_from_felt252"], + [44, "storage_address_from_base"], + [45, "const_as_immediate>"], + [46, "store_temp"], + [47, "storage_write_syscall"], + [48, "snapshot_take>"], + [49, "drop>"], + [50, "struct_construct>"], + [51, "struct_construct>>"], + [52, "enum_init,)>, 0>"], + [53, "const_as_immediate>"], + [54, "drop"], + [ + 55, + "const_as_immediate>" + ], + [ + 56, + "const_as_immediate>" + ], + [57, "drop>"] + ], + "user_func_names": [[0, "starknet_hello_world::OnlyConstructor::__wrapper__constructor"]] + }, + "contract_class_version": "0.1.0", + "entry_points_by_type": { + "EXTERNAL": [], + "L1_HANDLER": [], + "CONSTRUCTOR": [ + { + "selector": "0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194", + "function_idx": 0 + } + ] + }, + "abi": [ + { + "type": "constructor", + "name": "constructor", + "inputs": [ + { "name": "name", "type": "core::felt252" }, + { "name": "address", "type": "core::starknet::contract_address::ContractAddress" } + ] + }, + { + "type": "event", + "name": "starknet_hello_world::OnlyConstructor::Event", + "kind": "enum", + "variants": [] + } + ] +} diff --git a/__tests__/cairo1.test.ts b/__tests__/cairo1.test.ts index 20f636226..9fdd55fdb 100644 --- a/__tests__/cairo1.test.ts +++ b/__tests__/cairo1.test.ts @@ -24,6 +24,8 @@ import { compiledComplexSierra, compiledHelloSierra, compiledHelloSierraCasm, + compiledOnlyConstructorSierra, + compiledOnlyConstructorCasm, describeIfDevnet, getTestAccount, getTestProvider, @@ -40,6 +42,7 @@ describeIfDevnet('Cairo 1 Devnet', () => { const account = getTestAccount(provider); let dd: DeclareDeployUDCResponse; let cairo1Contract: Contract; + let onlyConstructorContract: Contract; initializeMatcher(expect); beforeAll(async () => { @@ -49,6 +52,18 @@ describeIfDevnet('Cairo 1 Devnet', () => { }); cairo1Contract = new Contract(compiledHelloSierra.abi, dd.deploy.contract_address, account); + + const ddOnlyConstructor = await account.declareAndDeploy({ + contract: compiledOnlyConstructorSierra, + casm: compiledOnlyConstructorCasm, + constructorCalldata: [101, account.address], + }); + + onlyConstructorContract = new Contract( + compiledOnlyConstructorSierra.abi, + ddOnlyConstructor.deploy.contract_address, + account + ); }); test('Declare & deploy v2 - Hello Cairo 1 contract', async () => { @@ -105,9 +120,14 @@ describeIfDevnet('Cairo 1 Devnet', () => { }); test('isCairo1', async () => { - const isContractCairo1 = cairo1Contract.isCairo1(); + let isContractCairo1 = cairo1Contract.isCairo1(); + expect(isContractCairo1).toBe(true); + let isAbiCairo1 = isCairo1Abi(cairo1Contract.abi); + expect(isAbiCairo1).toBe(true); + + isContractCairo1 = onlyConstructorContract.isCairo1(); expect(isContractCairo1).toBe(true); - const isAbiCairo1 = isCairo1Abi(cairo1Contract.abi); + isAbiCairo1 = isCairo1Abi(onlyConstructorContract.abi); expect(isAbiCairo1).toBe(true); }); diff --git a/__tests__/config/fixtures.ts b/__tests__/config/fixtures.ts index 818472344..80cd73c25 100644 --- a/__tests__/config/fixtures.ts +++ b/__tests__/config/fixtures.ts @@ -80,6 +80,12 @@ export const compiledSidMulticall = readContractSierra('starknetId/multicall/mul export const compiledSidMulticallCasm = readContractSierraCasm('starknetId/multicall/multicall'); export const compiledNonZero = readContractSierra('cairo/cairo263/zeroable.sierra'); export const compiledNonZeroCasm = readContractSierraCasm('cairo/cairo263/zeroable'); +export const compiledOnlyConstructorSierra = readContractSierra( + 'cairo/onlyConstructor/onlyConstructor.sierra' +); +export const compiledOnlyConstructorCasm = readContractSierraCasm( + 'cairo/onlyConstructor/onlyConstructor' +); export function getTestProvider( isProvider?: true, diff --git a/__tests__/factories/abi.ts b/__tests__/factories/abi.ts index 84a6d42d3..f2492fca0 100644 --- a/__tests__/factories/abi.ts +++ b/__tests__/factories/abi.ts @@ -10,6 +10,13 @@ export const getFunctionAbi = (inputsType: string): FunctionAbi => ({ type: 'function', }); +export const getConstructorAbi = (inputsType: string): FunctionAbi => ({ + inputs: [getAbiEntry(inputsType)], + name: 'test', + outputs: [getAbiEntry(inputsType)], + type: 'constructor', +}); + export const getInterfaceAbi = (functionAbiType: string = 'struct'): InterfaceAbi => ({ items: [getFunctionAbi(functionAbiType)], name: 'test_interface_abi', diff --git a/__tests__/utils/calldata/cairo.test.ts b/__tests__/utils/calldata/cairo.test.ts index 2c43cda3e..4b7aaf7f7 100644 --- a/__tests__/utils/calldata/cairo.test.ts +++ b/__tests__/utils/calldata/cairo.test.ts @@ -28,7 +28,13 @@ import { felt, } from '../../../src/utils/calldata/cairo'; import { ETH_ADDRESS, Literal, Uint, type ContractVersion, NON_ZERO_PREFIX } from '../../../src'; -import { getFunctionAbi, getAbiEnums, getAbiStructs, getInterfaceAbi } from '../../factories/abi'; +import { + getFunctionAbi, + getAbiEnums, + getAbiStructs, + getInterfaceAbi, + getConstructorAbi, +} from '../../factories/abi'; describe('isLen', () => { test('should return true if name ends with "_len"', () => { @@ -254,6 +260,10 @@ describe('isCairo1Abi', () => { expect(isCairo1Abi([getFunctionAbi('felt')])).toEqual(false); }); + test('should return false if ABI comes from Cairo 0 contract', () => { + expect(isCairo1Abi([getConstructorAbi('felt')])).toEqual(false); + }); + test('should throw an error if ABI does not come from Cairo 1 contract ', () => { expect(() => isCairo1Abi([{}])).toThrow(new Error('Unable to determine Cairo version')); }); diff --git a/src/utils/calldata/cairo.ts b/src/utils/calldata/cairo.ts index 88ca11801..253db4055 100644 --- a/src/utils/calldata/cairo.ts +++ b/src/utils/calldata/cairo.ts @@ -203,14 +203,16 @@ export function getAbiContractVersion(abi: Abi): ContractVersion { // determine by function io types "Cairo 1.1" or "Cairo 0.0" // find first function with inputs or outputs - const testFunction = abi.find( - (it) => it.type === 'function' && (it.inputs.length || it.outputs.length) + const testSubject = abi.find( + (it) => + (it.type === 'function' || it.type === 'constructor') && + (it.inputs.length || it.outputs.length) ); - if (!testFunction) { + if (!testSubject) { return { cairo: undefined, compiler: undefined }; } - const io = testFunction.inputs.length ? testFunction.inputs : testFunction.outputs; + const io = testSubject.inputs.length ? testSubject.inputs : testSubject.outputs; if (isCairo1Type(io[0].type)) { return { cairo: '1', compiler: '1' }; }