abstract_app/
interface.rs

1#[macro_export]
2/// Creates the interface for working with the app with [cw-orch](https://github.com/AbstractSDK/cw-orchestrator).
3/// This generates all the necessary code used to interact with the app in an cw-orch environment
4///
5/// ## Usage
6/// The macro takes three arguments:
7/// 1. The app's constant, declared in `contract.rs`.
8/// 2. The app's type, declared in `contract.rs`.
9/// 3. The name of the interface struct to be generated.
10/// ```rust,ignore
11/// cw_orch_interface!(APP, App, AppInterface);
12/// ```
13///
14/// This will generate :
15/// ```rust,ignore
16/// pub mod interface{
17///     #[cw_orch::interface(App::InstantiateMsg, App::ExecuteMsg, App::QueryMsg, App::MigrateMsg)]
18///     pub struct AppInterface;
19///
20///     impl <Chain: cw_orch::prelude::CwEnv> cw_orch::prelude::Uploadable for AppInterface<Chain> {
21///            // Looks for the wasm file in the app's artifacts directory
22///            // The name of the wasm file should contain the app crate name (snake_cased)
23///         fn wasm(&self) -> cw_orch::prelude::WasmPath {
24///             let wasm_name = env!("CARGO_CRATE_NAME").replace('-', "_");
25///             cw_orch::prelude::ArtifactsDir::auto(Some(env!("CARGO_MANIFEST_DIR").to_string()))
26///                 .find_wasm_path(&wasm_name).unwrap()
27///         }
28///
29///         fn wrapper(
30///             &self,
31///         ) -> Box<dyn cw_orch::prelude::MockContract<cosmwasm_std::Empty, cosmwasm_std::Empty>> {
32///             Box::new(
33///                 cw_orch::prelude::ContractWrapper::new_with_empty(
34///                     APP::execute, // This notation, doesn't actually work like so, but we use that to illustrate
35///                     APP::instantiate,
36///                     APP::query,
37///                 )
38///                 .with_reply(APP::reply)
39///                 .with_migrate(APP::migrate)
40///                 .with_sudo(APP::sudo),
41///             )
42///         }
43///     }
44///     impl<Chain: ::cw_orch::prelude::CwEnv> $crate::abstract_app::abstract_interface::AppDeployer<Chain> for AppInterface<Chain> {}
45/// }
46/// ```
47macro_rules! cw_orch_interface {
48    ($app_const:expr, $app_type:ty, $interface_name: ident) => {
49        #[cfg(not(target_arch = "wasm32"))]
50        mod _wrapper_fns {
51            use super::*;
52            $crate::__wrapper_fns_without_custom__!($app_const, $app_type);
53
54            pub fn execute(
55                deps: ::cosmwasm_std::DepsMut,
56                env: ::cosmwasm_std::Env,
57                info: ::cosmwasm_std::MessageInfo,
58                msg: <$app_type as $crate::sdk::base::ExecuteEndpoint>::ExecuteMsg,
59            ) -> Result<::cosmwasm_std::Response, <$app_type as $crate::sdk::base::Handler>::Error>
60            {
61                use $crate::sdk::base::ExecuteEndpoint;
62                $app_const.execute(deps, env, info, msg)
63            }
64        }
65
66        pub mod interface {
67            use super::*;
68
69            #[::cw_orch::interface(
70                _wrapper_fns::InstantiateMsg,
71                _wrapper_fns::ExecuteMsg,
72                _wrapper_fns::QueryMsg,
73                _wrapper_fns::MigrateMsg
74            )]
75            pub struct $interface_name;
76
77            $crate::__cw_orch_interfaces__!($interface_name);
78
79            #[cfg(not(target_arch = "wasm32"))]
80            impl<Chain: ::cw_orch::prelude::CwEnv> $crate::abstract_interface::RegisteredModule
81                for $interface_name<Chain>
82            {
83                type InitMsg = <$app_type as $crate::sdk::base::Handler>::CustomInitMsg;
84
85                fn module_id<'a>() -> &'a str {
86                    $app_const.module_id()
87                }
88
89                fn module_version<'a>() -> &'a str {
90                    $app_const.version()
91                }
92
93                fn dependencies<'a>() -> &'a [$crate::std::objects::dependency::StaticDependency] {
94                    $crate::sdk::base::Handler::dependencies(&$app_const)
95                }
96            }
97        }
98    };
99    ($app_const:expr, $app_type:ty, $interface_name: ident, $custom_exec:ty) => {
100        #[cfg(not(target_arch = "wasm32"))]
101        mod _wrapper_fns {
102            use super::*;
103            $crate::__wrapper_fns_without_custom__!($app_const, $app_type);
104
105            pub fn execute(
106                deps: ::cosmwasm_std::DepsMut,
107                env: ::cosmwasm_std::Env,
108                info: ::cosmwasm_std::MessageInfo,
109                msg: $custom_exec,
110            ) -> Result<::cosmwasm_std::Response, <$app_type as $crate::sdk::base::Handler>::Error>
111            {
112                use $crate::sdk::base::{CustomExecuteHandler, ExecuteEndpoint};
113                match CustomExecuteHandler::try_into_base(msg) {
114                    Ok(default) => $app_const.execute(deps, env, info, default),
115                    Err(custom) => custom.custom_execute(deps, env, info, $app_const),
116                }
117            }
118        }
119
120        pub mod interface {
121            use super::*;
122
123            #[::cw_orch::interface(
124                _wrapper_fns::InstantiateMsg,
125                $custom_exec,
126                _wrapper_fns::QueryMsg,
127                _wrapper_fns::MigrateMsg
128            )]
129            pub struct $interface_name;
130
131            $crate::__cw_orch_interfaces__!($interface_name);
132
133            #[cfg(not(target_arch = "wasm32"))]
134            impl<Chain: ::cw_orch::prelude::CwEnv> $crate::abstract_interface::RegisteredModule
135                for $interface_name<Chain>
136            {
137                type InitMsg = <$app_type as $crate::sdk::base::Handler>::CustomInitMsg;
138
139                fn module_id<'a>() -> &'a str {
140                    $app_const.module_id()
141                }
142
143                fn module_version<'a>() -> &'a str {
144                    $app_const.version()
145                }
146
147                fn dependencies<'a>() -> &'a [$crate::std::objects::dependency::StaticDependency] {
148                    $crate::sdk::base::Handler::dependencies(&$app_const)
149                }
150            }
151        }
152    };
153}
154
155#[macro_export]
156#[doc(hidden)]
157macro_rules! __wrapper_fns_without_custom__ {
158    ($app_const:expr, $app_type:ty) => {
159        pub fn instantiate(
160            deps: ::cosmwasm_std::DepsMut,
161            env: ::cosmwasm_std::Env,
162            info: ::cosmwasm_std::MessageInfo,
163            msg: <$app_type as $crate::sdk::base::InstantiateEndpoint>::InstantiateMsg,
164        ) -> Result<::cosmwasm_std::Response, <$app_type as $crate::sdk::base::Handler>::Error> {
165            use $crate::sdk::base::InstantiateEndpoint;
166            $app_const.instantiate(deps, env, info, msg)
167        }
168
169        pub fn query(
170            deps: ::cosmwasm_std::Deps,
171            env: ::cosmwasm_std::Env,
172            msg: <$app_type as $crate::sdk::base::QueryEndpoint>::QueryMsg,
173        ) -> Result<::cosmwasm_std::Binary, <$app_type as $crate::sdk::base::Handler>::Error> {
174            use $crate::sdk::base::QueryEndpoint;
175            $app_const.query(deps, env, msg)
176        }
177
178        pub fn migrate(
179            deps: ::cosmwasm_std::DepsMut,
180            env: ::cosmwasm_std::Env,
181            msg: <$app_type as $crate::sdk::base::MigrateEndpoint>::MigrateMsg,
182        ) -> Result<::cosmwasm_std::Response, <$app_type as $crate::sdk::base::Handler>::Error> {
183            use $crate::sdk::base::MigrateEndpoint;
184            $app_const.migrate(deps, env, msg)
185        }
186
187        pub fn reply(
188            deps: ::cosmwasm_std::DepsMut,
189            env: ::cosmwasm_std::Env,
190            msg: ::cosmwasm_std::Reply,
191        ) -> Result<::cosmwasm_std::Response, <$app_type as $crate::sdk::base::Handler>::Error> {
192            use $crate::sdk::base::ReplyEndpoint;
193            $app_const.reply(deps, env, msg)
194        }
195
196        pub fn sudo(
197            deps: ::cosmwasm_std::DepsMut,
198            env: ::cosmwasm_std::Env,
199            msg: <$app_type as $crate::sdk::base::Handler>::SudoMsg,
200        ) -> Result<::cosmwasm_std::Response, <$app_type as $crate::sdk::base::Handler>::Error> {
201            use $crate::sdk::base::SudoEndpoint;
202            $app_const.sudo(deps, env, msg)
203        }
204
205        pub type InstantiateMsg =
206            <$app_type as $crate::sdk::base::InstantiateEndpoint>::InstantiateMsg;
207        pub type ExecuteMsg = <$app_type as $crate::sdk::base::ExecuteEndpoint>::ExecuteMsg;
208        pub type QueryMsg = <$app_type as $crate::sdk::base::QueryEndpoint>::QueryMsg;
209        pub type MigrateMsg = <$app_type as $crate::sdk::base::MigrateEndpoint>::MigrateMsg;
210    };
211}
212
213#[macro_export]
214#[doc(hidden)]
215macro_rules! __cw_orch_interfaces__ {
216    ($interface_name: ident) => {
217        #[cfg(not(target_arch = "wasm32"))]
218        impl<Chain: ::cw_orch::prelude::CwEnv> ::cw_orch::prelude::Uploadable
219            for $interface_name<Chain>
220        {
221            fn wasm(_chain: &::cw_orch::prelude::ChainInfoOwned) -> ::cw_orch::prelude::WasmPath {
222                let wasm_name = env!("CARGO_CRATE_NAME").replace('-', "_");
223                ::cw_orch::prelude::ArtifactsDir::auto(Some(env!("CARGO_MANIFEST_DIR").to_string()))
224                    .find_wasm_path(&wasm_name)
225                    .unwrap()
226            }
227
228            fn wrapper() -> Box<
229                dyn ::cw_orch::prelude::MockContract<::cosmwasm_std::Empty, ::cosmwasm_std::Empty>,
230            > {
231                Box::new(
232                    ::cw_orch::prelude::ContractWrapper::new_with_empty(
233                        _wrapper_fns::execute,
234                        _wrapper_fns::instantiate,
235                        _wrapper_fns::query,
236                    )
237                    .with_reply(_wrapper_fns::reply)
238                    .with_migrate(_wrapper_fns::migrate)
239                    .with_sudo(_wrapper_fns::sudo),
240                )
241            }
242        }
243
244        #[cfg(not(target_arch = "wasm32"))]
245        impl<Chain: ::cw_orch::prelude::CwEnv> $crate::abstract_interface::AppDeployer<Chain>
246            for $interface_name<Chain>
247        {
248        }
249
250        #[cfg(not(target_arch = "wasm32"))]
251        impl<T: ::cw_orch::prelude::CwEnv> From<::cw_orch::contract::Contract<T>>
252            for $interface_name<T>
253        {
254            fn from(contract: ::cw_orch::contract::Contract<T>) -> Self {
255                Self(contract)
256            }
257        }
258    };
259}