From 4d1fdef94d356e7c41a71afbb64149be383fc60d Mon Sep 17 00:00:00 2001 From: Tomasz Kulik Date: Fri, 12 Jul 2024 10:35:10 +0200 Subject: [PATCH] chore: Update documentation and refactoring (#393) --- .github/workflows/ci.yml | 2 +- README.md | 39 ++++++++++ examples/Cargo.lock | 80 --------------------- examples/Cargo.toml | 1 - examples/contracts/cw1-subkeys/Cargo.toml | 1 - examples/contracts/cw1-whitelist/Cargo.toml | 1 - examples/contracts/cw20-base/Cargo.toml | 1 - sylvia-derive/src/lib.rs | 28 ++++++++ sylvia/src/types.rs | 2 + 9 files changed, 70 insertions(+), 85 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fb69144a..91c9c189 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,7 +51,7 @@ jobs: - name: Install Rust uses: actions-rs/toolchain@v1 with: - toolchain: 1.73.0 + toolchain: 1.74.0 target: wasm32-unknown-unknown profile: minimal override: true diff --git a/README.md b/README.md index 856d9e21..ef9d0cb1 100644 --- a/README.md +++ b/README.md @@ -273,9 +273,15 @@ impl CustomQuery for MyMsg {} #[sv::messages(interface as Interface)] #[sv::messages(interface as InterfaceWithCustomType: custom(msg, query))] #[sv::custom(msg=MyMsg, query=MyQuery)] +#[sv::msg_attr(exec, PartialOrd)] #[sv::override_entry_point(sudo=crate::entry_points::sudo(crate::SudoMsg))] impl MyContract { // ... + #[sv::msg(query)] + #[sv::attr(serde(rename(serialize = "CustomQueryMsg")))] + fn query_msg(&self, _ctx: QueryCtx) -> StdResult { + // ... + } } ``` @@ -292,6 +298,10 @@ impl MyContract { * `sv::custom` allows to define CustomMsg and CustomQuery for the contract. By default generated code will return `Response` and will use `Deps` and `DepsMut`. + * `sv::msg_attr` forwards any attribute to the message's type. + + * `sv::attr` forwards any attribute to the enum's variant. + ## Usage in external crates @@ -378,6 +388,35 @@ pub fn evaluate_member(&self, ctx: ExecCtx, ...) -> StdResult { } ``` + +## Executor message builder + +Sylvia defines the +[`ExecutorBuilder`](https://docs.rs/sylvia/latest/sylvia/types/struct.ExecutorBuilder.html) +type, which can be accessed through +[`Remote::executor`](https://docs.rs/sylvia/latest/sylvia/types/struct.Remote.html#method.executor). +It's generic over the contract type and exposes execute methods from the +contract and every interface implemented on it through an auto-generated `Executor` traits. +Execute messages of other contracts can be built with `Remote` as well by +calling `executor` method. It returns a message builder that implements +auto-generated `Executor` traits of all Sylvia contracts. +Methods defined in the `Executor` traits constructs an execute message, +which variant corresponds to the method name. +The message is then wrapped in the `WasmMsg`, and returned once +[`ExecutorBuilder::build()`](https://docs.rs/sylvia/latest/sylvia/types/struct.ExecutorBuilder.html#method.build) +method is called. + +```rust +use sylvia::types::Remote; +use other_contract::contract::OtherContract; +use other_contract::contract::sv::Executor; + +let some_exec_msg: WasmMsg = Remote::::new(remote_addr) + .executor() + .some_exec_method()? + .build(); +``` + ## Using unsupported entry points If there's a need for an entry point that is not implemented in Sylvia, you can implement diff --git a/examples/Cargo.lock b/examples/Cargo.lock index 456b197b..599d73f4 100644 --- a/examples/Cargo.lock +++ b/examples/Cargo.lock @@ -79,12 +79,6 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56953345e39537a3e18bdaeba4cb0c58a78c1f61f361dc0fa7c5c7340ae87c5f" -[[package]] -name = "bumpalo" -version = "3.15.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa" - [[package]] name = "byteorder" version = "1.5.0" @@ -331,7 +325,6 @@ dependencies = [ "cw1", "cw1-whitelist", "cw2", - "getrandom", "serde", "sylvia", "thiserror", @@ -350,7 +343,6 @@ dependencies = [ "cw-storage-plus", "cw1", "cw2", - "getrandom", "serde", "sylvia", "thiserror", @@ -400,7 +392,6 @@ dependencies = [ "cw20-allowances", "cw20-marketing", "cw20-minting", - "getrandom", "semver", "serde", "sylvia", @@ -663,10 +654,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ "cfg-if", - "js-sys", "libc", "wasi", - "wasm-bindgen", ] [[package]] @@ -744,15 +733,6 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" -[[package]] -name = "js-sys" -version = "0.3.69" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" -dependencies = [ - "wasm-bindgen", -] - [[package]] name = "k256" version = "0.13.3" @@ -800,12 +780,6 @@ version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" -[[package]] -name = "log" -version = "0.4.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" - [[package]] name = "memchr" version = "2.7.1" @@ -1233,60 +1207,6 @@ 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.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" -dependencies = [ - "cfg-if", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" -dependencies = [ - "bumpalo", - "log", - "once_cell", - "proc-macro2", - "quote", - "syn 2.0.65", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.65", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" - [[package]] name = "whitelist" version = "0.5.0" diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 22138576..48513423 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -34,7 +34,6 @@ cw-multi-test = "2.1.0" cw-storage-plus = "2.0.0" cw-utils = "2.0.0" cw2 = "2.0.0" -getrandom = "0.2.15" semver = "1.0.23" serde = { version = "1.0.203", default-features = false, features = ["derive"] } thiserror = "1.0.61" diff --git a/examples/contracts/cw1-subkeys/Cargo.toml b/examples/contracts/cw1-subkeys/Cargo.toml index 47725735..218534b9 100644 --- a/examples/contracts/cw1-subkeys/Cargo.toml +++ b/examples/contracts/cw1-subkeys/Cargo.toml @@ -21,7 +21,6 @@ cw1 = { path = "../../interfaces/cw1" } whitelist = { path = "../../interfaces/whitelist" } cw1-whitelist = { path = "../cw1-whitelist", features = ["library"] } cw2 = { workspace = true } -getrandom = { workspace = true, features = ["js"] } serde = { workspace = true } sylvia = { path = "../../../sylvia" } thiserror = { workspace = true } diff --git a/examples/contracts/cw1-whitelist/Cargo.toml b/examples/contracts/cw1-whitelist/Cargo.toml index 474d63d8..edafd667 100644 --- a/examples/contracts/cw1-whitelist/Cargo.toml +++ b/examples/contracts/cw1-whitelist/Cargo.toml @@ -27,7 +27,6 @@ thiserror = { workspace = true } cw2 = { workspace = true } cw-multi-test = { workspace = true, optional = true } anyhow = { workspace = true, optional = true } -getrandom = { workspace = true, features = ["js"] } [dev-dependencies] anyhow = { workspace = true } diff --git a/examples/contracts/cw20-base/Cargo.toml b/examples/contracts/cw20-base/Cargo.toml index 45cd6084..0dd0dc26 100644 --- a/examples/contracts/cw20-base/Cargo.toml +++ b/examples/contracts/cw20-base/Cargo.toml @@ -26,7 +26,6 @@ cw2 = { workspace = true } cw20-allowances = { path = "../../interfaces/cw20-allowances" } cw20-marketing = { path = "../../interfaces/cw20-marketing" } cw20-minting = { path = "../../interfaces/cw20-minting" } -getrandom = { workspace = true, features = ["js"] } semver = { workspace = true } serde = { workspace = true } sylvia = { path = "../../../sylvia" } diff --git a/sylvia-derive/src/lib.rs b/sylvia-derive/src/lib.rs index 08f613fd..53de87bb 100644 --- a/sylvia-derive/src/lib.rs +++ b/sylvia-derive/src/lib.rs @@ -227,6 +227,20 @@ pub(crate) fn crate_module() -> Path { /// # fn main() {} /// ``` /// +/// ### `sv::msg_attr(msg_type, {...})` +/// +/// This attribute can be used for the whole `trait Interface {}` block and +/// for the following message types: `exec`, `query` and `sudo`. +/// The `{...}` part will be forwarded as an attribute `#[{...}]` to the +/// given message type (enum or struct). +/// +/// ### `sv::attr({...})` +/// +/// Forwards variant's attribute to the specific enum's field in the +/// generated message type. It can be used along with `sv::msg(...)` +/// and only for message types variants that resolves in an enum field, +/// i.e. `exec`, `query` and `sudo`. +/// #[proc_macro_error] #[proc_macro_attribute] pub fn interface(attr: TokenStream, item: TokenStream) -> TokenStream { @@ -600,6 +614,20 @@ fn interface_impl(_attr: TokenStream2, item: TokenStream2) -> TokenStream2 { /// # fn main() {} /// ``` /// +/// ### `sv::msg_attr(msg_type, {...})` +/// +/// This attribute can be used for the whole `impl Contract {}` block and +/// for every message type: `exec`, `query`, `sudo`, `instantiate`, +/// `migrate` and `reply`. The `{...}` part will be forwarded as an +/// attribute `#[{...}]` to the given message type (enum or struct). +/// +/// ### `sv::attr({...})` +/// +/// Forwards variant's attribute to the specific enum's field in the +/// generated message type. It can be used along with `sv::msg(...)` +/// and only for message types variants that resolves in an enum field, +/// i.e. `exec`, `query` and `sudo`. +/// /// #[proc_macro_error] #[proc_macro_attribute] diff --git a/sylvia/src/types.rs b/sylvia/src/types.rs index 4b2d6862..45cc70ca 100644 --- a/sylvia/src/types.rs +++ b/sylvia/src/types.rs @@ -278,7 +278,9 @@ impl ExecutorBuilder<(EmptyExecutorBuilderState, Contract)> { _state: std::marker::PhantomData, } } +} +impl ExecutorBuilder<(T, Contract)> { /// Adds the funds to the execution message. pub fn with_funds(self, funds: Vec) -> Self { Self { funds, ..self }