abstract_adapter/
interface.rs

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