From b1596c2313187106aa921fc8c3538ad90417e4d7 Mon Sep 17 00:00:00 2001 From: Dmitryii Osipov Date: Mon, 28 Aug 2023 14:02:18 +0400 Subject: [PATCH] Support payload in `read_state` rpc call (#1349) --- .github/workflows/CI-CD-gear-js-api.yml | 2 +- api/CHANGELOG.md | 4 + api/Makefile | 5 +- api/README.md | 26 +- api/programs/Cargo.lock | 423 ++++++++++++++-------- api/programs/test-gas/io/src/lib.rs | 2 +- api/programs/test-gas/test_gas.meta.txt | 1 - api/programs/test-meta/io/src/lib.rs | 8 +- api/programs/test-meta/src/wasm.rs | 20 +- api/programs/test-meta/test_meta.meta.txt | 1 - api/src/Gas.ts | 10 +- api/src/Message.ts | 10 +- api/src/Program.ts | 8 +- api/src/ResumeSession.ts | 2 +- api/src/State.ts | 78 ++-- api/src/Voucher.ts | 1 - api/src/default/rpc.json | 2 + api/src/default/types-metadata.json | 11 +- api/src/metadata/metadata.ts | 2 +- api/src/metadata/programMetadata.ts | 114 ++++-- api/src/types/interfaces/metadata.ts | 29 +- api/src/types/interfaces/program/index.ts | 1 + api/src/types/interfaces/program/state.ts | 43 +++ api/src/utils/create-payload.ts | 3 +- api/test/Code.test.ts | 4 +- api/test/Gas.test.ts | 4 +- api/test/Message.test.ts | 6 +- api/test/Meta.test.ts | 120 +----- api/test/Program.test.ts | 6 +- api/test/State.test.ts | 34 +- api/test/Voucher.test.ts | 4 +- api/test/config.ts | 2 +- api/test/testSequencer.js | 16 +- 33 files changed, 621 insertions(+), 381 deletions(-) delete mode 100644 api/programs/test-gas/test_gas.meta.txt delete mode 100644 api/programs/test-meta/test_meta.meta.txt create mode 100644 api/src/types/interfaces/program/state.ts diff --git a/.github/workflows/CI-CD-gear-js-api.yml b/.github/workflows/CI-CD-gear-js-api.yml index 306908dedf..efef24fb80 100644 --- a/.github/workflows/CI-CD-gear-js-api.yml +++ b/.github/workflows/CI-CD-gear-js-api.yml @@ -70,7 +70,7 @@ jobs: - name: "Prepare: build programs" working-directory: api/programs - run: cargo build --release --locked + run: cargo build --locked - name: "Prepare: download Gear examples" working-directory: api/test/wasm diff --git a/api/CHANGELOG.md b/api/CHANGELOG.md index 269a8166b4..37880a988f 100644 --- a/api/CHANGELOG.md +++ b/api/CHANGELOG.md @@ -8,6 +8,10 @@ https://github.com/gear-tech/gear-js/pull/1353 - Remove `sendMessageWithVoucher` and `sendReplyWithVoucher` methods according to https://github.com/gear-tech/gear/pull/3083. - Add optional `prepaid` and `account` fields to `api.message.send` and `api.message.sendReply` method arguments. They are used to send messages with the issued voucher. +https://github.com/gear-tech/gear-js/pull/1349 +- Add `payload` parameter to `api.programState.read` and `api.programState.readUsingWasm` methods according to https://github.com/gear-tech/gear/pull/3059 and https://github.com/gear-tech/gear/pull/3173 +- Support new `ProgramMetadata` version. + ## 0.32.4 _07/25/2023_ diff --git a/api/Makefile b/api/Makefile index 46ea9731a7..be99cd5981 100644 --- a/api/Makefile +++ b/api/Makefile @@ -8,7 +8,10 @@ update: @cargo update --manifest-path programs/Cargo.toml build: - @cargo build --release --manifest-path programs/Cargo.toml + @cargo +nightly build --release --manifest-path programs/Cargo.toml + +build-debug: + @cargo +nightly build --manifest-path programs/Cargo.toml clean: @rm -rvf programs/target diff --git a/api/README.md b/api/README.md index 7ae6fd6fbf..e7e8974977 100644 --- a/api/README.md +++ b/api/README.md @@ -85,14 +85,14 @@ There're 2 types of metadata. 1. `ProgramMetadata` is used to encode/decode messages to/from a program as well as to read the whole state of the program -_You can use `getProgramMetadata` function to create the program metadata. The function takes metadata of the program in format of hex string. It will return object of `ProgramMetadata` class that has property `types` that contains all types of the program._ +_You can use `ProgramMetadata.from` method to create the program metadata. The method takes metadata of the program in format of hex string. It will return object of `ProgramMetadata` class that has property `types` that contains all types of the program._ **You should pass an object of this class to function arguments when you want to send some extrinsics that require encoding payloads** ```javascript -import { getProgramMetadata } from '@gear-js/api'; +import { ProgramMetadata } from '@gear-js/api'; -const meta = getProgramMetadata(`0x...`); +const meta = ProgramMetadata.from(`0x...`); meta.types.init.input; // can be used to encode input message for init entrypoint of the program meta.types.init.output; // can be used to decode output message for init entrypoint of the program // the same thing available for all entrypoints of the program @@ -117,7 +117,7 @@ Both `ProgramMetadata` and `StateMetadata` classes have a few methods that can h ```javascript import { ProgramMetadata } from '@gear-js/api'; -const meta = getProgramMetadata(`0x...`); +const meta = ProgramMetadata.from(`0x...`); meta.getTypeName(4); // will return name of type with this index // or @@ -137,7 +137,7 @@ meta.createType(4, { value: 'value' }); // to encode or decode data **_Extrinsics are used to interact with programs running on blockchain networks powered by Gear Protocol_** -_In all cases of sending extrinsics, there is no need to encode the payloads by yourself. It's sufficient to pass the program metadata obtained from the `getProgramMetadata` function to the methods that creates extrinsics._ +_In all cases of sending extrinsics, there is no need to encode the payloads by yourself. It's sufficient to pass the program metadata obtained from the `ProgramMetadata.from` method to the methods that creates extrinsics._ ### Upload program @@ -280,7 +280,7 @@ Gas calculation returns GasInfo object contains 5 parameters: ```javascript const code = fs.readFileSync('demo_meta.opt.wasm'); -const meta = getProgramMetadata('0x...'); +const meta = ProgramMetadata.from('0x...'); const gas = await gearApi.program.calculateGas.handle( '0x...', // source id '0x...', //program id @@ -388,23 +388,27 @@ console.log(`Program with address ${programId} ${programExists ? 'exists' : "doe There are 2 ways to read state of a program. 1. Read full state of a program. - _To read full state of the program you need to have only metadata of this program. You can call `api.programState.read` method to get the state._ + _To read full state of the program you need to provide metadata of this program and input payload expected by the program. You can call `api.programState.read` method to get the state._ ```javascript - await api.programState.read({ programId: `0x...` }, programMetadata); + await api.programState.read({ programId: `0x...`, payload: [1, 2, 3] }, programMetadata); // Also you can read the state of the program at some specific block. - await api.programState.read({ programId: `0x...`, at: `0x...` }, programMetadata); + await api.programState.read({ programId: `0x...`, payload: [1, 2, 3], at: `0x...` }, programMetadata); ``` 2. Read state using wasm. _If you have some program that is able to read state and return you only necessary data you need to use `api.programState.readUsingWasm` method._ + _Note that since `state` function of gear programs expects input payload you need to provide it in the parameters of `readUsingWasm` method_ ```javascript const wasm = readFileSync('path/to/state.meta.wasm'); + const stateMetadata = await getStateMetadata(wasm); + const programMetadata = ProgramMetadata.from('0x...'); const state = await api.programState.readUsingWasm( - { programId, fn_name: 'name_of_function_to_execute', wasm, argument: { input: 'payload' } }, - wasm, + { programId, fn_name: 'name_of_function_to_execute', wasm, payload: [1, 2, 3], argument: { input: 'payload' } }, + stateMetadata, + programMetadata ); ``` diff --git a/api/programs/Cargo.lock b/api/programs/Cargo.lock index 99e45c14cd..b9bc600858 100644 --- a/api/programs/Cargo.lock +++ b/api/programs/Cargo.lock @@ -15,13 +15,19 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "1.0.2" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41" +checksum = "6748e8def348ed4d14996fa801f4122cd763fff530258cdc03f64b25f89d3a5a" dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" + [[package]] name = "android-tzdata" version = "0.1.1" @@ -39,9 +45,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.71" +version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" [[package]] name = "arrayvec" @@ -58,23 +64,24 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" + [[package]] name = "bitvec" version = "1.0.1" @@ -123,18 +130,18 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "camino" -version = "1.1.4" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c530edf18f37068ac2d977409ed5cd50d53d73bc653c7647b48eb78976ac9ae2" +checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c" dependencies = [ "serde", ] [[package]] name = "cargo-platform" -version = "0.1.2" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbdb825da8a5df079a43676dbe042702f1707b1109f713a01420fbb4cc71fa27" +checksum = "2cfa25e60aea747ec7e1124f238816749faa93759c6ff5b31f1ccdda137f4479" dependencies = [ "serde", ] @@ -155,9 +162,12 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.79" +version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +dependencies = [ + "libc", +] [[package]] name = "cfg-if" @@ -182,13 +192,13 @@ dependencies = [ [[package]] name = "colored" -version = "2.0.0" +version = "2.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" +checksum = "2674ec482fbc38012cf31e6c42ba0177b431a0cb6f15fe40efa5aab1bda516f6" dependencies = [ - "atty", + "is-terminal", "lazy_static", - "winapi", + "windows-sys", ] [[package]] @@ -228,6 +238,26 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "dlmalloc" version = "0.1.4" @@ -242,9 +272,9 @@ dependencies = [ [[package]] name = "either" -version = "1.8.1" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" [[package]] name = "enum-iterator" @@ -263,14 +293,35 @@ checksum = "eecf8589574ce9b895052fa12d69af7a233f99e6107f5cb8dd1044f2a17bfdcb" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.29", ] [[package]] name = "equivalent" -version = "1.0.0" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "errno" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88bffebc5d80432c9b140ee17875ff173a8ab62faad5b257da912bd2f6c1c0a1" +checksum = "6b30f669a7961ef1631673d2766cc92f52d64f7ef354d4fe0ddfd30ed52f0f4f" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] [[package]] name = "fixed-hash" @@ -350,18 +401,19 @@ dependencies = [ [[package]] name = "galloc" -version = "0.2.1" -source = "git+https://github.com/gear-tech/gear.git#3f1d903f74dcd79ae0161192c53436a96ca1cf0e" +version = "0.3.2" +source = "git+https://github.com/gear-tech/gear.git#0677deae2d9d06576a4b42012063e149a290d72c" dependencies = [ "dlmalloc", ] [[package]] name = "gcore" -version = "0.2.1" -source = "git+https://github.com/gear-tech/gear.git#3f1d903f74dcd79ae0161192c53436a96ca1cf0e" +version = "0.3.2" +source = "git+https://github.com/gear-tech/gear.git#0677deae2d9d06576a4b42012063e149a290d72c" dependencies = [ "gear-core-errors", + "gear-stack-buffer", "gsys", "parity-scale-codec", "static_assertions", @@ -369,14 +421,16 @@ dependencies = [ [[package]] name = "gear-core" -version = "0.1.0" -source = "git+https://github.com/gear-tech/gear.git#3f1d903f74dcd79ae0161192c53436a96ca1cf0e" +version = "0.3.2" +source = "git+https://github.com/gear-tech/gear.git#0677deae2d9d06576a4b42012063e149a290d72c" dependencies = [ "blake2-rfc", + "byteorder", "derive_more", + "enum-iterator", "gear-core-errors", "gear-wasm-instrument", - "hashbrown 0.13.2", + "hashbrown", "hex", "log", "parity-scale-codec", @@ -388,23 +442,35 @@ dependencies = [ [[package]] name = "gear-core-errors" -version = "0.1.0" -source = "git+https://github.com/gear-tech/gear.git#3f1d903f74dcd79ae0161192c53436a96ca1cf0e" +version = "0.3.2" +source = "git+https://github.com/gear-tech/gear.git#0677deae2d9d06576a4b42012063e149a290d72c" dependencies = [ "derive_more", "enum-iterator", "scale-info", ] +[[package]] +name = "gear-stack-buffer" +version = "0.3.2" +source = "git+https://github.com/gear-tech/gear.git#0677deae2d9d06576a4b42012063e149a290d72c" + +[[package]] +name = "gear-wasm" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f745ed9eb163f4509f01d5564e37db52ec43dd23569bafdba597a5f1e4c125c9" + [[package]] name = "gear-wasm-builder" version = "0.1.2" -source = "git+https://github.com/gear-tech/gear.git#3f1d903f74dcd79ae0161192c53436a96ca1cf0e" +source = "git+https://github.com/gear-tech/gear.git#0677deae2d9d06576a4b42012063e149a290d72c" dependencies = [ "anyhow", "cargo_metadata", "chrono", "colored", + "dirs", "gear-core", "gear-wasm-instrument", "gmeta", @@ -421,17 +487,28 @@ dependencies = [ [[package]] name = "gear-wasm-instrument" -version = "0.1.0" -source = "git+https://github.com/gear-tech/gear.git#3f1d903f74dcd79ae0161192c53436a96ca1cf0e" +version = "0.3.2" +source = "git+https://github.com/gear-tech/gear.git#0677deae2d9d06576a4b42012063e149a290d72c" dependencies = [ "enum-iterator", - "wasm-instrument", + "gwasm-instrument", +] + +[[package]] +name = "getrandom" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.11.0+wasi-snapshot-preview1", ] [[package]] name = "gmeta" -version = "0.2.1" -source = "git+https://github.com/gear-tech/gear.git#3f1d903f74dcd79ae0161192c53436a96ca1cf0e" +version = "0.3.2" +source = "git+https://github.com/gear-tech/gear.git#0677deae2d9d06576a4b42012063e149a290d72c" dependencies = [ "blake2-rfc", "derive_more", @@ -442,18 +519,18 @@ dependencies = [ [[package]] name = "gmeta-codegen" -version = "0.1.0" -source = "git+https://github.com/gear-tech/gear.git#3f1d903f74dcd79ae0161192c53436a96ca1cf0e" +version = "0.3.2" +source = "git+https://github.com/gear-tech/gear.git#0677deae2d9d06576a4b42012063e149a290d72c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.29", ] [[package]] name = "gstd" -version = "0.2.1" -source = "git+https://github.com/gear-tech/gear.git#3f1d903f74dcd79ae0161192c53436a96ca1cf0e" +version = "0.3.2" +source = "git+https://github.com/gear-tech/gear.git#0677deae2d9d06576a4b42012063e149a290d72c" dependencies = [ "bs58", "futures", @@ -461,7 +538,7 @@ dependencies = [ "gcore", "gear-core-errors", "gstd-codegen", - "hashbrown 0.13.2", + "hashbrown", "hex", "parity-scale-codec", "primitive-types", @@ -472,25 +549,25 @@ dependencies = [ [[package]] name = "gstd-codegen" version = "0.1.0" -source = "git+https://github.com/gear-tech/gear.git#3f1d903f74dcd79ae0161192c53436a96ca1cf0e" +source = "git+https://github.com/gear-tech/gear.git#0677deae2d9d06576a4b42012063e149a290d72c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.29", ] [[package]] name = "gsys" -version = "0.2.1" -source = "git+https://github.com/gear-tech/gear.git#3f1d903f74dcd79ae0161192c53436a96ca1cf0e" +version = "0.3.2" +source = "git+https://github.com/gear-tech/gear.git#0677deae2d9d06576a4b42012063e149a290d72c" [[package]] -name = "hashbrown" -version = "0.13.2" +name = "gwasm-instrument" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +checksum = "bcb127cb43d375de7cdacffd0e4e1c746e52381d11a0465909ae6fbecb99c6c3" dependencies = [ - "ahash", + "gear-wasm", ] [[package]] @@ -498,15 +575,16 @@ name = "hashbrown" version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" +dependencies = [ + "ahash", + "allocator-api2", +] [[package]] name = "hermit-abi" -version = "0.1.19" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] +checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" [[package]] name = "hex" @@ -564,7 +642,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d" dependencies = [ "equivalent", - "hashbrown 0.14.0", + "hashbrown", ] [[package]] @@ -573,11 +651,22 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e04e2fd2b8188ea827b32ef11de88377086d690286ab35747ef7f9bf3ccb590" +[[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys", +] + [[package]] name = "itoa" -version = "1.0.6" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "js-sys" @@ -608,11 +697,17 @@ dependencies = [ "libc", ] +[[package]] +name = "linux-raw-sys" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503" + [[package]] name = "log" -version = "0.4.19" +version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] name = "memchr" @@ -628,9 +723,9 @@ checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" [[package]] name = "num-traits" -version = "0.2.15" +version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" dependencies = [ "autocfg", ] @@ -683,17 +778,11 @@ version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be5e13c266502aadf83426d87d81a0f5d1ef45b8027f5a471c360abfe4bfae92" -[[package]] -name = "parity-wasm" -version = "0.45.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1ad0aff30c1da14b1254fcb2af73e1fa9a28670e584a626f53a369d0e157304" - [[package]] name = "paste" -version = "1.0.12" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" +checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" [[package]] name = "pathdiff" @@ -703,9 +792,9 @@ checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" [[package]] name = "pin-project-lite" -version = "0.2.9" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" +checksum = "12cc1b0bf1727a77a54b6654e7b5f1af8604923edc8b81885f8ec92f9e3f0a05" [[package]] name = "pin-utils" @@ -752,14 +841,14 @@ checksum = "2ecdabd73c8beaf98c66e45aff3032b56260ee49eb5d0d1222ecce269bfafda7" dependencies = [ "byteorder", "log", - "parity-wasm 0.42.2", + "parity-wasm", ] [[package]] name = "quote" -version = "1.0.31" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fe8a65d69dd0808184ebb5f836ab526bb259db23c657efa38711b1072ee47f0" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] @@ -770,11 +859,43 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags 1.3.2", +] + +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom", + "redox_syscall", + "thiserror", +] + [[package]] name = "regex" -version = "1.8.4" +version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0ab3ca65655bb1e41f2a8c8cd662eb4fb035e67c3f78da1d61dffe89d07300f" +checksum = "81bc1d4caf89fac26a70747fe603c130093b53c773888797a6329091246d651a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fed1ceff11a1dddaee50c9dc8e4938bd106e9d89ae372f192311e7da498e3b69" dependencies = [ "aho-corasick", "memchr", @@ -783,9 +904,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.7.2" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78" +checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" [[package]] name = "rustc_version" @@ -796,11 +917,24 @@ dependencies = [ "semver", ] +[[package]] +name = "rustix" +version = "0.38.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19ed4fa021d81c8392ce04db050a3da9a60299050b7ae1cf482d862b54a7218f" +dependencies = [ + "bitflags 2.4.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "ryu" -version = "1.0.13" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" [[package]] name = "scale-info" @@ -828,38 +962,38 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.17" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" +checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" dependencies = [ "serde", ] [[package]] name = "serde" -version = "1.0.171" +version = "1.0.186" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30e27d1e4fd7659406c492fd6cfaf2066ba8773de45ca75e855590f856dc34a9" +checksum = "9f5db24220c009de9bd45e69fb2938f4b6d2df856aa9304ce377b3180f83b7c1" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.171" +version = "1.0.186" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "389894603bd18c46fa56231694f8d827779c0951a667087194cf9de94ed24682" +checksum = "5ad697f7e0b65af4983a4ce8f56ed5b357e8d3c36651bf6a7e13639c17b8e670" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.29", ] [[package]] name = "serde_json" -version = "1.0.99" +version = "1.0.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46266871c240a00b8f503b877622fe33430b3c7d963bdc0f2adc511e54a1eae3" +checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360" dependencies = [ "itoa", "ryu", @@ -900,9 +1034,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.26" +version = "2.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45c3457aacde3c65315de5031ec191ce46604304d2446e803d71ade03308d970" +checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a" dependencies = [ "proc-macro2", "quote", @@ -992,22 +1126,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.40" +version = "1.0.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" +checksum = "97a802ec30afc17eee47b2855fc72e0c4cd62be9b4efe6591edde0ec5bd68d8f" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.40" +version = "1.0.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" +checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.29", ] [[package]] @@ -1017,15 +1151,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" dependencies = [ "libc", - "wasi", + "wasi 0.10.0+wasi-snapshot-preview1", "winapi", ] [[package]] name = "toml" -version = "0.7.5" +version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ebafdf5ad1220cb59e7d17cf4d2c72015297b75b19a10472f99b89225089240" +checksum = "c17e963a819c331dcacd7ab957d80bc2b9a9c1e71c804826d2f283dd65306542" dependencies = [ "serde", "serde_spanned", @@ -1044,9 +1178,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.19.11" +version = "0.19.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "266f016b7f039eec8a1a80dfe6156b633d208b9fccca5e4db1d6775b0c4e34a7" +checksum = "f8123f27e969974a3dfba720fdb560be359f57b44302d280ba72e76a74480e8a" dependencies = [ "indexmap", "serde", @@ -1069,9 +1203,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.9" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0" +checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" [[package]] name = "version_check" @@ -1085,6 +1219,12 @@ version = "0.10.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.87" @@ -1106,7 +1246,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.29", "wasm-bindgen-shared", ] @@ -1128,7 +1268,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.29", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1139,14 +1279,6 @@ version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" -[[package]] -name = "wasm-instrument" -version = "0.2.1" -source = "git+https://github.com/gear-tech/wasm-instrument.git?branch=gear-stable#756a8b92dab5a5fa841226eebbaf215812262e3b" -dependencies = [ - "parity-wasm 0.45.0", -] - [[package]] name = "wasmparser-nostd" version = "0.100.1" @@ -1199,10 +1331,19 @@ dependencies = [ ] [[package]] -name = "windows-targets" +name = "windows-sys" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", @@ -1215,51 +1356,51 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_msvc" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_i686_gnu" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_msvc" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_x86_64_gnu" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnullvm" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_msvc" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "winnow" -version = "0.4.7" +version = "0.5.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca0ace3845f0d96209f0375e6d367e3eb87eb65d27d445bdc9f1843a26f39448" +checksum = "7c2e3184b9c4e92ad5167ca73039d0c42476302ab603e2fec4487511f38ccefc" dependencies = [ "memchr", ] diff --git a/api/programs/test-gas/io/src/lib.rs b/api/programs/test-gas/io/src/lib.rs index 09053c810c..9fe208b8ff 100644 --- a/api/programs/test-gas/io/src/lib.rs +++ b/api/programs/test-gas/io/src/lib.rs @@ -16,7 +16,7 @@ impl Metadata for ProgramMetadata { type Reply = InputStruct; type Others = InOut<(), ()>; type Signal = (); - type State = (); + type State = InOut<(), ()>; } #[derive(TypeInfo, Decode, Encode)] diff --git a/api/programs/test-gas/test_gas.meta.txt b/api/programs/test-gas/test_gas.meta.txt deleted file mode 100644 index 058083c914..0000000000 --- a/api/programs/test-gas/test_gas.meta.txt +++ /dev/null @@ -1 +0,0 @@ -0001000100000000010100000001000000000101000000010000000000000000d80800082c746573745f6761735f696f2c496e7075745374727563740000040114696e707574040118537472696e670000040000050200 \ No newline at end of file diff --git a/api/programs/test-meta/io/src/lib.rs b/api/programs/test-meta/io/src/lib.rs index eb3e6208be..9e35987709 100644 --- a/api/programs/test-meta/io/src/lib.rs +++ b/api/programs/test-meta/io/src/lib.rs @@ -5,7 +5,11 @@ extern crate alloc; use alloc::string::String; use codec::{Decode, Encode}; use gmeta::{InOut, Metadata}; -use gstd::{prelude::*, ActorId, BTreeMap, BTreeSet}; +use gstd::{ + collections::{BTreeMap, BTreeSet}, + prelude::*, + ActorId, +}; use primitive_types::H256; use scale_info::TypeInfo; @@ -17,7 +21,7 @@ impl Metadata for ProgramMetadata { type Others = InOut<(), ()>; type Reply = String; type Signal = H256; - type State = Vec; + type State = InOut, Vec>; } #[derive(TypeInfo, Default, Decode, Encode)] diff --git a/api/programs/test-meta/src/wasm.rs b/api/programs/test-meta/src/wasm.rs index 9a7468039b..551b7dd75f 100644 --- a/api/programs/test-meta/src/wasm.rs +++ b/api/programs/test-meta/src/wasm.rs @@ -1,5 +1,9 @@ use crate::{Action, Id, Person, Wallet}; -use gstd::{msg, prelude::*, BTreeMap}; +use gstd::{ + collections::{BTreeMap, BTreeSet}, + debug, msg, + prelude::*, +}; use test_meta_io::EmptyStruct; static mut STATE: Vec = Vec::new(); @@ -64,5 +68,17 @@ async fn main() { #[no_mangle] extern "C" fn state() { - msg::reply(unsafe { STATE.clone() }, 0).expect("Error in state"); + debug!("{:?}", msg::load_bytes()); + let input: Option = msg::load().expect("Unable to load input"); + let mut result: Vec = Vec::new(); + if input.is_some() { + let wallet = unsafe { STATE.get(input.unwrap() as usize) }; + if wallet.is_some() { + result.push(wallet.unwrap().clone()); + } + } else { + result = unsafe { STATE.clone() }; + } + + msg::reply(result, 0).expect("Error in state"); } diff --git a/api/programs/test-meta/test_meta.meta.txt b/api/programs/test-meta/test_meta.meta.txt deleted file mode 100644 index 10d4b2acd2..0000000000 --- a/api/programs/test-meta/test_meta.meta.txt +++ /dev/null @@ -1 +0,0 @@ -000100010000000001030000000107000000010400000001040000000000011a000000011b000000bd0f7c00042042547265655365740404540104000400080000000400000503000800000204000c042042547265654d617008044b0110045601040004001400000010000005020014000002180018000004081004001c0830746573745f6d6574615f696f18416374696f6e0001180c4f6e6504002001384f7074696f6e3c537472696e673e0000000c54776f04002401185665633c583e0001001454687265650401186669656c6431340164526573756c743c2875382c20537472696e67292c206933323e00020010466f75720400400150536f6d655374727563743c753132382c2075383e00030010466976650400540154536f6d655374727563743c537472696e672c20583e0004000c536978080050011c4163746f724964000060012c456d707479537472756374000500002004184f7074696f6e04045401100108104e6f6e6500000010536f6d650400100000010000240000022800280830746573745f6d6574615f696f0458000004002c01242875382c207531362900002c00000408043000300000050400340418526573756c7408045401380445013c0108084f6b040038000000000c45727204003c000001000038000004080410003c0000050b00400830746573745f6d6574615f696f28536f6d655374727563740808503101440850320104000c011861727261793848011c5b50313b20385d00011c617272617933324c01205b50323b2033325d0001146163746f7250011c4163746f7249640000440000050700480000030800000044004c0000032000000004005010106773746418636f6d6d6f6e287072696d6974697665731c4163746f724964000004004c01205b75383b2033325d0000540830746573745f6d6574615f696f28536f6d655374727563740808503101100850320128000c011861727261793858011c5b50313b20385d00011c617272617933325c01205b50323b2033325d0001146163746f7250011c4163746f7249640000580000030800000010005c000003200000002800600830746573745f6d6574615f696f2c456d7074795374727563740000040114656d7074796401082829000064000004000068083c7072696d69746976655f74797065731048323536000004004c01205b75383b2033325d00006c0000027000700830746573745f6d6574615f696f1857616c6c6574000008010869647401084964000118706572736f6e780118506572736f6e0000740830746573745f6d6574615f696f084964000008011c646563696d616c4401107531323800010c68657808011c5665633c75383e0000780830746573745f6d6574615f696f18506572736f6e000008011c7375726e616d65100118537472696e670001106e616d65100118537472696e670000 \ No newline at end of file diff --git a/api/src/Gas.ts b/api/src/Gas.ts index 02fdc4c2e6..44a2c4ad0e 100644 --- a/api/src/Gas.ts +++ b/api/src/Gas.ts @@ -16,13 +16,13 @@ export class GearGas { * @param payload Payload of init message * @param value Value of message * @param allowOtherPanics Should RPC call return error if other contracts panicked, during communication with the initial one - * @param meta (optional) Program metadata obtained using `getProgramMetadata` function. + * @param meta (optional) Program metadata obtained using `ProgramMetadata.from` method. * @param typeIndexOrTypeName Index of type in the registry. If not specified the type index from `meta.init.input` will be used instead. * If meta is not passed it's possible to specify type name that can be one of the default rust types * @example * ```javascript * const code = fs.readFileSync('demo_meta.opt.wasm'); - * const meta = await getProgramMetadata('0x...'); + * const meta = ProgramMetadata.from('0x...'); * const gas = await gearApi.program.gasSpent.init( * '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', * code, @@ -79,7 +79,7 @@ export class GearGas { * @example * ```javascript * const code = fs.readFileSync('demo_meta.opt.wasm'); - * const meta = await getProgramMetadata('0x...'); + * const meta = ProgramMetadata.from('0x...'); * const gas = await gearApi.program.gasSpent.init( * '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', * code, @@ -137,7 +137,7 @@ export class GearGas { * @example * ```javascript * const code = fs.readFileSync('demo_meta.opt.wasm'); - * const meta = await getProgramMetadata('0x...'); + * const meta = ProgramMetadata.from('0x...'); * const gas = await gearApi.program.gasSpent.handle( * '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', * '0xa178362715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', @@ -187,7 +187,7 @@ export class GearGas { * @example * ```javascript * const code = fs.readFileSync('demo_async.opt.wasm'); - * const meta = await getProgramMetadata('0x...'); + * const meta = ProgramMetadata.from('0x...'); * const gas = await gearApi.program.gasSpent.reply( * '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', * '0x518e6bc03d274aadb3454f566f634bc2b6aef9ae6faeb832c18ae8300fd72635', diff --git a/api/src/Message.ts b/api/src/Message.ts index bc546f97f1..1a3da92fea 100644 --- a/api/src/Message.ts +++ b/api/src/Message.ts @@ -21,7 +21,7 @@ export class GearMessage extends GearTransaction { * ```javascript * const programId = '0x..'; * const hexMeta = '0x...'; - * const meta = getProgramMetadata(hexMeta); + * const meta = ProgramMetadata.from(hexMeta); * * const tx = api.message.send({ * destination: programId, @@ -117,7 +117,7 @@ export class GearMessage extends GearTransaction { const _payload = encodePayload(payload, metaOrHexRegistry, 'handle', typeIndexOrTypeName); try { - this.extrinsic = this._api.tx.gear.sendMessage(destination, _payload, gasLimit, value || 0, prepaid); + this.extrinsic = this._api.tx.gear.sendMessage(destination, _payload, gasLimit, value || 0, prepaid || false); return this.extrinsic; } catch (error) { throw new SendMessageError(error.message); @@ -127,14 +127,14 @@ export class GearMessage extends GearTransaction { /** * ## Send reply message * @param args Message parameters - * @param meta Program metadata obtained using `getProgramMetadata` function. + * @param meta Program metadata obtained using `ProgramMetadata.from` method. * @param typeIndex (optional) Index of type in the registry. If not specified the type index from `meta.reply.input` will be used instead. * @returns Submitted result * @example * ```javascript * const replyToMessage = '0x..'; * const hexMeta = '0x...'; - * const meta = getProgramMetadata(hexMeta); + * const meta = ProgramMetadata.from(hexMeta); * * const tx = api.message.send({ * replyToId: replyToMessage, @@ -237,7 +237,7 @@ export class GearMessage extends GearTransaction { const _payload = encodePayload(payload, metaOrHexRegistry, 'reply', typeIndexOrTypeName); try { - this.extrinsic = this._api.tx.gear.sendReply(replyToId, _payload, gasLimit, value, prepaid); + this.extrinsic = this._api.tx.gear.sendReply(replyToId, _payload, gasLimit, value, prepaid || false); return this.extrinsic; } catch (error) { throw new SendReplyError(); diff --git a/api/src/Program.ts b/api/src/Program.ts index 4e8dc5a22d..af23c52826 100644 --- a/api/src/Program.ts +++ b/api/src/Program.ts @@ -41,7 +41,7 @@ export class GearProgram extends GearTransaction { /** * ### Upload program with code using program metadata to encode payload * @param args Program parameters - * @param meta (optional) Program metadata obtained using `getProgramMetadata` function. + * @param meta (optional) Program metadata obtained using `ProgramMetadata.from` method * @param typeIndex (optional) Index of type in the registry. If not specified the type index from `meta.init.input` will be used instead. * @returns Object containing program id, generated (or specified) salt, code id, prepared extrinsic * @example @@ -49,7 +49,7 @@ export class GearProgram extends GearTransaction { * const api = await GearApi.create(); * const code = fs.readFileSync('path/to/program.opt.wasm'); * cosnt hexMeta = '0x...'; - * const meta = getProgramMetadata(hexMeta); + * const meta = ProgramMetadata.from(hexMeta); * const { programId, codeId, salt, extrinsic } = api.program.upload({ * code, * initPayload: { field: 'someValue' }, @@ -108,7 +108,7 @@ export class GearProgram extends GearTransaction { /** * ### Create program from uploaded on chain code using program metadata to encode payload * @param args Program parameters - * @param meta (optional) Program metadata obtained using `getProgramMetadata` function. + * @param meta (optional) Program metadata obtained using `ProgramMetadata.from` method. * @param typeIndex (optional) Index of type in the registry. If not specified the type index from `meta.init.input` will be used instead. * @returns Object containing program id, generated (or specified) salt, prepared extrinsic * @example @@ -116,7 +116,7 @@ export class GearProgram extends GearTransaction { * const api = await GearApi.create(); * const codeId = '0x...'; * cosnt hexMeta = '0x...'; - * const meta = getProgramMetadata(hexMeta); + * const meta = ProgramMetadata.from(hexMeta); * const { programId, codeId, salt, extrinsic } = api.program.create({ * code, * initPayload: { field: 'someValue' }, diff --git a/api/src/ResumeSession.ts b/api/src/ResumeSession.ts index 875be5fb6d..30e73e446a 100644 --- a/api/src/ResumeSession.ts +++ b/api/src/ResumeSession.ts @@ -8,7 +8,7 @@ import { CreateType } from './metadata'; import { GearApi } from './GearApi'; import { GearTransaction } from './Transaction'; -const SIXTEEN_KB = 16384; +const SIXTEEN_KB = 0x4000; export class GearResumeSession extends GearTransaction { constructor(protected _api: GearApi) { diff --git a/api/src/State.ts b/api/src/State.ts index a563f28258..c57cf5b6e9 100644 --- a/api/src/State.ts +++ b/api/src/State.ts @@ -1,15 +1,10 @@ import { Codec } from '@polkadot/types/types'; -import { HexString } from '@polkadot/util/types'; -import { CreateType, ProgramMetadata, StateMetadata } from './metadata'; +import { CreateType, MetadataVersion, ProgramMetadata, StateMetadata } from './metadata'; +import { HumanTypesRepr, ReadStateParams, ReadStateUsingWasmParams } from './types'; import { Bytes } from '@polkadot/types'; import { GearProgramStorage } from './Storage'; -interface ReadStateArgs { - programId: HexString; - at?: HexString; -} - export class GearProgramState extends GearProgramStorage { /** * ## Read state using meta wasm file @@ -17,36 +12,65 @@ export class GearProgramState extends GearProgramStorage { * @param meta StateMetadata returned from getStateMetadata function */ async readUsingWasm( - args: { - programId: HexString; - fn_name: string; - wasm: Buffer | Uint8Array | HexString; - argument?: any; - at?: HexString; - }, - meta: StateMetadata, + params: ReadStateUsingWasmParams, + stateMeta: StateMetadata, + programMeta: ProgramMetadata, ): Promise { - const fnTypes = meta?.functions[args.fn_name]; + const fnTypes = stateMeta?.functions[params.fn_name]; + const stateType = + programMeta.version === MetadataVersion.V2Rust ? (programMeta.types.state as HumanTypesRepr).input : null; - const payload = + const argument = fnTypes?.input !== undefined && fnTypes?.input !== null - ? Array.from(meta.createType(fnTypes.input, args.argument).toU8a()) + ? Array.from(stateMeta.createType(fnTypes.input, params.argument).toU8a()) : null; - const code = typeof args.wasm === 'string' ? args.wasm : CreateType.create('Bytes', Array.from(args.wasm)); + const payload = isNaN(stateType) + ? [] + : Array.from(programMeta.createType((programMeta.types.state as HumanTypesRepr).input, params.payload).toU8a()); - const state = await this._api.rpc['gear'].readStateUsingWasm(args.programId, args.fn_name, code, payload, args.at); - return meta && fnTypes ? meta.createType(fnTypes.output, state) : state; + const code = + typeof params.wasm === 'string' ? params.wasm : CreateType.create('Bytes', Array.from(params.wasm)); + + const state = await this._api.rpc['gear'].readStateUsingWasm( + params.programId, + payload, + params.fn_name, + code, + argument, + params.at, + ); + return stateMeta && fnTypes ? stateMeta.createType(fnTypes.output, state) : state; } /** - * - * @param args ProgramId and hash of block where it's necessary to read state (optional) - * @param meta Program metadata returned from getProgramMetadata function + * ### Read state of program (calls `gear_readState` rpc call) + * @param args ProgramId, payload and hash of block where it's necessary to read state (optional) + * @param meta Program metadata returned from `ProgramMetadata.from` method. * @param type (optional) Index of type to decode state. metadata.types.state is uesd by default + * + * @example + * const meta = ProgramMetadata.from('0x...'); + * const programId = '0x...'; + * + * const result = await api.programState.read({ programId, payload: { id: 1 } }, meta); + * console.log(result.toJSON()); */ - async read(args: ReadStateArgs, meta: ProgramMetadata, type?: number): Promise { - const state = await this._api.rpc['gear'].readState(args.programId, args.at || null); - return meta.createType(type || meta.types.state, state); + async read(args: ReadStateParams, meta: ProgramMetadata, type?: number): Promise { + const payload = + meta.version === MetadataVersion.V2Rust + ? Array.from(meta.createType((meta.types.state as HumanTypesRepr).input, args.payload).toU8a()) + : []; + const state = await this._api.rpc['gear'].readState(args.programId, payload, args.at || null); + + if (type !== undefined) { + return meta.createType(type, state); + } + + if (meta.version === MetadataVersion.V1Rust) { + return meta.createType(meta.types.state as number, state); + } + + return meta.createType((meta.types.state as HumanTypesRepr).output, state); } } diff --git a/api/src/Voucher.ts b/api/src/Voucher.ts index 5f852b034c..cbfb1fa26f 100644 --- a/api/src/Voucher.ts +++ b/api/src/Voucher.ts @@ -30,7 +30,6 @@ export class GearVoucher extends GearTransaction { ): { extrinsic: SubmittableExtrinsic<'promise', ISubmittableResult>; voucherId: HexString } { const voucherId = generateVoucherId(to, program); this.extrinsic = this._api.tx.gearVoucher.issue(to, program, value); - this.extrinsic.signAndSend('', () => {}); return { extrinsic: this.extrinsic, voucherId }; } diff --git a/api/src/default/rpc.json b/api/src/default/rpc.json index 0a7422971a..737ed34aa3 100644 --- a/api/src/default/rpc.json +++ b/api/src/default/rpc.json @@ -115,6 +115,7 @@ "readState": { "params": [ { "name": "program_id", "type": "H256" }, + { "name": "payload", "type": "Vec" }, { "name": "at", "type": "Option" } ], "type": "Bytes" @@ -122,6 +123,7 @@ "readStateUsingWasm": { "params": [ { "name": "program_id", "type": "H256" }, + { "name": "payload", "type": "Bytes" }, { "name": "fn_name", "type": "Bytes" }, { "name": "wasm", "type": "Bytes" }, { "name": "argument", "type": "Option" }, diff --git a/api/src/default/types-metadata.json b/api/src/default/types-metadata.json index 045a046cdb..9a8b481250 100644 --- a/api/src/default/types-metadata.json +++ b/api/src/default/types-metadata.json @@ -7,7 +7,7 @@ "input": "Option", "output": "Option" }, - "ProgramMetadataRepr": { + "ProgramMetadataReprRustV1": { "init": "TypesRepr", "handle": "TypesRepr", "reply": "Option", @@ -16,6 +16,15 @@ "state": "Option", "reg": "Vec" }, + "ProgramMetadataReprRustV2": { + "init": "TypesRepr", + "handle": "TypesRepr", + "reply": "Option", + "others": "TypesRepr", + "signal": "Option", + "state": "TypesRepr", + "reg": "Vec" + }, "StateMetadataRepr": { "functions": "BTreeMap", "reg": "Vec" diff --git a/api/src/metadata/metadata.ts b/api/src/metadata/metadata.ts index c516bb17bb..a77e912a76 100644 --- a/api/src/metadata/metadata.ts +++ b/api/src/metadata/metadata.ts @@ -87,7 +87,7 @@ export class GearMetadata { this.registry.register(types); } - createType(typeIndex: number, payload: unknown): Codec { + createType(typeIndex: number, payload: unknown): T { const type = this.regTypes.get(typeIndex); assert.notStrictEqual(type, undefined, `Type with index ${typeIndex} not found in registered types`); return this.registry.createType(type.name, payload); diff --git a/api/src/metadata/programMetadata.ts b/api/src/metadata/programMetadata.ts index 20303bb676..39b4de1996 100644 --- a/api/src/metadata/programMetadata.ts +++ b/api/src/metadata/programMetadata.ts @@ -1,45 +1,109 @@ import { u16, u8 } from '@polkadot/types'; -import { HexString } from '@polkadot/util/types'; import { hexToU8a } from '@polkadot/util'; -import { HumanProgramMetadataRepr, ProgramMetadataRepr } from '../types'; +import { + HumanProgramMetadataReprRustV1, + HumanProgramMetadataReprRustV2, + ProgramMetadataRepr, + ProgramMetadataReprRustV1, + ProgramMetadataReprRustV2, +} from '../types'; import { CreateType } from './create-type'; import { GearMetadata } from './metadata'; -enum Lang { +export enum Lang { RUST = 0, - ASSEMBLYSCRIPT, } -export class ProgramMetadata extends GearMetadata { - public types: Omit; +export enum MetadataVersion { + V1Rust = 1, + V2Rust = 2, +} - constructor({ reg, ...types }: HumanProgramMetadataRepr) { - super(reg); - this.types = types; - } +enum MetadataTypeName { + V1Rust = 'ProgramMetadataReprRustV1', + V2Rust = 'ProgramMetadataReprRustV2', } -export function getProgramMetadata(hexMetadata: HexString | string): ProgramMetadata { - if (!hexMetadata.startsWith('0x')) { - hexMetadata = '0x' + hexMetadata; +function getMetadataTypeName(version: number): string { + switch (version) { + case MetadataVersion.V1Rust: + return MetadataTypeName.V1Rust; + case MetadataVersion.V2Rust: + return MetadataTypeName.V2Rust; + default: + throw new Error('Metadata: Invalid metadata version'); } +} + +export class ProgramMetadata extends GearMetadata { + public types: Omit | Omit; + public lang: Lang; + public version: MetadataVersion; - const u8aMeta = hexToU8a(hexMetadata); + constructor(metadata: Uint8Array, lang: number, version: number) { + let metaRepr: ProgramMetadataRepr; + if (lang === Lang.RUST) { + try { + metaRepr = CreateType.create( + getMetadataTypeName(version) as string, + metadata, + ); + } catch (err) { + throw new Error('Metadata: Invalid metadata'); + } + } else { + throw new Error('Metadata: Unsupported lang'); + } - const lang = CreateType.create('u8', u8aMeta[0]).toNumber(); + const { reg, ...types } = + version === MetadataVersion.V1Rust + ? (metaRepr.toJSON() as HumanProgramMetadataReprRustV1) + : (metaRepr.toJSON() as HumanProgramMetadataReprRustV2); - // TODO: support different versions - // const version = CreateType.create('u16', u8aMeta.slice(1, 3)).toNumber(); + super(reg); - if (lang === Lang.RUST) { - try { - const metaRepr = CreateType.create('ProgramMetadataRepr', u8aMeta.slice(3)).toJSON(); - return new ProgramMetadata(metaRepr); - } catch (err) { - throw new Error('Metadata: Invalid metadata'); - } + this.version = version; + this.lang = lang; + this.types = types; } - // TODO: support assemblyscript metadata + /** + * ### Get `ProgramMetadata` instance from metadata in hex format + * Since we've started to support different versions of metadata generated when compilng a program written in Rust + * it may be necessary to check what the version is. This can be obtained from the `metadata.version` field of `ProgramMetadata` class. + * + * + * This will help to understand what types the metadata contains. For instance, metadata V1 has a `state` field + * thath describes the type of the output of the `state` function. However, in metadata V2, the `state` field includes 2 types: `input` and `output`. + * This change was made because starting from this version, the program expects input for the state function. + * @param hexMetadata metadata generated during program compilation + * + * @example + * import { ProgramMetada, MetadataVersion } from '@gear-js/api'; + * + * const metaHex = '0x...'; + * const meta = ProgramMetadata.from(metaHex); + * + * // State decoding + * const someBytes = '0x...'; + * + * if (meta.version === MetadataVersion.V1Rust) { + * const result = CreateType.create(meta.types.state, somBytes).toJSON(); + * } else { + * const result = CreateType.create(meta.types.state.output, someBytes).toJSON(); + * } + * + */ + static from(hexMetadata: string): ProgramMetadata { + if (!hexMetadata.startsWith('0x')) { + hexMetadata = '0x' + hexMetadata; + } + const u8aMeta = hexToU8a(hexMetadata); + + const lang = CreateType.create('u8', u8aMeta[0]).toNumber(); + const version = CreateType.create('u16', u8aMeta.slice(1, 3)).toNumber(); + + return new ProgramMetadata(u8aMeta.slice(3), lang, version); + } } diff --git a/api/src/types/interfaces/metadata.ts b/api/src/types/interfaces/metadata.ts index 64ebd466e9..c84b57ad84 100644 --- a/api/src/types/interfaces/metadata.ts +++ b/api/src/types/interfaces/metadata.ts @@ -13,17 +13,20 @@ export type HumanTypesRepr = { }; export interface ProgramMetadataRepr extends Struct { + reg: Vec; +} + +export interface ProgramMetadataReprRustV1 extends ProgramMetadataRepr { init: TypesRepr; handle: TypesRepr; others: TypesRepr; reply: Option; signal: Option; state: Option; - reg: Vec; - toJSON: () => HumanProgramMetadataRepr; + toJSON: () => HumanProgramMetadataReprRustV1; } -export type HumanProgramMetadataRepr = { +export type HumanProgramMetadataReprRustV1 = { init: HumanTypesRepr; handle: HumanTypesRepr; reply: number | null; @@ -33,6 +36,26 @@ export type HumanProgramMetadataRepr = { reg: HexString; }; +export interface ProgramMetadataReprRustV2 extends ProgramMetadataRepr { + init: TypesRepr; + handle: TypesRepr; + others: TypesRepr; + reply: Option; + signal: Option; + state: TypesRepr; + toJSON: () => HumanProgramMetadataReprRustV2; +} + +export type HumanProgramMetadataReprRustV2 = { + init: HumanTypesRepr; + handle: HumanTypesRepr; + reply: number | null; + others: HumanTypesRepr; + signal: number | null; + state: HumanTypesRepr; + reg: HexString; +}; + export interface StateMetadataRepr extends Struct { functions: BTreeMap; reg: Vec; diff --git a/api/src/types/interfaces/program/index.ts b/api/src/types/interfaces/program/index.ts index 34c3d42d41..ce101a70de 100644 --- a/api/src/types/interfaces/program/index.ts +++ b/api/src/types/interfaces/program/index.ts @@ -2,3 +2,4 @@ export * from './pages'; export * from './code'; export * from './extrinsic'; export * from './storage'; +export * from './state'; diff --git a/api/src/types/interfaces/program/state.ts b/api/src/types/interfaces/program/state.ts new file mode 100644 index 0000000000..045bb381d3 --- /dev/null +++ b/api/src/types/interfaces/program/state.ts @@ -0,0 +1,43 @@ +import { HexString } from '@polkadot/util/types'; + +export interface ReadStateParams { + /** + * Program Id + */ + programId: HexString; + /** + * Input payload expected by the `state` function + */ + payload: any; + /** + * Block hash at which state is to be received + */ + at?: HexString; +} + +export interface ReadStateUsingWasmParams { + /** + * Program Id + */ + programId: HexString; + /** + * Input payload expected by the `state` function of the onchain program + */ + payload?: any; + /** + * Function name to execute + */ + fn_name: string; + /** + * Compiled program using to read `state` of the onchain program + */ + wasm: Buffer | Uint8Array | HexString; + /** + * (Optional) The argument expected by the program using to read state + */ + argument?: any; + /** + * (Optional) Block hash at which state is to be read + */ + at?: HexString; +} diff --git a/api/src/utils/create-payload.ts b/api/src/utils/create-payload.ts index f023a1255c..ff342a5a1d 100644 --- a/api/src/utils/create-payload.ts +++ b/api/src/utils/create-payload.ts @@ -4,7 +4,6 @@ import { HexString } from '@polkadot/util/types'; import { GearMetadata, ProgramMetadata, isProgramMeta } from '../metadata'; import { CreateType } from '../metadata'; -import { HumanProgramMetadataRepr } from '../types'; export function getRegistry(metaOrHexRegistry: HexString): HexString { if (!metaOrHexRegistry) { @@ -19,7 +18,7 @@ export function getRegistry(metaOrHexRegistry: HexString): HexString { export function encodePayload( payload: unknown, hexRegistryOrMeta: HexString | ProgramMetadata, - type: keyof Omit, + type: string, typeIndexOrPayloadType?: number | string, ): Array { if (payload === undefined) { diff --git a/api/test/Code.test.ts b/api/test/Code.test.ts index 9904d34cd7..748ad7a72a 100644 --- a/api/test/Code.test.ts +++ b/api/test/Code.test.ts @@ -2,13 +2,13 @@ import { HexString } from '@polkadot/util/types'; import { join } from 'path'; import { readFileSync } from 'fs'; -import { GEAR_EXAMPLES_WASM_DIR, WS_ADDRESS } from './config'; +import { TARGET, WS_ADDRESS } from './config'; import { getAccount, sendTransaction, sleep } from './utilsFunctions'; import { GearApi } from '../src'; const api = new GearApi({ providerAddress: WS_ADDRESS }); const accounts = {}; -const code = readFileSync(join(GEAR_EXAMPLES_WASM_DIR, 'demo_sum.opt.wasm')); +const code = readFileSync(join(TARGET, 'test_waitlist.opt.wasm')); let codeId: HexString; beforeAll(async () => { diff --git a/api/test/Gas.test.ts b/api/test/Gas.test.ts index 0e0d5ea58a..2167684ef8 100644 --- a/api/test/Gas.test.ts +++ b/api/test/Gas.test.ts @@ -4,7 +4,7 @@ import { join } from 'path'; import { readFileSync } from 'fs'; import { u64 } from '@polkadot/types-codec'; -import { GearApi, getProgramMetadata } from '../src'; +import { GearApi, ProgramMetadata } from '../src'; import { TARGET, TEST_GAS_META, WS_ADDRESS } from './config'; import { checkInit, getAccount, listenToUserMessageSent, sendTransaction, sleep } from './utilsFunctions'; import { GasInfo } from '../src/types'; @@ -18,7 +18,7 @@ let codeId: HexString; let messageId: HexString; const code = readFileSync(join(TARGET, 'test_gas.opt.wasm')); -const meta = getProgramMetadata(`0x${readFileSync(TEST_GAS_META, 'utf-8')}`); +const meta = ProgramMetadata.from(`0x${readFileSync(TEST_GAS_META, 'utf-8')}`); const gasLimits: { init?: u64; handle?: u64; reply?: u64 } = { init: undefined, diff --git a/api/test/Message.test.ts b/api/test/Message.test.ts index 13011433db..fe57d59ca6 100644 --- a/api/test/Message.test.ts +++ b/api/test/Message.test.ts @@ -3,7 +3,7 @@ import { KeyringPair } from '@polkadot/keyring/types'; import { join } from 'path'; import { readFileSync } from 'fs'; -import { GearApi, getProgramMetadata } from '../src'; +import { GearApi, ProgramMetadata } from '../src'; import { TARGET, TEST_META_META, WS_ADDRESS } from './config'; import { checkInit, getAccount, sendTransaction, sleep } from './utilsFunctions'; import { decodeAddress } from '../src/utils'; @@ -15,7 +15,7 @@ let messageToClaim: HexString; const code = readFileSync(join(TARGET, 'test_meta.opt.wasm')); const metaHex: HexString = `0x${readFileSync(TEST_META_META, 'utf-8')}`; -const metadata = getProgramMetadata(metaHex); +const metadata = ProgramMetadata.from(metaHex); beforeAll(async () => { await api.isReadyOrError; @@ -60,7 +60,7 @@ describe('Gear Message', () => { { destination: programId, payload: message.payload, - gasLimit: 2_000_000_000, + gasLimit: 20_000_000_000, value: message.value, }, metadata, diff --git a/api/test/Meta.test.ts b/api/test/Meta.test.ts index 648a0adbaf..3ba46ea6ec 100644 --- a/api/test/Meta.test.ts +++ b/api/test/Meta.test.ts @@ -1,13 +1,13 @@ import { TEST_META_META } from './config'; import fs from 'fs'; -import { ProgramMetadata, getProgramMetadata } from '../src'; +import { ProgramMetadata } from '../src'; let meta: ProgramMetadata; beforeAll(() => { const hex = fs.readFileSync(TEST_META_META, 'utf-8'); - meta = getProgramMetadata(`0x${hex}`); + meta = ProgramMetadata.from(`0x${hex}`); }); describe('Get type definitions', () => { @@ -18,7 +18,7 @@ describe('Get type definitions', () => { reply: 4, others: { input: null, output: null }, signal: 26, - state: 27, + state: { input: 27, output: 29 }, }); }); @@ -492,114 +492,6 @@ describe('Get type definitions', () => { expect(meta.getTypeDef(26)).toEqual('H256'); expect(meta.getTypeDef(26, true)).toEqual({ kind: 'primitive', name: 'H256', type: 'H256' }); }); - - test('Get type structure 27', () => { - expect(meta.getTypeDef(27)).toEqual([ - { - id: { - decimal: 'U128', - hex: ['U8'], - }, - person: { - surname: 'Str', - name: 'Str', - }, - }, - ]); - expect(meta.getTypeDef(27, true)).toEqual({ - name: 'Vec', - kind: 'sequence', - type: { - name: 'Wallet', - kind: 'composite', - type: { - id: { - name: 'Id', - kind: 'composite', - type: { - decimal: { name: 'U128', kind: 'primitive', type: 'U128' }, - hex: { name: 'Vec', kind: 'sequence', type: { name: 'U8', kind: 'primitive', type: 'U8' } }, - }, - }, - person: { - name: 'Person', - kind: 'composite', - type: { - surname: { name: 'Str', kind: 'primitive', type: 'Str' }, - name: { name: 'Str', kind: 'primitive', type: 'Str' }, - }, - }, - }, - }, - }); - }); - - test('Get type structure 28', () => { - expect(meta.getTypeDef(28)).toEqual({ - id: { - decimal: 'U128', - hex: ['U8'], - }, - person: { - surname: 'Str', - name: 'Str', - }, - }); - expect(meta.getTypeDef(28, true)).toEqual({ - name: 'Wallet', - kind: 'composite', - type: { - id: { - name: 'Id', - kind: 'composite', - type: { - decimal: { name: 'U128', kind: 'primitive', type: 'U128' }, - hex: { name: 'Vec', kind: 'sequence', type: { name: 'U8', kind: 'primitive', type: 'U8' } }, - }, - }, - person: { - name: 'Person', - kind: 'composite', - type: { - surname: { name: 'Str', kind: 'primitive', type: 'Str' }, - name: { name: 'Str', kind: 'primitive', type: 'Str' }, - }, - }, - }, - }); - }); - - test('Get type structure 29', () => { - expect(meta.getTypeDef(29)).toEqual({ - decimal: 'U128', - hex: ['U8'], - }); - expect(meta.getTypeName(29)).toEqual('Id'); - expect(meta.getTypeIndexByName('TestMetaIoId')).toEqual(29); - expect(meta.getTypeDef(29, true)).toEqual({ - name: 'Id', - kind: 'composite', - type: { - decimal: { name: 'U128', kind: 'primitive', type: 'U128' }, - hex: { name: 'Vec', kind: 'sequence', type: { name: 'U8', kind: 'primitive', type: 'U8' } }, - }, - }); - }); - - test('Get type structure 30', () => { - expect(meta.getTypeDef(30)).toEqual({ - surname: 'Str', - name: 'Str', - }); - expect(meta.getTypeDef(30, true)).toEqual({ - name: 'Person', - kind: 'composite', - type: { - surname: { name: 'Str', kind: 'primitive', type: 'Str' }, - name: { name: 'Str', kind: 'primitive', type: 'Str' }, - }, - }); - }); }); const payload = @@ -611,7 +503,7 @@ const metaHex = describe.skip('Decode complicated type', () => { test('Check that there is no Lookup types in type defenitions', () => { - meta = getProgramMetadata(metaHex); + meta = ProgramMetadata.from(metaHex); let isLookupTypeFound = false; @@ -625,7 +517,7 @@ describe.skip('Decode complicated type', () => { }); test('Decode payload', () => { - const decoded = meta.createType(meta.types.state!, payload); + const decoded = meta.createType(meta.types.state as number, payload); const json = decoded.toJSON() as any; expect(json).toHaveProperty('config'); @@ -648,7 +540,7 @@ const activitiesMetaHex = describe('Create Option type', () => { test('test', () => { - const meta = getProgramMetadata(activitiesMetaHex); + const meta = ProgramMetadata.from(activitiesMetaHex); const type = meta.getTypeDef(meta.types.handle.input!, true); diff --git a/api/test/Program.test.ts b/api/test/Program.test.ts index 6fa2428e3c..8fdc4a2df7 100644 --- a/api/test/Program.test.ts +++ b/api/test/Program.test.ts @@ -5,7 +5,7 @@ import { bufferToU8a } from '@polkadot/util'; import { join } from 'path'; import { readFileSync } from 'fs'; -import { GearApi, decodeAddress, getProgramMetadata } from '../src'; +import { GearApi, decodeAddress, ProgramMetadata } from '../src'; import { TARGET, TEST_META_META, WS_ADDRESS } from './config'; import { checkInit, getAccount, sendTransaction, sleep, waitForPausedProgram } from './utilsFunctions'; @@ -33,7 +33,7 @@ afterAll(async () => { describe('New Program', () => { test('Upload program', async () => { - const metadata = getProgramMetadata(metaHex); + const metadata = ProgramMetadata.from(metaHex); const program = api.program.upload( { @@ -92,7 +92,7 @@ describe('New Program', () => { test('Сreate program', async () => { expect(codeId).toBeDefined(); - const metadata = getProgramMetadata(metaHex); + const metadata = ProgramMetadata.from(metaHex); const { programId, salt } = api.program.create( { diff --git a/api/test/State.test.ts b/api/test/State.test.ts index 1c889c37f2..d9f541c9e7 100644 --- a/api/test/State.test.ts +++ b/api/test/State.test.ts @@ -3,7 +3,7 @@ import { KeyringPair } from '@polkadot/keyring/types'; import { join } from 'path'; import { readFileSync } from 'fs'; -import { CreateType, GearApi, StateMetadata, getProgramMetadata, getStateMetadata } from '../src'; +import { CreateType, GearApi, ProgramMetadata, StateMetadata, getStateMetadata } from '../src'; import { TARGET, TEST_META_META, WS_ADDRESS } from './config'; import { checkInit, getAccount, sleep } from './utilsFunctions'; @@ -21,7 +21,7 @@ let stateV2Meta: StateMetadata; const metaHex = `0x${readFileSync(TEST_META_META, 'utf-8')}` as HexString; -const meta = getProgramMetadata(metaHex); +const meta = ProgramMetadata.from(metaHex); let programId: HexString; @@ -56,12 +56,8 @@ describe('Read State', () => { test('Get program state', async () => { expect(programId).toBeDefined(); - const state = await api.programState.read({ programId }, meta); + const state = await api.programState.read({ programId, payload: 1 }, meta); expect([ - { - id: { decimal: 0, hex: '0x00' }, - person: { surname: 'Surname0', name: 'Name0' }, - }, { id: { decimal: 1, hex: '0x01' }, person: { surname: 'Surname1', name: 'Name1' }, @@ -81,8 +77,9 @@ describe('Read State', () => { test('Read state v1 all_wallets', async () => { expect(programId).toBeDefined(); const state = await api.programState.readUsingWasm( - { programId, fn_name: 'all_wallets', wasm: stateV1 }, + { programId, payload: null, fn_name: 'all_wallets', wasm: stateV1 }, stateV1Meta, + meta, ); expect(state.toJSON()).toMatchObject([ @@ -100,8 +97,9 @@ describe('Read State', () => { test('Read state v1 first_wallet', async () => { expect(programId).toBeDefined(); const state = await api.programState.readUsingWasm( - { programId, fn_name: 'first_wallet', wasm: Uint8Array.from(stateV1) }, + { programId, payload: null, fn_name: 'first_wallet', wasm: Uint8Array.from(stateV1) }, stateV1Meta, + meta, ); expect(state.toJSON()).toMatchObject({ @@ -114,8 +112,9 @@ describe('Read State', () => { expect(programId).toBeDefined(); const wasmAsHex = CreateType.create('Bytes', Array.from(stateV1)).toHex(); const state = await api.programState.readUsingWasm( - { programId, fn_name: 'last_wallet', wasm: wasmAsHex }, + { programId, payload: null, fn_name: 'last_wallet', wasm: wasmAsHex }, stateV1Meta, + meta, ); expect(state.toJSON()).toMatchObject({ @@ -135,8 +134,9 @@ describe('Read State', () => { test('Read state v2 wallet_by_id', async () => { expect(programId).toBeDefined(); const state = await api.programState.readUsingWasm( - { programId, fn_name: 'wallet_by_id', wasm: stateV2, argument: { decimal: 1, hex: '0x01' } }, + { programId, payload: null, fn_name: 'wallet_by_id', wasm: stateV2, argument: { decimal: 1, hex: '0x01' } }, stateV2Meta, + meta, ); expect(state.toJSON()).toMatchObject({ @@ -148,8 +148,15 @@ describe('Read State', () => { test('Read state v2 wallet_by_person', async () => { expect(programId).toBeDefined(); const state = await api.programState.readUsingWasm( - { programId, fn_name: 'wallet_by_person', wasm: stateV2, argument: { surname: 'Surname0', name: 'Name0' } }, + { + programId, + payload: null, + fn_name: 'wallet_by_person', + wasm: stateV2, + argument: { surname: 'Surname0', name: 'Name0' }, + }, stateV2Meta, + meta, ); expect(state.toJSON()).toMatchObject({ @@ -161,8 +168,9 @@ describe('Read State', () => { test('Read state v2 wallet_by_u128', async () => { expect(programId).toBeDefined(); const state = await api.programState.readUsingWasm( - { programId, fn_name: 'wallet_by_u128', wasm: stateV2, argument: 1 }, + { programId, payload: null, fn_name: 'wallet_by_u128', wasm: stateV2, argument: 1 }, stateV2Meta, + meta, ); expect(state.toJSON()).toMatchObject({ diff --git a/api/test/Voucher.test.ts b/api/test/Voucher.test.ts index 1dada307d0..6f5c7b8725 100644 --- a/api/test/Voucher.test.ts +++ b/api/test/Voucher.test.ts @@ -3,7 +3,7 @@ import { KeyringPair } from '@polkadot/keyring/types'; import { join } from 'path'; import { readFileSync } from 'fs'; -import { GearApi, decodeAddress, getProgramMetadata } from '../src'; +import { GearApi, ProgramMetadata, decodeAddress } from '../src'; import { TARGET, TEST_META_META, WS_ADDRESS } from './config'; import { checkInit, getAccount, sendTransaction, sleep } from './utilsFunctions'; @@ -16,7 +16,7 @@ let msgId: HexString; const api = new GearApi({ providerAddress: WS_ADDRESS }); const code = readFileSync(join(TARGET, 'test_meta.opt.wasm')); const metaHex: HexString = `0x${readFileSync(TEST_META_META, 'utf-8')}`; -const metadata = getProgramMetadata(metaHex); +const metadata = ProgramMetadata.from(metaHex); beforeAll(async () => { await api.isReadyOrError; diff --git a/api/test/config.ts b/api/test/config.ts index dc01a36d79..3b9ff21730 100644 --- a/api/test/config.ts +++ b/api/test/config.ts @@ -1,5 +1,5 @@ export const TEST_WASM_DIR = 'test/wasm'; -export const TARGET = 'programs/target/wasm32-unknown-unknown/release'; +export const TARGET = 'programs/target/wasm32-unknown-unknown/debug'; export const GEAR_EXAMPLES_WASM_DIR = 'test/wasm/examples'; export const PROGRAMS_DIR = 'programs'; export const TEST_META_META = `${TARGET}/test_meta.meta.txt`; diff --git a/api/test/testSequencer.js b/api/test/testSequencer.js index 9b0fedda2d..bf97c87c15 100644 --- a/api/test/testSequencer.js +++ b/api/test/testSequencer.js @@ -5,21 +5,27 @@ export default class CustomSequencer extends Sequencer.default.default { const copyTests = Array.from(tests); const result = new Array(copyTests.length); + const apiTest = tests.find(({ path }) => path.includes('GearApi')); + const codeTest = tests.find(({ path }) => path.includes('Code')); + let counter = 0; + if (apiTest && tests.length > 1) counter++; + if (codeTest && tests.length > 1) counter++; + for (const test of copyTests) { - if (test.path.includes('DebugMode')) { - result[result.length - 1] = test; - continue; - } else if (test.path.includes('GearApi')) { + if (test.path.includes('GearApi') && tests.length > 1) { result[0] = test; - counter++; + continue; + } else if (test.path.includes('Code') && tests.length > 1) { + result[1] = test; continue; } else { result[counter] = test; counter++; } } + return result; } }