From aa93431102938dddc852625482355f7f25de8402 Mon Sep 17 00:00:00 2001 From: Tomasz Kulik Date: Wed, 26 Jun 2024 13:17:04 +0200 Subject: [PATCH] chore: Update docs after review ; Resolve one TODO in the code related to Executors --- sylvia/src/types.rs | 54 ++++++++++++++++++++++++++++++++---------- sylvia/tests/remote.rs | 35 +++++++++------------------ 2 files changed, 53 insertions(+), 36 deletions(-) diff --git a/sylvia/src/types.rs b/sylvia/src/types.rs index d5a8f078..35125773 100644 --- a/sylvia/src/types.rs +++ b/sylvia/src/types.rs @@ -168,16 +168,16 @@ pub struct ReadyExecutorBuilderState; /// should be created using a [Remote] object, obtained through the /// [Remote::executor] method. /// -/// ExecutorEmptyBuilder implements the -/// Executor traits generated by the contract and interface macros, -/// encompassing all sv::msg(exec) methods of the specified contracts +/// [ExecutorBuilder] implements the +/// Executor traits generated by the `contract` and `interface` macros, +/// encompassing all `sv::msg(exec)` methods of the specified contracts /// and interfaces. /// /// ```rust /// pub mod another_contract { -/// use cosmwasm_std::{Response, StdResult}; -/// use sylvia::contract; -/// use sylvia::types::{ExecCtx, InstantiateCtx}; +/// # use cosmwasm_std::{Response, StdResult}; +/// # use sylvia::contract; +/// # use sylvia::types::{ExecCtx, InstantiateCtx}; /// /// pub struct AnotherContract {} /// @@ -197,12 +197,12 @@ pub struct ReadyExecutorBuilderState; /// } /// } /// -/// use cosmwasm_std::{coin, Addr, Response, StdResult}; -/// use cw_storage_plus::Item; -/// use sylvia::contract; -/// use sylvia::types::{ExecCtx, InstantiateCtx, Remote}; -/// use another_contract::AnotherContract; -/// use another_contract::sv::Executor; +/// # use cosmwasm_std::{coin, Addr, Response, StdResult}; +/// # use cw_storage_plus::Item; +/// # use sylvia::contract; +/// # use sylvia::types::{ExecCtx, InstantiateCtx, Remote}; +/// # use another_contract::AnotherContract; +/// # use another_contract::sv::Executor; /// /// pub struct Contract<'a> { /// pub remote_contract: Item>, @@ -235,6 +235,36 @@ pub struct ReadyExecutorBuilderState; /// /// # fn main() {} /// ``` +/// +/// It is also possible to call execution methods on other contract's interface: +/// +/// ```rust +/// pub mod interface { +/// # use cosmwasm_std::{Response, StdResult, StdError}; +/// # use sylvia::interface; +/// # use sylvia::types::ExecCtx; +/// +/// #[interface] +/// pub trait Interface { +/// type Error: From; +/// +/// #[sv::msg(exec)] +/// fn exec_method(&self, ctx: ExecCtx) -> StdResult; +/// } +/// } +/// +/// # use interface::Interface; +/// # use interface::sv::Executor; +/// # use cosmwasm_std::{Addr, Response, StdError, StdResult}; +/// # use sylvia::types::Remote; +/// +/// fn execute_method(remote_addr: Addr) -> StdResult { +/// let remote = Remote::<'_, dyn Interface>::new(remote_addr); +/// let msg = remote.executor().exec_method()?.build(); +/// Ok(Response::new().add_message(msg)) +/// } +/// ``` +/// pub struct ExecutorBuilder { contract: String, funds: Vec, diff --git a/sylvia/tests/remote.rs b/sylvia/tests/remote.rs index a4c194d8..96b94d4d 100644 --- a/sylvia/tests/remote.rs +++ b/sylvia/tests/remote.rs @@ -188,15 +188,15 @@ where } pub mod manager { - use cosmwasm_std::{to_json_binary, Addr, Response, StdError, StdResult, WasmMsg}; + use cosmwasm_std::{Addr, Response, StdError, StdResult}; use cw_storage_plus::Item; use schemars::JsonSchema; use serde::de::DeserializeOwned; use serde::Serialize; use sylvia::contract; - use sylvia::types::{ExecCtx, InstantiateCtx, InterfaceApi, QueryCtx}; + use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx}; - use crate::counter::sv::{Api as CounterApi, Querier}; + use crate::counter::sv::{Executor, Querier}; use crate::{ExampleMsg, ExampleQuery, InterfaceStorage}; pub struct ManagerContract<'a, CounterT> { @@ -236,27 +236,14 @@ pub mod manager { ctx: ExecCtx, value: CounterT, ) -> Result, StdError> { - // This should be simplified to something like - // ```rust - // let msg = self - // .remote_counter - // .load(ctx.deps.storage)? - // .add(value) - // .with_funds(&[]) - // .build(); - // ``` - // after https://github.com/CosmWasm/sylvia/issues/130 - let msg = as InterfaceApi>::Exec::add(value); - let wasm = WasmMsg::Execute { - contract_addr: self - .remote_counter - .load(ctx.deps.storage)? - .interface_remote - .as_ref() - .to_string(), - msg: to_json_binary(&msg)?, - funds: vec![], - }; + let wasm = self + .remote_counter + .load(ctx.deps.storage)? + .interface_remote + .executor() + .with_funds(vec![]) + .add(value)? + .build(); let resp = Response::new().add_message(wasm); Ok(resp) }