From af268b825b58d41de3b97ea2f396d1b6ca92eee8 Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Mon, 12 Dec 2022 11:55:18 -0300 Subject: [PATCH 1/8] feat: add smart contract example --- deps/std/encoding/base58.ts | 3 +- effects/events.ts | 5 +- effects/rpc_known_clients.ts | 1 + effects/rpc_known_methods.ts | 1 + examples/smart_contract.ts | 159 +++++++++++++++++++++++ examples/smart_contract/codec.ts | 48 +++++++ examples/smart_contract/flipper.contract | 1 + examples/smart_contract/flipper.wasm | Bin 0 -> 15044 bytes examples/smart_contract/metadata.json | 108 +++++++++++++++ 9 files changed, 324 insertions(+), 2 deletions(-) create mode 100644 examples/smart_contract.ts create mode 100644 examples/smart_contract/codec.ts create mode 100644 examples/smart_contract/flipper.contract create mode 100644 examples/smart_contract/flipper.wasm create mode 100644 examples/smart_contract/metadata.json diff --git a/deps/std/encoding/base58.ts b/deps/std/encoding/base58.ts index e94d0fdd6..66333c9b0 100644 --- a/deps/std/encoding/base58.ts +++ b/deps/std/encoding/base58.ts @@ -1 +1,2 @@ -export * from "https://deno.land/std@0.154.0/encoding/base58.ts" +// export * from "https://deno.land/std@0.167.0/encoding/base58.ts" +export * from "https://raw.githubusercontent.com/kratico/deno_std/main/encoding/base58.ts" diff --git a/effects/events.ts b/effects/events.ts index 6bb3be7c8..59661b662 100644 --- a/effects/events.ts +++ b/effects/events.ts @@ -24,7 +24,10 @@ export function events() + .as<{ + event?: Record + phase: { value: number } + }[]>() return Z .ls(idx, events) .next(([idx, events]) => { diff --git a/effects/rpc_known_clients.ts b/effects/rpc_known_clients.ts index 166565742..8ffd8bb41 100644 --- a/effects/rpc_known_clients.ts +++ b/effects/rpc_known_clients.ts @@ -14,3 +14,4 @@ export const moonbeam = proxyClient("wss://wss.api.moonbeam.network") export const statemint = proxyClient("wss://statemint-rpc.polkadot.io") export const subsocial = proxyClient("wss://para.subsocial.network") export const westend = proxyClient("wss://westend-rpc.polkadot.io") +export const local = proxyClient("ws://127.0.0.1:9944") diff --git a/effects/rpc_known_methods.ts b/effects/rpc_known_methods.ts index 0092b1452..f83d55729 100644 --- a/effects/rpc_known_methods.ts +++ b/effects/rpc_known_methods.ts @@ -5,6 +5,7 @@ import { rpcCall, rpcSubscription } from "./rpc.ts" // TODO: generate the following? export namespace state { export const getMetadata = rpcCall<[at?: U.HexHash], U.HexHash>("state_getMetadata") + export const call = rpcCall<[method: string, data: U.Hex], U.HexHash>("state_call") export const getStorage = rpcCall< [key: known.StorageKey, at?: U.HexHash], known.StorageData diff --git a/examples/smart_contract.ts b/examples/smart_contract.ts new file mode 100644 index 000000000..d2d789823 --- /dev/null +++ b/examples/smart_contract.ts @@ -0,0 +1,159 @@ +import * as C from "http://localhost:5646/@local/mod.ts" +import * as T from "http://localhost:5646/@local/test_util/mod.ts" +import * as U from "http://localhost:5646/@local/util/mod.ts" + +import { $contractsApiCallArgs, $contractsApiCallReturn } from "./smart_contract/codec.ts" + +const contract = await getContract( + "./examples/smart_contract/flipper.wasm", + "./examples/smart_contract/metadata.json", +) + +const contractAddress = U.throwIfError(await instantiateContractTx().run()) +console.log("Deployed Contract address", U.ss58.encode(42, contractAddress)) +console.log("get message", U.throwIfError(await sendGetMessage(contractAddress).run())) +console.log("flip message in block", U.throwIfError(await sendFlipMessage(contractAddress).run())) +console.log("get message", U.throwIfError(await sendGetMessage(contractAddress).run())) + +function instantiateContractTx() { + const constructor = findContractConstructorByLabel("default")! + const tx = C.extrinsic(C.local)({ + sender: T.alice.address, + call: { + type: "Contracts", + value: { + type: "instantiateWithCode", + value: 0n, + // TODO: create sendDryRunContractInitiate and fetch these gasLimit value + gasLimit: { + refTime: 200_000_000_000n, + proofSize: 0n, + }, + storageDepositLimit: undefined, + code: contract.wasm, + data: U.hex.decode(constructor.selector), + salt: Uint8Array.from(Array.from([0, 0, 0, 0]), () => Math.floor(Math.random() * 16)), + }, + }, + }) + .signed(T.alice.sign) + const finalizedIn = tx.watch(({ end }) => + (status) => { + // .watch emits a single inBlock event + if (typeof status !== "string" && status.inBlock) { + return end(status.inBlock) + } else if (C.rpc.known.TransactionStatus.isTerminal(status)) { + return end(new Error()) + } + return + } + ) + return C.events(tx, finalizedIn).next((events) => { + const event = events.find((e) => + e.event?.type === "Contracts" && e.event?.value?.type === "Instantiated" + ) + return event?.event?.value.contract as Uint8Array + }) +} + +function sendMessageDryRunContractCall( + address: Uint8Array, + message: C.M.ContractMetadata.Message | C.M.ContractMetadata.Constructor, +) { + const key = U.hex.encode($contractsApiCallArgs.encode([ + T.alice.publicKey, + address, + 0n, + undefined, + undefined, + U.hex.decode(message.selector), + ])) + return C.state.call(C.local)( + "ContractsApi_call", + key, + ) + .next((encodedResponse) => { + return $contractsApiCallReturn.decode(U.hex.decode(encodedResponse)) + }) +} + +function sendGetMessage(address: Uint8Array) { + const message = findContractMessageByLabel("get")! + const key = U.hex.encode($contractsApiCallArgs.encode([ + T.alice.publicKey, + address, + 0n, + undefined, + undefined, + U.hex.decode(message.selector), + ])) + return C.state.call(C.local)( + "ContractsApi_call", + key, + ) + .next((encodedResponse) => { + const response = $contractsApiCallReturn.decode(U.hex.decode(encodedResponse)) + if (message.returnType.type === null) { + return undefined + } + return contract.deriveCodec(message.returnType.type).decode(response.result.data) + }) +} + +function sendFlipMessage(address: Uint8Array) { + const message = findContractMessageByLabel("flip")! + const value = sendMessageDryRunContractCall(address, message) + .next(({ gas_required }) => { + return { + type: "call", + dest: C.MultiAddress.Id(address), + value: 0n, + data: U.hex.decode(message.selector), + gasLimit: { + refTime: gas_required.ref_time, + proofSize: gas_required.proof_size, + }, + storageDepositLimit: undefined, + } + }) + return C.extrinsic(C.local)({ + sender: T.alice.address, + call: C.Z.rec({ + type: "Contracts", + value, + }), + }) + .signed(T.alice.sign) + .watch(({ end }) => + (status) => { + // .watch emits a single inBlock event + if (typeof status !== "string" && status.inBlock) { + return end(status.inBlock) + } else if (C.rpc.known.TransactionStatus.isTerminal(status)) { + return end(new Error()) + } + return + } + ) +} + +function findContractConstructorByLabel(label: string) { + return contract.metadata.V3.spec.constructors.find((c) => c.label === label) +} + +function findContractMessageByLabel(label: string) { + return contract.metadata.V3.spec.messages.find((c) => c.label === label) +} + +async function getContract(wasmFile: string, metadataFile: string) { + const wasm = await Deno.readFile(wasmFile) + const metadata = C.M.ContractMetadata.normalize(JSON.parse( + await Deno.readTextFile(metadataFile), + )) + const deriveCodec = C.M.DeriveCodec(metadata.V3.types) + return { + wasm, + metadata, + deriveCodec, + } +} diff --git a/examples/smart_contract/codec.ts b/examples/smart_contract/codec.ts new file mode 100644 index 000000000..8081d6533 --- /dev/null +++ b/examples/smart_contract/codec.ts @@ -0,0 +1,48 @@ +import * as C from "http://localhost:5646/@local/mod.ts" + +const $balanceCodec = C.$.u128 +const $weightCodec = C.$.object( + ["ref_time", C.$.compact(C.$.u64)], + ["proof_size", C.$.compact(C.$.u64)], +) + +export const $contractsApiCallArgs = C.$.tuple( + // origin + C.$.sizedUint8Array(32), + // dest + C.$.sizedUint8Array(32), + // balance + $balanceCodec, + // weight + C.$.option($weightCodec), + // storage_deposit_limit + C.$.option($balanceCodec), + // data + C.$.uint8Array, +) + +export const $contractsApiCallReturn = C.$.object( + // gas_consumed + ["gas_consumed", $weightCodec], + // gas_required + ["gas_required", $weightCodec], + // storage_deposit + [ + "storage_deposit", + C.$.taggedUnion("type", [ + ["Refund", ["value", $balanceCodec]], + ["Charge", ["value", $balanceCodec]], + ]), + ], + // debug_message + ["debug_message", C.$.str], + // result + [ + "result", + C.$.object( + ["flags", C.$.u32], + // TODO: improve result error coded + ["data", C.$.result(C.$.uint8Array, C.$.never)], + ), + ], +) diff --git a/examples/smart_contract/flipper.contract b/examples/smart_contract/flipper.contract new file mode 100644 index 000000000..7b5e7984e --- /dev/null +++ b/examples/smart_contract/flipper.contract @@ -0,0 +1 @@ +{"source":{"hash":"0x2345709e061bfa0d374eaf0c25f19c8dce43ac11362c55196e1ecf465781b750","language":"ink! 3.4.0","compiler":"rustc 1.67.0-nightly","wasm":"0x0061736d01000000014e0d60037f7f7f017f60027f7f017f60027f7f0060057f7f7f7f7f0060037f7f7f0060047f7f7f7f0060017f0060000060047f7f7f7f017f60017f017e60017f017f6000017f60067f7f7f7f7f7f0002a30107057365616c30127365616c5f64656275675f6d6573736167650001057365616c30107365616c5f7365745f73746f726167650004057365616c30107365616c5f6765745f73746f726167650000057365616c300a7365616c5f696e7075740002057365616c300b7365616c5f72657475726e0004057365616c30167365616c5f76616c75655f7472616e73666572726564000203656e76066d656d6f727902010210033b3a0002010601020a00020303040b02070607030100000401010006070206010903050101010308050803040101040000030508050101010101050c040501700110100501000608017f01418080040b071102066465706c6f7900140463616c6c00160915010041010b0f0a30313c2723393a223d1a1c1d3b240acf513a2b01017f037f2002200346047f200005200020036a200120036a2d00003a0000200341016a21030c010b0b0b7901017f230041406a22022400200241206a200141186a290000370300200241186a200141106a290000370300200241106a200141086a29000037030020024201370328200220012900003703082002419ea104360230200242808001370234200241086a200241306a2000100841011001200241406b24000b7701037f230041106b220224002000280204210320002802002104200220013a000f200241012004200341c49a04100f200228020020022802042002410f6a410141d49a041010200345044041a49904412341e49a041011000b2000200341016b3602042000200441016a360200200241106a240020040b5201017f230041206b220124002001410c6a4101360200200141146a4101360200200141909d04360208200141003602002001410136021c200120003602182001200141186a360210200141948304100b000b910101017f230041306b22022400200241146a41013602002002411c6a4101360200200241909d043602102002410036020820024102360224200220002d0000410274220041eca0046a28020036022c200220004180a1046a280200360228200141046a28020021002002200241206a3602182002200241286a36022020012802002000200241086a1033200241306a24000b840301037f230041206b22032400200341013a00182003200136021420032000360210200341dc890436020c200341909d04360208230041406a220224002002200341086a36020c2002410436022420022002410c6a360220410021004104210402400240024002400240024003402000200441c899046a2802006a22012000490d0120012100200441086a22044114470d000b4100210041012104024020014110490d002001200120016a22034b200345720d0020034100480d022002200310212003210020022802002204450d060b2002410036021820022004360214200220003602102002410136023c20024102360234200241c89904360230200241003602282002200241206a360238200241106a200241286a10180d02419ca1042d0000450440419da1042d00004101710d060b2002280214200228021810004109470d030c040b41c08404411c418499041011000b1020000b41c086044133200241286a4184850441e087041017000b419ca10441013a00000b419da10441013a00000b000b4001017f230041106b22012400200141003a000f20002001410f6a4101100d047f4102054101410220012d000f22004101461b410020001b0b200141106a24000b6001047f230041106b22032400200028020422042002492205450440200341086a4100200220002802002206103e200120022003280208200328020c41a4a00410102003200220042006103e200020032903003702000b200341106a240020050b4701017f230041106b220224002002410036020c024020012002410c6a4104100d4504402000200228020c360001200041003a00000c010b200041013a00000b200241106a24000b2200200120034d044020002001360204200020023602000f0b200120032004102f000b8501002001200346044020002002200110061a0f0b230041306b220024002000200336020420002001360200200041146a41033602002000411c6a41023602002000412c6a4105360200200041ec920436021020004100360208200041053602242000200041206a360218200020003602282000200041046a360220200041086a2004100b000b5001017f230041206b220324002003410c6a4101360200200341146a4100360200200341909d04360210200341003602002003200136021c200320003602182003200341186a36020820032002100b000b6c02027f027e230041206b22002400200041086a220142003703002000420037030020004110360214200020003602102000411036021c20002000411c6a1005200041106a200028021c10132001290300210220002903002103200041206a2400410541042002200384501b0b3501017f230041106b22022400200241086a20012000280200200028020441dc9b04100f20002002290308370200200241106a24000bd00301057f230041306b220024002000027f02400240101241ff0171410546044020004180800136022c2000419ea104360228200041286a1015200020002903283703082000200041086a100e20002d00000d022000280001220141187621022001411076210320014108762104027f200141ff01712201419b01470440200141ed0147200441ff017141cb004772200341ff0171419d0147720d0441022002411b460d011a0c040b200441ff017141ae0147200341ff0171419d014772200241de0047720d03200041086a100c41ff017122014102460d0320014101710b2101101241ff01712102024002402001410247044020024105460d010c040b20024105470d03200041206a4200370300200041186a4200370300200041106a4200370300200042003703084100200041086a10070c010b200041206a4200370300200041186a4200370300200041106a4200370300200042003703082001200041086a10070b200041306a24000f0b200041043a0008200041086a1009000b41040c010b41030b3a0000200041146a41013602002000411c6a4101360200200041ec8304360210200041003602082000410136022c2000200041286a36021820002000360228200041086a41948304100b000b3301017f230041106b220124002001200028020436020c20002802002001410c6a10032000200128020c1013200141106a24000b8a0601057f230041d0006b2200240002400240024002400240101241ff01712201410546044020004180800136023c2000419ea104360238200041386a101520002000290338370308200041306a200041086a100e024020002d00300d002000280031220141187621022001411076210320014108762104200141ff01712201412f470440200141e30047200441ff0171413a4772200341ff017141a50147720d0141012101200241d100470d010c030b200441ff017141860147200341ff017141db0047720d0041002101200241d901460d020b410321010c020b200020013a0008200041086a1009000b200041106a4200370300200041186a4200370300200041206a420037030020004200370308200042013703282000418080013602342000419ea104360230200041808001360238200041086a419ea104200041386a10022102200041306a200028023810130240024002402002410c4f0d00200241027441bca0046a2802000e0402000001000b200041c4006a4101360200200041cc006a4100360200200041d48204360240200041909d0436024820004100360238200041386a41dc8204100b000b230041106b220124002001411736020c200141988104360208230041206b220024002000410c6a4101360200200041146a4101360200200041909d04360208200041003602002000410236021c2000200141086a3602182000200041186a360210200041b08104100b000b20002000290330370338200041386a100c41ff017122024102460d03101241ff017121032001044020034105460d02410421010c010b4104210120034105460d020b200020013a0030200041146a41013602002000411c6a4101360200200041988404360210200041003602082000410136023c2000200041386a3602182000200041306a360238200041086a41948304100b000b20024541a483041007200041d0006a24000f0b230041106b220024002000419ea104360200200042808001370204410020002002410047100841011004000b200041003a0038418080044127200041386a41c08104418881041017000b860101017f230041406a220524002005200136020c200520003602082005200336021420052002360210200541246a41023602002005412c6a41023602002005413c6a4103360200200541908a0436022020054100360218200541023602342005200541306a3602282005200541106a3602382005200541086a360230200541186a2004100b000b5501017f230041206b2202240020022000360204200241186a200141106a290200370300200241106a200141086a29020037030020022001290200370308200241046a41a08404200241086a1019200241206a24000bee0301057f230041406a22032400200341033a003820034280808080800437033020034100360228200341003602202003200136021c20032000360218027f0240024020022802002201450440200241146a28020022004103742105200041ffffffff017121072002280210210441002101034020012005460d02200228020820016a220041046a28020022060440200328021820002802002006200328021c28020c1100000d040b200141086a2101200428020020042802042106200441086a2104200341186a2006110100450d000b0c020b200228020422074105742100200741ffffff3f71210703402000450d01200228020820046a220541046a28020022060440200328021820052802002006200328021c28020c1100000d030b20032001411c6a2d00003a00382003200141146a290200370330200341106a200228021022052001410c6a103220032003290310370320200341086a2005200141046a103220032003290308370328200441086a2104200041206b210020012802002106200141206a2101200520064103746a2205280200200341186a2005280204110100450d000b0c010b2002410c6a28020020074b04402003280218200228020820074103746a22002802002000280204200328021c28020c1100000d010b41000c010b41010b200341406b24000b0f00200028020020012002101b41000b5801017f20022000280200200028020822036b4b0440200020032002101e101f200028020821030b200028020420036a2001200210061a2003200220036a22014b044041c08404411c41f088041011000b200020013602080bbe0201027f230041106b220224000240024002400240200028020022002002410c6a027f0240024020014180014f04402002410036020c2001418010490d012001418080044f0d0220022001413f71418001723a000e20022001410c7641e001723a000c20022001410676413f71418001723a000d41030c030b200028020822032000280200460d030c040b20022001413f71418001723a000d2002200141067641c001723a000c41020c010b20022001413f71418001723a000f20022001410676413f71418001723a000e20022001410c76413f71418001723a000d2002200141127641077141f001723a000c41040b101b0c020b200020034101101e101f200028020821030b200028020420036a20013a0000200341016a2201450d01200020013602080b200241106a240041000f0b41c08404411c41e088041011000b4a01017f230041206b220224002000280200200241186a200141106a290200370300200241106a200141086a29020037030020022001290200370308200241086a1018200241206a24000b990401067f230041206b220324000240027f41002001200120026a22014b0d001a2000280200220220026a22052002490d012005200120012005491b22014108200141084b1b2201417f73411f7621040240200204402003410136021820032002360214200320002802043602100c010b200341003602180b200341106a2106230041106b220524002003027f0240027f0240200404400240200141004e044020062802080d012005200110212005280204210220052802000c040b0c040b20062802042208450440200541086a20011021200528020c210220052802080c030b2001210241004194a104280200220420016a22072004490d021a200628020021064198a1042802002007490440200141ffff036a220741107640002202417f46200241ffff0371200247720d022002411074220420074180807c716a22022004490d024198a1042002360200200121024100200120046a22072004490d031a0b4194a104200736020041002004450d021a20042006200810060c020b200320013602040c020b2001210241000b2204044020032004360204200341086a200236020041000c020b20032001360204200341086a410136020041010c010b200341086a410036020041010b360200200541106a240020032802004504402003280204210220002001360200200020023602044181808080780c010b200341086a2802000b200341206a24000f0b41e084044121418486041011000b1b00024020004181808080784704402000450d01000b0f0b1020000b4601017f230041206b22002400200041146a41013602002000411c6a4100360200200041a88604360210200041909d0436021820004100360208200041086a41b08604100b000ba10101027f027f41004194a104280200220320016a22022003490d001a02404198a1042802002002490440200141ffff036a22022001490d012002411076220240002203417f46200341ffff0371200347720d012003411074220320024110746a22022003490d014198a10420023602004100200120036a22022003490d021a0b4194a104200236020020030c010b41000b210220002001360204200020023602000b0300010b0e0020002802001a03400c000b000b0c0042abf2ce83adf3f7da440bb20101017f230041106b2205240020022003490440230041306b220024002000200236020420002003360200200041146a41023602002000411c6a41023602002000412c6a4105360200200041e88f0436021020004100360208200041053602242000200041206a3602182000200041046a36022820002000360220200041086a2004100b000b200541086a2003200220011026200528020c21012000200528020836020020002001360204200541106a24000b1400200020012002200341849f0441a08904103f0bd806020b7f027e230041406a2203240020002802002202ad210d0240024002400240024002400240024020024190ce004f044041272100200d210e0240034020004104490d01200341196a20006a220241046b200e200e4290ce0080220d4290ce007e7da7220441ffff037141e4006e220641017441978b046a2f00003b0000200241026b2004200641e4006c6b41ffff037141017441978b046a2f00003b0000200041046b2100200e42ffc1d72f56200d210e0d000b200da7220241e3004d0d0320004102490d090c020b0c080b41272100200241e3004b0d002002410a490d040c020b200041026b2200200341196a6a200da72202200241ffff037141e4006e220241e4006c6b41ffff037141017441978b046a2f00003b00000b2002410a490d01200041024f0d000c050b200041026b2200200341196a6a200241017441978b046a2f00003b00000c020b2000450d030b200041016b2200200341196a6a200241306a3a00000b200041274b0d01412820006b412720006b22062001280218220541017122071b2102410021042005410471044041909d042104200241909d0441909d04102820026a22024b0d010b412b418080c40020071b2107200341196a20006a2108024020012802084504404101210020012802002202200141046a280200220120072004102b0d01200220082006200128020c11000021000c010b024020022001410c6a28020022094904402005410871450d01200128021c210b2001413036021c20012d0020210c41012100200141013a002020012802002205200141046a280200220a20072004102b0d02200341106a2001200920026b4101102c20032802142202418080c400460d022003280210200520082006200a28020c1100000d0220022005200a102d0d022001200c3a00202001200b36021c410021000c020b4101210020012802002202200141046a280200220120072004102b0d01200220082006200128020c11000021000c010b41012100200341086a2001200920026b4101102c200328020c2205418080c400460d00200328020820012802002202200141046a280200220120072004102b0d00200220082006200128020c1100000d00200520022001102d21000b200341406b240020000f0b41808904411c41d08d041011000b41a089044121419499041011000ba704010a7f230041106b2203240002400240200020016b22024110490d002002200141036a417c7120016b220049200041044b720d00200220006b22044104490d002001200010292206200020016a22082004417c716a200441037110296a220220064f0440200441027621050240024003402005450d05200320082005200541c001200541c001491b418c9104102a200328020c21052003280208210820032003280200200328020422002000417c71418c9204102a200328020c210920032802082107024020032802042200450440410021010c010b2003280200220420004102746a210a4100210103402004220641106a2104410021000240034020012001200020066a280200220b417f73410776200b410676724181828408716a22014d0440200041046a22004110470d010c020b0b41808904411c418494041011000b2004200a470d000b0b20022002200141087641ff81fc0771200141ff81fc07716a418180046c4110766a22024b0d012009450d000b200941027421004100210103402001200120072802002204417f734107762004410676724181828408716a22014b0d02200741046a2107200041046b22000d000b20022002200141087641ff81fc0771200141ff81fc07716a418180046c4110766a22024d0d0441808904411c41b494041011000b41808904411c419494041011000b41808904411c41a494041011000b41808904411c41f493041011000b20012002102921020b200341106a240020020b4601017f200145044041000f0b024003402002200220002c000041bf7f4a6a22024b0d01200041016a2100200141016b22010d000b20020f0b41808904411c418499041011000b3e00200220034f044020002003360204200020013602002000410c6a200220036b3602002000200120034102746a3602080f0b41a49904412320041011000b39000240027f2002418080c40047044041012000200220012802101101000d011a0b20030d0141000b0f0b200020034100200128020c1100000bae0101027f20022104024002400240200320012d0020220320034103461b41ff0171220341016b0e03010001020b200241016a2203044020034101762104200241017621030c020b41808904411c41e08d041011000b41002104200221030b200341016a2102200128021c2103200128020421052001280200210102400340200241016b2202450d01200120032005280210110100450d000b418080c40021030b20002003360204200020043602000b3201017f027f0340200020002004460d011a200441016a2104200220012003280210110100450d000b200441016b0b2000490b4b01017f230041106b22052400200120034d0440200541086a4100200120021026200528020c21012000200528020836020020002001360204200541106a24000f0b200120032004102f000b7501017f230041306b220324002003200136020420032000360200200341146a41023602002003411c6a41023602002003412c6a410536020020034188900436021020034100360208200341053602242003200341206a3602182003200341046a36022820032003360220200341086a2002100b000bea04010b7f230041106b2209240020002802042104200028020021030240024002402001280208220b410147200128021022024101477145044020024101470d02200320046a210c200141146a28020041016a210a410021022003210003402000200c460d03027f024020002c0000220641004e0440200041016a2105200641ff017121070c010b20002d0001413f7121052006411f7121072006415f4d044020074106742005722107200041026a21050c010b20002d0002413f7120054106747221082006417049044020082007410c74722107200041036a21050c010b200041046a210520022106418080c4002007411274418080f0007120002d0003413f71200841067472722207418080c400460d011a0b2002200520006b6a22062002490d0320070b2108200a41016b220a044020052100200621022008418080c400470d010c040b0b2008418080c400460d02024002402002450d00200220044f04404100210020022004460d010c020b41002100200220036a2c00004140480d010b200321000b2002200420001b21042000200320001b21030c020b200128020020032004200128020428020c11000021000c020b41808904411c41b495041011000b200b450440200128020020032004200128020428020c11000021000c010b2001410c6a2802002200200320046a2003102822024b0440200941086a2001200020026b4100102c41012100200928020c2202418080c400460d0120092802082001280200220520032004200141046a280200220128020c1100000d01200220052001102d21000c010b200128020020032004200128020428020c11000021000b200941106a240020000b140020002802002001200028020428020c1101000b5501027f0240027f02400240200228020041016b0e020103000b200241046a0c010b200120022802044103746a22012802044106470d0120012802000b2802002104410121030b20002004360204200020033602000b4901017f230041206b22032400200341186a200241106a290200370300200341106a200241086a2902003703002003200229020037030820002001200341086a1019200341206a24000bda08010b7f23004190016b22032400200341003b0184012003410a3602800120034281808080a00137037820032002360274200341003602702003200236026c200320013602682003200236026420034100360260200028020421062000280200210720002802082108200341fc006a2109027f0340024002400240024020032d008501450440200341d8006a2003280268220c200328026c2003280270200328027410350240024020032802582201450d00200328025c2100034002400240024002400240027f0240024002400240200328027822020440200220096a41016b2d00002104200041084f04402001200141036a417c712202460440200041086b210a410021020c040b200341d0006a200220016b22022000200020024b1b22022001200041e48e04102e200341c8006a200420032802502003280254103620032802484101470d02200328024c21010c050b200341306a20042001200010362003280234210120032802300c050b41a08904412141cc97041011000b2002200041086b220a4b0d010b200441818284086c210b0340200241046a22052002490d04200120026a280200200b73220d417f73200d41818284086b71200120056a280200200b732205417f73200541818284086b7172418081828478710d012002200241086a22024b0d072002200a4d0d000b0b200341406b20012000200241948f041025200341386a2004200328024020032802441036410020032802384101470d011a2002200328023c6a220120024f0d0041808904411c41a48f041011000b41010b4101460440200141016a2200450d022000200328027022006a22022000490d0320032002360270200220032802782200490d05200341286a2003280268200328026c200220006b2002103520032802282202450d05200328022c2100200341206a20032802782009410441fc9704102e20022000200328022020032802241037450d05200341186a200328026020032802702200200c103820032000360260200328021c2102200328021821000c080b200320032802743602700c060b41808904411c41f48e041011000b41808904411c41dc97041011000b41808904411c41ec97041011000b41808904411c41848f041011000b200341106a2003280268200328026c20032802702003280274103520032802142100200328021022010d000b0b4100210020032d0085010d00024020032d008401044020032802642101200328026021040c010b2003280264220120032802602204490d0420012004460d010b200341013a008501200341086a2004200120032802681038200328020c2102200328020821000b20000d010b41000c050b20082d0000450d01200741a08a044104200628020c110000450d010c020b41a08904412141c495041011000b2003410a36028c0120082002047f200320002002200241016b419c920410252003418c016a41012003280200200328020410370541000b3a0000200720002002200628020c110000450d010b0b41010b20034190016a24000b4c01037f230041106b220524002002200449200320044b72450440200541086a2003200420011026200528020c2107200528020821060b2000200736020420002006360200200541106a24000b5701027f024002402003450440410021030c010b200141ff017121054101210103402005200220046a2d0000460440200421030c030b2003200441016a2204470d000b0b410021010b20002003360204200020013602000b4d01017f2001200346047f027f034041002001450d011a200141016b210120022d0000210320002d00002104200041016a2100200241016a210220032004460d000b200420036b0b0541010b450b1400200020012002200341c8960441a08904103f0b5801027f230041206b22022400200128020421032001280200200241186a2000280200220041106a290200370300200241106a200041086a290200370300200220002902003703082003200241086a1019200241206a24000b0b002000280200200110300b1800200128020041b4a0044105200128020428020c1100000b990301037f230041406a22022400200028020021034101210002402001280200220441ec8904410c200141046a280200220128020c1100000d0002402003280208220004402002200036020c200241346a4102360200410121002002413c6a4101360200200241fc890436023020024100360228200241073602142002200241106a36023820022002410c6a36021020042001200241286a1033450d010c020b20032802002200200328020428020c11090042c8b5e0cfca86dbd3897f520d002002200036020c200241346a4102360200410121002002413c6a4101360200200241fc890436023020024100360228200241083602142002200241106a36023820022002410c6a36021020042001200241286a10330d010b200328020c21002002411c6a4103360200200241246a41033602002002413c6a4105360200200241346a4105360200200241c489043602182002410036021020022000410c6a3602382002200041086a3602302002410236022c200220003602282002200241286a36022020042001200241106a103321000b200241406b240020000b980401047f230041106b22022400024002400240024002400240024002400240024002400240024020002d000041016b0e0b0102030405060708090a0b000b4101210020012802002203418a9d0441062001280204220528020c22041100000d0b024020012d0018410471450440200341a88a04410120041100000d0d200341b4a00441052004110000450d010c0d0b200341a68a04410220041100000d0c2002200536020420022003360200200241013a000f20022002410f6a360208200241b4a004410510340d0c200241a48a04410210340d0c0b200341c189044101200411000021000c0b0b200128020041fd9c04410d200128020428020c11000021000c0a0b200128020041ef9c04410e200128020428020c11000021000c090b200128020041e49c04410b200128020428020c11000021000c080b200128020041ca9c04411a200128020428020c11000021000c070b200128020041bc9c04410e200128020428020c11000021000c060b200128020041ac9c044110200128020428020c11000021000c050b200128020041a09c04410c200128020428020c11000021000c040b200128020041959c04410b200128020428020c11000021000c030b2001280200418e9c044107200128020428020c11000021000c020b200128020041ff9b04410f200128020428020c11000021000c010b200128020041ec9b044113200128020428020c11000021000b200241106a240020000b1400200020012002200341849f0441a09f04103f0b2800200120024d04402000200220016b3602042000200120036a3602000f0b2005412120041011000b0bf6200400418080040ba103636f756c64206e6f742070726f7065726c79206465636f64652073746f7261676520656e7472792f55736572732f7061726974792f2e636172676f2f72656769737472792f7372632f6769746875622e636f6d2d316563633632393964623965633832332f696e6b5f73746f726167652d332e342e302f7372632f7472616974732f6d6f642e72732700010061000000a20000000a00000073746f7261676520656e7472792077617320656d707479002700010061000000a30000000a0000000900000001000000010000000a0000002f55736572732f7061726974792f2e636172676f2f72656769737472792f7372632f6769746875622e636f6d2d316563633632393964623965633832332f696e6b5f656e762d332e342e302f7372632f656e67696e652f6f6e5f636861696e2f696d706c732e7273656e636f756e746572656420756e6578706563746564206572726f72380101001c000000d000010068000000f6000000170000002f55736572732f7061726974792f436f64652f3466756e2f666c69707065722f6c69622e727300006c0101002600000008000000050041c483040b716469737061746368696e6720696e6b2120636f6e7374727563746f72206661696c65643a20000000c4010100250000006469737061746368696e6720696e6b21206d657373616765206661696c65643a20000000f4010100210000000900000004000000040000000b0000000c0000000d0041c084040bd11a617474656d707420746f206164642077697468206f766572666c6f7700000000617474656d707420746f206d756c7469706c792077697468206f766572666c6f770000000900000000000000010000000e0000002f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f616c6c6f632f7372632f7261775f7665632e727300940201006f0000008a0100001c0000006361706163697479206f766572666c6f770000001403010011000000940201006f00000006020000050000006120666f726d617474696e6720747261697420696d706c656d656e746174696f6e2072657475726e656420616e206572726f722f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f616c6c6f632f7372632f666d742e72730000730301006b00000064020000200000002f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f616c6c6f632f7372632f7665632f6d6f642e727300f00301006f0000002f0700000d000000f00301006f0000009d07000009000000617474656d707420746f206164642077697468206f766572666c6f7700000000617474656d707420746f2073756274726163742077697468206f766572666c6f77293a00900e010000000000c204010001000000c2040100010000000900000000000000010000000f00000070616e69636b65642061742027272c20f804010001000000f9040100030000003a200000900e0100000000000c05010002000000202020202c0a280a282f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f636f72652f7372632f666d742f6e756d2e727330303031303230333034303530363037303830393130313131323133313431353136313731383139323032313232323332343235323632373238323933303331333233333334333533363337333833393430343134323433343434353436343734383439353035313532353335343535353635373538353936303631363236333634363536363637363836393730373137323733373437353736373737383739383038313832383338343835383638373838383939303931393239333934393539363937393839392f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f636f72652f7372632f666d742f6d6f642e72730000005f0601006e0000005d0500000d0000005f0601006e000000ed050000380000002f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f636f72652f7372632f736c6963652f6d656d6368722e727300f0060100730000004e0000002f000000f0060100730000005a0000001f000000f0060100730000006300000009000000f0060100730000006800000027000000f006010073000000680000003e00000072616e676520737461727420696e64657820206f7574206f662072616e676520666f7220736c696365206f66206c656e67746820b407010012000000c60701002200000072616e676520656e6420696e64657820f807010010000000c6070100220000002f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f636f72652f7372632f736c6963652f697465722e72730000001808010071000000c4050000250000002f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f636f72652f7372632f736c6963652f6d6f642e72739c08010070000000f30300002f0000009c08010070000000d30800001e000000736f7572636520736c696365206c656e67746820282920646f6573206e6f74206d617463682064657374696e6174696f6e20736c696365206c656e67746820282c09010015000000410901002b000000c1040100010000002f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f636f72652f7372632f7374722f636f756e742e7273840901007000000047000000150000008409010070000000540000001100000084090100700000005a00000009000000840901007000000064000000110000008409010070000000660000000d0000002f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f636f72652f7372632f7374722f697465722e727300440a01006f0000009100000011000000440a01006f0000004c0200003c0000002f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f636f72652f7372632f7374722f7472616974732e7273000000d40a010071000000ca000000130000002f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f636f72652f7372632f7374722f7061747465726e2e72730000580b010072000000a101000047000000580b010072000000b401000020000000580b010072000000b401000011000000580b010072000000b8010000260000002f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f636f72652f7372632f697465722f7472616974732f616363756d2e72730c0c0100780000008d00000001000000290501006e000000cd01000005000000617373657274696f6e206661696c65643a206d6964203c3d2073656c662e6c656e28290a900e010000000000c70c0100010000002f55736572732f7061726974792f2e636172676f2f72656769737472792f7372632f6769746875622e636f6d2d316563633632393964623965633832332f696e6b5f656e762d332e342e302f7372632f656e67696e652f6f6e5f636861696e2f6275666665722e7273000000d80c0100690000005800000009000000d80c0100690000005800000031000000d80c0100690000008b000000210000002f55736572732f7061726974792f2e636172676f2f72656769737472792f7372632f6769746875622e636f6d2d316563633632393964623965633832332f696e6b5f656e762d332e342e302f7372632f656e67696e652f6f6e5f636861696e2f6578742e72730000740d0100660000008c0100001400000045636473615265636f766572794661696c65644c6f6767696e6744697361626c6564556e6b6e6f776e4e6f7443616c6c61626c65436f64654e6f74466f756e645f456e646f776d656e74546f6f4c6f775472616e736665724661696c65645f42656c6f7753756273697374656e63655468726573686f6c644b65794e6f74466f756e6443616c6c6565526576657274656443616c6c6565547261707065644465636f6465900e0100000000007061696420616e20756e70617961626c65206d657373616765636f756c64206e6f74207265616420696e707574756e61626c6520746f206465636f646520696e707574656e636f756e746572656420756e6b6e6f776e2073656c6563746f72756e61626c6520746f206465636f64652073656c6563746f722f55736572732f7061726974792f2e7275737475702f746f6f6c636861696e732f6e696768746c792d616172636836342d6170706c652d64617277696e2f6c69622f727573746c69622f7372632f727573742f6c6962726172792f636f72652f7372632f736c6963652f696e6465782e72730000100f01007200000081010000470041a09f040bf301617474656d707420746f2073756274726163742077697468206f766572666c6f772f55736572732f7061726974792f2e636172676f2f72656769737472792f7372632f6769746875622e636f6d2d316563633632393964623965633832332f7061726974792d7363616c652d636f6465632d332e322e312f7372632f636f6465632e7273c10f0100630000007a0000000e0000004572726f72000000000000000100000002000000030000000400000005000000060000000700000008000000090000000c0000000b000000190000001c000000160000001400000019000000f70e0100db0e0100c50e0100b10e0100980e01"},"contract":{"name":"flipper","version":"0.1.0","authors":["[your_name] <[your_email]>"]},"V3":{"spec":{"constructors":[{"args":[{"label":"init_value","type":{"displayName":["bool"],"type":0}}],"docs":["Constructor that initializes the `bool` value to the given `init_value`."],"label":"new","payable":false,"selector":"0x9bae9d5e"},{"args":[],"docs":["Constructor that initializes the `bool` value to `false`.","","Constructors can delegate to other constructors."],"label":"default","payable":false,"selector":"0xed4b9d1b"}],"docs":[],"events":[],"messages":[{"args":[],"docs":[" A message that can be called on instantiated contracts."," This one flips the value of the stored `bool` from `true`"," to `false` and vice versa."],"label":"flip","mutates":true,"payable":false,"returnType":null,"selector":"0x633aa551"},{"args":[],"docs":[" Simply returns the current value of our `bool`."],"label":"get","mutates":false,"payable":false,"returnType":{"displayName":["bool"],"type":0},"selector":"0x2f865bd9"}]},"storage":{"struct":{"fields":[{"layout":{"cell":{"key":"0x0000000000000000000000000000000000000000000000000000000000000000","ty":0}},"name":"value"}]}},"types":[{"id":0,"type":{"def":{"primitive":"bool"}}}]}} \ No newline at end of file diff --git a/examples/smart_contract/flipper.wasm b/examples/smart_contract/flipper.wasm new file mode 100644 index 0000000000000000000000000000000000000000..fa0674ef762a74dcc1a410cb5436d33628ef7de7 GIT binary patch literal 15044 zcmchedyHJyUB}OT%wzY?*texOoZiS4>c+EP-85XheFj_rMT zXFW6P2ZFBCx}i-2q!4IA8|u(ND?t&UAn^x+6a}ctD~L)ITA@hv4}=0lwBjKZh0ph# zJL6p^5X!?|-MQy=f9LmpoO^AW-E)C4CU{r%RJ6Id8El>k`@iN?fsxHk<7UjM*mnX3 zOx|O8uo=8xKbxlvSJ6GOX~Itjr9!vWTy5N<-;;~2`!>#;JlE=Wn`c@k@KdRq>b80( zyS;Wt6Y8 zEt)WDtz9UdYn^L%E{8#wMsGcA!k`$0!%RhS5|)ak2zJClV2Y)%7z{;)APAyzp&T(5 z8xs}E8VQ0zB`yT(L7Em=T84gmTkEUs%Vt-+&|F#f6o`rU`$5(JykW)^1Av3N5xh0J8pau)D&(3r!9o(-03Q6)%{ zcMVz(KMoOPhK!$K!9EyBZ#OqFXgW-}Kk~siO-%AJ1SeV=o|bR` zDYW}?n4xtrQ}6(a7!^*3w?e2vCN}KVjeY)FybOuc;Q%bcEH9@M+#1lCT#YE*j)YLMyPY|>DP02v zr+r#0y$l_l&wQO+S?Jy$1BjN7U*rDUqs1hgk>8r8b zXRkiy6f4aj;^_xsD>jcT+p8an?Q=gGr>|oenBp-lVEUFx=Dl8^<6hUWzMZftaQd58 z<4qeDP-N8opxKEE1jqJTWVtsHgCxdNL7W?jq$ne)qy8uA1(0_lVmYi3mx}4z;3lja zLB05?*e96mo8bIE&D;=<0j+CQ~=a3#yQc^8hG^{pu&=DdGM8 zsEnZd(s?!t3b})F7=rM8IG^5PUkuLMf|Cew1}YD*rSstMPS)}oVYxpVnTg5{UtG)% z=d!R;Gh%`|zVHJ0qOKRfm(Xwl``)AhWF|im97o)N{rliJu0Xr}p*h}xgK9n=9Pd

l%C{q6yogj_Ew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U4{RP_6+5WtO!)-V8-$tMR2ysRMxY_C9>=#>? zvaG$)%i5J}nU?ovSjt z%Yiu$4$mN~H*vptIC%B>B>S(k@7Hk#l2gC`=S13Xr{8UFbQWN|#M|fV$o_1x-Rim& zi7>Emmi(woFz~*A`_cnLfq4yZEcdr?|9tL$ZVH{f&ZtXCVD(k@7oEqsgipVJ4{gP` zeqZ`2eeCxaZ2UlR4U8)P0OZ79^b4!ca?cbSoTdSuJ;owI05BKBt9{@`*ZlL9V zP?7xCiT-^Xr}@8VUiM#R&n5P}mrM4twLi9{|54gCuK#b`T9gzhn_nZ=Vw z*B09smD%3YZr|0ucu)R6@;s=MM_T0k-?MRFm#UBYr*+TSPOE#iy}Ed3>vA82z_wcN zZGi#xknaJ8`eyMCS6BAsxZXs>WXv0D>&?qTB+Dz5>lJCI)l~H2zYuP$xfRqPd2#AS zx7T2fFlh{xSiAF*C;AgN$majD_86pjK0k1g+bK}G)7$O+$mzJT*FF^|Uk|n<#xI}v z-hk=1sNud;KiK8}Hnt9`pkJV>9X~YY@=(_Ywa" + ] + }, + "V3": { + "spec": { + "constructors": [ + { + "args": [ + { + "label": "init_value", + "type": { + "displayName": [ + "bool" + ], + "type": 0 + } + } + ], + "docs": [ + "Constructor that initializes the `bool` value to the given `init_value`." + ], + "label": "new", + "payable": false, + "selector": "0x9bae9d5e" + }, + { + "args": [], + "docs": [ + "Constructor that initializes the `bool` value to `false`.", + "", + "Constructors can delegate to other constructors." + ], + "label": "default", + "payable": false, + "selector": "0xed4b9d1b" + } + ], + "docs": [], + "events": [], + "messages": [ + { + "args": [], + "docs": [ + " A message that can be called on instantiated contracts.", + " This one flips the value of the stored `bool` from `true`", + " to `false` and vice versa." + ], + "label": "flip", + "mutates": true, + "payable": false, + "returnType": null, + "selector": "0x633aa551" + }, + { + "args": [], + "docs": [ + " Simply returns the current value of our `bool`." + ], + "label": "get", + "mutates": false, + "payable": false, + "returnType": { + "displayName": [ + "bool" + ], + "type": 0 + }, + "selector": "0x2f865bd9" + } + ] + }, + "storage": { + "struct": { + "fields": [ + { + "layout": { + "cell": { + "key": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ty": 0 + } + }, + "name": "value" + } + ] + } + }, + "types": [ + { + "id": 0, + "type": { + "def": { + "primitive": "bool" + } + } + } + ] + } +} \ No newline at end of file From 45ed0dd8e64feab5751dfae34ffc83ed4a5fb2a2 Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Mon, 12 Dec 2022 12:26:29 -0300 Subject: [PATCH 2/8] feat: use zombienet in smart contract example --- effects/rpc_known_clients.ts | 1 - examples/smart_contract.toml | 24 ++++++++++++++++++++++++ examples/smart_contract.ts | 26 ++++++++++++++++++++------ 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 examples/smart_contract.toml diff --git a/effects/rpc_known_clients.ts b/effects/rpc_known_clients.ts index 8ffd8bb41..166565742 100644 --- a/effects/rpc_known_clients.ts +++ b/effects/rpc_known_clients.ts @@ -14,4 +14,3 @@ export const moonbeam = proxyClient("wss://wss.api.moonbeam.network") export const statemint = proxyClient("wss://statemint-rpc.polkadot.io") export const subsocial = proxyClient("wss://para.subsocial.network") export const westend = proxyClient("wss://westend-rpc.polkadot.io") -export const local = proxyClient("ws://127.0.0.1:9944") diff --git a/examples/smart_contract.toml b/examples/smart_contract.toml new file mode 100644 index 000000000..412088413 --- /dev/null +++ b/examples/smart_contract.toml @@ -0,0 +1,24 @@ +[relaychain] +default_image = "docker.io/paritypr/polkadot-debug:master" +default_command = "polkadot" +default_args = ["-lparachain=debug"] +chain = "rococo-local" + +[[relaychain.nodes]] +name = "alice" +validator = true + +[[relaychain.nodes]] +name = "bob" +validator = true + +[[parachains]] +id = 1000 +cumulus_based = true +chain = "contracts-rococo-local" + +[parachains.collator] +name = "collator01" +image = "docker.io/parity/polkadot-parachain:latest" +command = "polkadot-parachain" +args = ["-lparachain=debug"] diff --git a/examples/smart_contract.ts b/examples/smart_contract.ts index d2d789823..391714aad 100644 --- a/examples/smart_contract.ts +++ b/examples/smart_contract.ts @@ -1,12 +1,17 @@ +import * as path from "http://localhost:5646/@local/deps/std/path.ts" import * as C from "http://localhost:5646/@local/mod.ts" import * as T from "http://localhost:5646/@local/test_util/mod.ts" import * as U from "http://localhost:5646/@local/util/mod.ts" import { $contractsApiCallArgs, $contractsApiCallReturn } from "./smart_contract/codec.ts" +const configFile = getFilePath("smart_contract.toml") +const zombienet = await T.zombienet.start(configFile) +const client = zombienet.clients.byName["collator01"]! + const contract = await getContract( - "./examples/smart_contract/flipper.wasm", - "./examples/smart_contract/metadata.json", + getFilePath("smart_contract/flipper.wasm"), + getFilePath("smart_contract/metadata.json"), ) const contractAddress = U.throwIfError(await instantiateContractTx().run()) @@ -15,9 +20,11 @@ console.log("get message", U.throwIfError(await sendGetMessage(contractAddress). console.log("flip message in block", U.throwIfError(await sendFlipMessage(contractAddress).run())) console.log("get message", U.throwIfError(await sendGetMessage(contractAddress).run())) +await zombienet.close() + function instantiateContractTx() { const constructor = findContractConstructorByLabel("default")! - const tx = C.extrinsic(C.local)({ + const tx = C.extrinsic(client)({ sender: T.alice.address, call: { type: "Contracts", @@ -68,7 +75,7 @@ function sendMessageDryRunContractCall( undefined, U.hex.decode(message.selector), ])) - return C.state.call(C.local)( + return C.state.call(client)( "ContractsApi_call", key, ) @@ -87,7 +94,7 @@ function sendGetMessage(address: Uint8Array) { undefined, U.hex.decode(message.selector), ])) - return C.state.call(C.local)( + return C.state.call(client)( "ContractsApi_call", key, ) @@ -116,7 +123,7 @@ function sendFlipMessage(address: Uint8Array) { storageDepositLimit: undefined, } }) - return C.extrinsic(C.local)({ + return C.extrinsic(client)({ sender: T.alice.address, call: C.Z.rec({ type: "Contracts", @@ -157,3 +164,10 @@ async function getContract(wasmFile: string, metadataFile: string) { deriveCodec, } } + +function getFilePath(relativeFilePath: string) { + return path.join( + path.dirname(path.fromFileUrl(import.meta.url)), + relativeFilePath, + ) +} From bacc83e489fcfc281deff6cab519e133c8df872a Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Mon, 12 Dec 2022 12:33:39 -0300 Subject: [PATCH 3/8] fix: cspell and format --- cspell.json | 3 ++- examples/smart_contract/metadata.json | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cspell.json b/cspell.json index 856043707..282c91603 100644 --- a/cspell.json +++ b/cspell.json @@ -17,6 +17,7 @@ "frame_metadata/raw_erc20_metadata.json", "target", "**/__snapshots__/*.snap", - "codegen/_output" + "codegen/_output", + "examples/smart_contract" ] } diff --git a/examples/smart_contract/metadata.json b/examples/smart_contract/metadata.json index cc9c0bfae..7060a8834 100644 --- a/examples/smart_contract/metadata.json +++ b/examples/smart_contract/metadata.json @@ -105,4 +105,4 @@ } ] } -} \ No newline at end of file +} From 90a21b6897346708178655602708f193633142f5 Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Tue, 13 Dec 2022 10:36:08 -0300 Subject: [PATCH 4/8] feat: move ContractsApi_Call codec to frame_metadata --- examples/smart_contract.ts | 17 ++-- .../zombienet.toml} | 0 frame_metadata/Contract.ts | 77 +++++++++++++++++++ 3 files changed, 83 insertions(+), 11 deletions(-) rename examples/{smart_contract.toml => smart_contract/zombienet.toml} (100%) diff --git a/examples/smart_contract.ts b/examples/smart_contract.ts index 391714aad..4e26670a5 100644 --- a/examples/smart_contract.ts +++ b/examples/smart_contract.ts @@ -2,10 +2,9 @@ import * as path from "http://localhost:5646/@local/deps/std/path.ts" import * as C from "http://localhost:5646/@local/mod.ts" import * as T from "http://localhost:5646/@local/test_util/mod.ts" import * as U from "http://localhost:5646/@local/util/mod.ts" +import { $contractsApiCallArgs, $contractsApiCallReturn } from "../frame_metadata/Contract.ts" -import { $contractsApiCallArgs, $contractsApiCallReturn } from "./smart_contract/codec.ts" - -const configFile = getFilePath("smart_contract.toml") +const configFile = getFilePath("smart_contract/zombienet.toml") const zombienet = await T.zombienet.start(configFile) const client = zombienet.clients.byName["collator01"]! @@ -110,15 +109,15 @@ function sendGetMessage(address: Uint8Array) { function sendFlipMessage(address: Uint8Array) { const message = findContractMessageByLabel("flip")! const value = sendMessageDryRunContractCall(address, message) - .next(({ gas_required }) => { + .next(({ gasRequired }) => { return { type: "call", dest: C.MultiAddress.Id(address), value: 0n, data: U.hex.decode(message.selector), gasLimit: { - refTime: gas_required.ref_time, - proofSize: gas_required.proof_size, + refTime: gasRequired.refTime, + proofSize: gasRequired.proofSize, }, storageDepositLimit: undefined, } @@ -158,11 +157,7 @@ async function getContract(wasmFile: string, metadataFile: string) { await Deno.readTextFile(metadataFile), )) const deriveCodec = C.M.DeriveCodec(metadata.V3.types) - return { - wasm, - metadata, - deriveCodec, - } + return { wasm, metadata, deriveCodec } } function getFilePath(relativeFilePath: string) { diff --git a/examples/smart_contract.toml b/examples/smart_contract/zombienet.toml similarity index 100% rename from examples/smart_contract.toml rename to examples/smart_contract/zombienet.toml diff --git a/frame_metadata/Contract.ts b/frame_metadata/Contract.ts index fa33bbde1..524dbdf6a 100644 --- a/frame_metadata/Contract.ts +++ b/frame_metadata/Contract.ts @@ -1,3 +1,4 @@ +import * as $ from "../deps/scale.ts" import { unreachable } from "../deps/std/testing/asserts.ts" import { Ty, TyDef, UnionTyDefMember } from "./scale_info.ts" @@ -182,3 +183,79 @@ export namespace ContractMetadata { return normalize(contractMetadata).V3.types } } + +const $balanceCodec = $.u128 + +export interface Weight { + refTime: bigint + proofSize: bigint +} +const $weightCodec: $.Codec = $.object( + ["refTime", $.compact($.u64)], + ["proofSize", $.compact($.u64)], +) + +export type ContractsApiCallArgs = [ + origin: Uint8Array, + dest: Uint8Array, + balance: bigint, + weight: Weight | undefined, + storageDepositLimit: bigint | undefined, + data: Uint8Array, +] +export const $contractsApiCallArgs: $.Codec = $.tuple( + // origin + $.sizedUint8Array(32), + // dest + $.sizedUint8Array(32), + // balance + $balanceCodec, + // weight + $.option($weightCodec), + // storage_deposit_limit + $.option($balanceCodec), + // data + $.uint8Array, +) + +export interface ContractsApiCallReturn { + gasConsumed: Weight + gasRequired: Weight + storageDeposit: { + type: "Refund" + value: bigint + } | { + type: "Charge" + value: bigint + } + debugMessage: string + result: { + flags: number + data: Uint8Array + } +} +export const $contractsApiCallReturn: $.Codec = $.object( + // gas_consumed + ["gasConsumed", $weightCodec], + // gas_required + ["gasRequired", $weightCodec], + // storage_deposit + [ + "storageDeposit", + $.taggedUnion("type", [ + ["Refund", ["value", $balanceCodec]], + ["Charge", ["value", $balanceCodec]], + ]), + ], + // debug_message + ["debugMessage", $.str], + // result + [ + "result", + $.object( + ["flags", $.u32], + // TODO: improve result error coded + ["data", $.result($.uint8Array, $.never)], + ), + ], +) From 702e53a5e83ec9f460fb345676058c4199af91d2 Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Wed, 14 Dec 2022 11:47:40 -0300 Subject: [PATCH 5/8] feat: compute gas estimate for contract instantiate --- examples/smart_contract.ts | 103 ++++++++++++++++++++++++++----------- frame_metadata/Contract.ts | 89 +++++++++++++++++++++++++++++++- 2 files changed, 161 insertions(+), 31 deletions(-) diff --git a/examples/smart_contract.ts b/examples/smart_contract.ts index 4e26670a5..33c1f142c 100644 --- a/examples/smart_contract.ts +++ b/examples/smart_contract.ts @@ -2,7 +2,12 @@ import * as path from "http://localhost:5646/@local/deps/std/path.ts" import * as C from "http://localhost:5646/@local/mod.ts" import * as T from "http://localhost:5646/@local/test_util/mod.ts" import * as U from "http://localhost:5646/@local/util/mod.ts" -import { $contractsApiCallArgs, $contractsApiCallReturn } from "../frame_metadata/Contract.ts" +import { + $contractsApiCallArgs, + $contractsApiCallResult, + $contractsApiInstantiateArgs, + $contractsApiInstantiateResult, +} from "../frame_metadata/Contract.ts" const configFile = getFilePath("smart_contract/zombienet.toml") const zombienet = await T.zombienet.start(configFile) @@ -23,31 +28,34 @@ await zombienet.close() function instantiateContractTx() { const constructor = findContractConstructorByLabel("default")! - const tx = C.extrinsic(client)({ - sender: T.alice.address, - call: { - type: "Contracts", - value: { + const salt = Uint8Array.from(Array.from([0, 0, 0, 0]), () => Math.floor(Math.random() * 16)) + const value = preSubmitContractInstantiateDryRunGasEstimate(constructor, contract.wasm, salt) + .next(({ gasRequired }) => { + return { type: "instantiateWithCode", value: 0n, - // TODO: create sendDryRunContractInitiate and fetch these gasLimit value gasLimit: { - refTime: 200_000_000_000n, - proofSize: 0n, + refTime: gasRequired.refTime, + proofSize: gasRequired.proofSize, }, storageDepositLimit: undefined, code: contract.wasm, data: U.hex.decode(constructor.selector), - salt: Uint8Array.from(Array.from([0, 0, 0, 0]), () => Math.floor(Math.random() * 16)), - }, - }, + salt, + } + }) + const tx = C.extrinsic(client)({ + sender: T.alice.address, + call: C.Z.rec({ + type: "Contracts", + value, + }), }) .signed(T.alice.sign) const finalizedIn = tx.watch(({ end }) => (status) => { - // .watch emits a single inBlock event - if (typeof status !== "string" && status.inBlock) { - return end(status.inBlock) + if (typeof status !== "string" && (status.inBlock ?? status.finalized)) { + return end(status.inBlock ?? status.finalized) } else if (C.rpc.known.TransactionStatus.isTerminal(status)) { return end(new Error()) } @@ -55,6 +63,12 @@ function instantiateContractTx() { } ) return C.events(tx, finalizedIn).next((events) => { + const extrinsicFailed = events.some((e) => + e.event?.type === "System" && e.event?.value?.type === "ExtrinsicFailed" + ) + if (extrinsicFailed) { + return new Error("extrinsic failed") + } const event = events.find((e) => e.event?.type === "Contracts" && e.event?.value?.type === "Instantiated" ) @@ -62,7 +76,30 @@ function instantiateContractTx() { }) } -function sendMessageDryRunContractCall( +function preSubmitContractInstantiateDryRunGasEstimate( + message: C.M.ContractMetadata.Constructor, + code: Uint8Array, + salt: Uint8Array, +) { + const key = U.hex.encode($contractsApiInstantiateArgs.encode([ + T.alice.publicKey, + 0n, + undefined, + undefined, + { type: "Upload", value: code }, + U.hex.decode(message.selector), + salt, + ])) + return C.state.call(client)( + "ContractsApi_instantiate", + key, + ) + .next((encodedResponse) => { + return $contractsApiInstantiateResult.decode(U.hex.decode(encodedResponse)) + }) +} + +function preSubmitContractCallDryRunGasEstimate( address: Uint8Array, message: C.M.ContractMetadata.Message | C.M.ContractMetadata.Constructor, ) { @@ -79,7 +116,7 @@ function sendMessageDryRunContractCall( key, ) .next((encodedResponse) => { - return $contractsApiCallReturn.decode(U.hex.decode(encodedResponse)) + return $contractsApiCallResult.decode(U.hex.decode(encodedResponse)) }) } @@ -98,7 +135,7 @@ function sendGetMessage(address: Uint8Array) { key, ) .next((encodedResponse) => { - const response = $contractsApiCallReturn.decode(U.hex.decode(encodedResponse)) + const response = $contractsApiCallResult.decode(U.hex.decode(encodedResponse)) if (message.returnType.type === null) { return undefined } @@ -108,7 +145,7 @@ function sendGetMessage(address: Uint8Array) { function sendFlipMessage(address: Uint8Array) { const message = findContractMessageByLabel("flip")! - const value = sendMessageDryRunContractCall(address, message) + const value = preSubmitContractCallDryRunGasEstimate(address, message) .next(({ gasRequired }) => { return { type: "call", @@ -122,7 +159,7 @@ function sendFlipMessage(address: Uint8Array) { storageDepositLimit: undefined, } }) - return C.extrinsic(client)({ + const tx = C.extrinsic(client)({ sender: T.alice.address, call: C.Z.rec({ type: "Contracts", @@ -130,17 +167,25 @@ function sendFlipMessage(address: Uint8Array) { }), }) .signed(T.alice.sign) - .watch(({ end }) => - (status) => { - // .watch emits a single inBlock event - if (typeof status !== "string" && status.inBlock) { - return end(status.inBlock) - } else if (C.rpc.known.TransactionStatus.isTerminal(status)) { - return end(new Error()) - } - return + const finalizedIn = tx.watch(({ end }) => + (status) => { + if (typeof status !== "string" && (status.inBlock ?? status.finalized)) { + return end(status.inBlock ?? status.finalized) + } else if (C.rpc.known.TransactionStatus.isTerminal(status)) { + return end(new Error()) } + return + } + ) + return C.Z.ls(finalizedIn, C.events(tx, finalizedIn)).next(([finalizedIn, events]) => { + const extrinsicFailed = events.some((e) => + e.event?.type === "System" && e.event?.value?.type === "ExtrinsicFailed" ) + if (extrinsicFailed) { + return new Error("extrinsic failed") + } + return finalizedIn + }) } function findContractConstructorByLabel(label: string) { diff --git a/frame_metadata/Contract.ts b/frame_metadata/Contract.ts index 524dbdf6a..0202d0289 100644 --- a/frame_metadata/Contract.ts +++ b/frame_metadata/Contract.ts @@ -218,7 +218,7 @@ export const $contractsApiCallArgs: $.Codec = $.tuple( $.uint8Array, ) -export interface ContractsApiCallReturn { +export interface ContractsApiCallResult { gasConsumed: Weight gasRequired: Weight storageDeposit: { @@ -234,7 +234,7 @@ export interface ContractsApiCallReturn { data: Uint8Array } } -export const $contractsApiCallReturn: $.Codec = $.object( +export const $contractsApiCallResult: $.Codec = $.object( // gas_consumed ["gasConsumed", $weightCodec], // gas_required @@ -259,3 +259,88 @@ export const $contractsApiCallReturn: $.Codec = $.object ), ], ) + +export type ContractsApiInstantiateArgs = [ + origin: Uint8Array, + balance: bigint, + gasLimit: Weight | undefined, + storageDepositLimit: bigint | undefined, + codeOrHash: { + type: "Upload" | "Existing" + value: Uint8Array + }, + data: Uint8Array, + salt: Uint8Array, +] +export const $contractsApiInstantiateArgs: $.Codec = $.tuple( + // origin + $.sizedUint8Array(32), + // balance + $balanceCodec, + // gasLimit + $.option($weightCodec), + // storageDepositLimit + $.option($balanceCodec), + // codeOrHash + $.taggedUnion("type", [ + // code + ["Upload", ["value", $.uint8Array]], + // hash + ["Existing", ["value", $.sizedUint8Array(32)]], + ]), + // data + $.uint8Array, + // salt + $.uint8Array, +) + +export interface ContractsApiInstantiateResult { + gasConsumed: Weight + gasRequired: Weight + storageDeposit: { + type: "Refund" + value: bigint + } | { + type: "Charge" + value: bigint + } + debugMessage: string + result: { + result: { + flags: number + data: Uint8Array + } + accountId: Uint8Array + } +} +export const $contractsApiInstantiateResult: $.Codec = $.object( + // gas_consumed + ["gasConsumed", $weightCodec], + // gas_required + ["gasRequired", $weightCodec], + // storage_deposit + [ + "storageDeposit", + $.taggedUnion("type", [ + ["Refund", ["value", $balanceCodec]], + ["Charge", ["value", $balanceCodec]], + ]), + ], + // debug_message + ["debugMessage", $.str], + // result + [ + "result", + $.object( + [ + "result", + $.object( + ["flags", $.u32], + // TODO: improve result error coded + ["data", $.result($.uint8Array, $.never)], + ), + ], + ["accountId", $.sizedUint8Array(32)], + ), + ], +) From 3925e81227ccb32438293bf4b3bd3cd4844cbce7 Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Wed, 14 Dec 2022 11:54:07 -0300 Subject: [PATCH 6/8] chore: add base58 dep comment --- deps/std/encoding/base58.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deps/std/encoding/base58.ts b/deps/std/encoding/base58.ts index 66333c9b0..b2361fcb0 100644 --- a/deps/std/encoding/base58.ts +++ b/deps/std/encoding/base58.ts @@ -1,2 +1,2 @@ -// export * from "https://deno.land/std@0.167.0/encoding/base58.ts" -export * from "https://raw.githubusercontent.com/kratico/deno_std/main/encoding/base58.ts" +// TODO: use std@0.168.0/encoding/base58.ts when https://github.com/denoland/deno_std/pull/2982 is released +export * from "https://raw.githubusercontent.com/denoland/deno_std/01696ce149463f498301782ac5e9ee322a86182c/encoding/base58.ts" From 730e773bc668f8ff3c3cb30c92dbead46e766239 Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Wed, 14 Dec 2022 11:58:53 -0300 Subject: [PATCH 7/8] chore: clean up --- examples/smart_contract/codec.ts | 48 -------------------------------- 1 file changed, 48 deletions(-) delete mode 100644 examples/smart_contract/codec.ts diff --git a/examples/smart_contract/codec.ts b/examples/smart_contract/codec.ts deleted file mode 100644 index 8081d6533..000000000 --- a/examples/smart_contract/codec.ts +++ /dev/null @@ -1,48 +0,0 @@ -import * as C from "http://localhost:5646/@local/mod.ts" - -const $balanceCodec = C.$.u128 -const $weightCodec = C.$.object( - ["ref_time", C.$.compact(C.$.u64)], - ["proof_size", C.$.compact(C.$.u64)], -) - -export const $contractsApiCallArgs = C.$.tuple( - // origin - C.$.sizedUint8Array(32), - // dest - C.$.sizedUint8Array(32), - // balance - $balanceCodec, - // weight - C.$.option($weightCodec), - // storage_deposit_limit - C.$.option($balanceCodec), - // data - C.$.uint8Array, -) - -export const $contractsApiCallReturn = C.$.object( - // gas_consumed - ["gas_consumed", $weightCodec], - // gas_required - ["gas_required", $weightCodec], - // storage_deposit - [ - "storage_deposit", - C.$.taggedUnion("type", [ - ["Refund", ["value", $balanceCodec]], - ["Charge", ["value", $balanceCodec]], - ]), - ], - // debug_message - ["debug_message", C.$.str], - // result - [ - "result", - C.$.object( - ["flags", C.$.u32], - // TODO: improve result error coded - ["data", C.$.result(C.$.uint8Array, C.$.never)], - ), - ], -) From 451c90cd781130ecc565c6a176698d5f0eff1fae Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Wed, 14 Dec 2022 12:09:38 -0300 Subject: [PATCH 8/8] chore: add derived contract address from dry run --- examples/smart_contract.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/smart_contract.ts b/examples/smart_contract.ts index 33c1f142c..49b79eb81 100644 --- a/examples/smart_contract.ts +++ b/examples/smart_contract.ts @@ -30,7 +30,9 @@ function instantiateContractTx() { const constructor = findContractConstructorByLabel("default")! const salt = Uint8Array.from(Array.from([0, 0, 0, 0]), () => Math.floor(Math.random() * 16)) const value = preSubmitContractInstantiateDryRunGasEstimate(constructor, contract.wasm, salt) - .next(({ gasRequired }) => { + .next(({ gasRequired, result: { accountId } }) => { + // the contract address derived from the code hash and the salt + console.log("Derived contract address", U.ss58.encode(42, accountId)) return { type: "instantiateWithCode", value: 0n,