1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use cosmwasm_std::{Empty, Response};
pub type ApiResult<C = Empty> = Result<Response<C>, ApiError>;
pub use crate::state::ApiContract;
pub use error::ApiError;
pub mod endpoints;
pub mod error;
pub mod features;
mod handler;
pub mod schema;
pub mod state;
#[cfg(feature = "test-utils")]
pub mod mock {
use crate::{ApiContract, ApiError};
use abstract_boot::ApiDeployer;
use abstract_core::api::{self, *};
use abstract_sdk::{base::InstantiateEndpoint, AbstractSdkError};
use abstract_testing::prelude::{
TEST_ADMIN, TEST_ANS_HOST, TEST_MODULE_ID, TEST_VERSION, TEST_VERSION_CONTROL,
};
use boot_core::{BootEnvironment, ContractWrapper};
use cosmwasm_std::{
testing::{mock_env, mock_info},
DepsMut, Empty, Env, MessageInfo, Response, StdError,
};
use thiserror::Error;
pub const TEST_METADATA: &str = "test_metadata";
pub const TEST_TRADER: &str = "test_trader";
#[derive(Error, Debug, PartialEq)]
pub enum MockError {
#[error("{0}")]
Std(#[from] StdError),
#[error(transparent)]
Api(#[from] ApiError),
#[error("{0}")]
Abstract(#[from] abstract_core::AbstractError),
#[error("{0}")]
AbstractSdk(#[from] AbstractSdkError),
}
#[cosmwasm_schema::cw_serde]
pub struct MockInitMsg;
#[cosmwasm_schema::cw_serde]
pub struct MockExecMsg;
impl abstract_core::api::ApiExecuteMsg for MockExecMsg {}
#[cosmwasm_schema::cw_serde]
pub struct MockQueryMsg;
impl abstract_core::api::ApiQueryMsg for MockQueryMsg {}
#[cosmwasm_schema::cw_serde]
pub struct MockReceiveMsg;
pub type MockApiContract =
ApiContract<MockError, MockInitMsg, MockExecMsg, MockQueryMsg, MockReceiveMsg>;
pub const MOCK_API: MockApiContract =
MockApiContract::new(TEST_MODULE_ID, TEST_VERSION, Some(TEST_METADATA))
.with_execute(|_, _, _, _, _| Ok(Response::new().set_data("mock_response".as_bytes())))
.with_instantiate(mock_init_handler);
pub type ApiMockResult = Result<(), MockError>;
crate::export_endpoints!(MOCK_API, MockApiContract);
pub fn mock_init(deps: DepsMut) -> Result<Response, MockError> {
let api = MOCK_API;
let info = mock_info(TEST_ADMIN, &[]);
let init_msg = InstantiateMsg {
base: BaseInstantiateMsg {
ans_host_address: TEST_ANS_HOST.into(),
version_control_address: TEST_VERSION_CONTROL.into(),
},
module: MockInitMsg,
};
api.instantiate(deps, mock_env(), info, init_msg)
}
pub fn mock_init_custom(deps: DepsMut, api: MockApiContract) -> Result<Response, MockError> {
let info = mock_info(TEST_ADMIN, &[]);
let init_msg = InstantiateMsg {
base: BaseInstantiateMsg {
ans_host_address: TEST_ANS_HOST.into(),
version_control_address: TEST_VERSION_CONTROL.into(),
},
module: MockInitMsg,
};
api.instantiate(deps, mock_env(), info, init_msg)
}
fn mock_init_handler(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
_api: MockApiContract,
_msg: MockInitMsg,
) -> Result<Response, MockError> {
Ok(Response::new().set_data("mock_response".as_bytes()))
}
type Exec = api::ExecuteMsg<MockExecMsg>;
type Query = api::QueryMsg<MockQueryMsg>;
type Init = api::InstantiateMsg<MockInitMsg>;
#[boot_core::boot_contract(Init, Exec, Query, Empty)]
pub struct BootMockApi;
impl<Chain: BootEnvironment> ApiDeployer<Chain, MockInitMsg> for BootMockApi<Chain> {}
impl<Chain: boot_core::BootEnvironment> BootMockApi<Chain> {
pub fn new(name: &str, chain: Chain) -> Self {
Self(boot_core::Contract::new(name, chain).with_mock(Box::new(
ContractWrapper::new_with_empty(self::execute, self::instantiate, self::query),
)))
}
}
#[macro_export]
macro_rules! gen_api_mock {
($name:ident, $id:expr, $version:expr, $deps:expr) => {
use ::abstract_core::api::*;
use ::cosmwasm_std::Empty;
use ::abstract_api::mock::{MockExecMsg, MockQueryMsg, MockReceiveMsg, MockInitMsg, MockApiContract, MockError};
const MOCK_API: ::abstract_api::mock::MockApiContract = ::abstract_api::mock::MockApiContract::new($id, $version, None)
.with_dependencies($deps);
fn instantiate(
deps: ::cosmwasm_std::DepsMut,
env: ::cosmwasm_std::Env,
info: ::cosmwasm_std::MessageInfo,
msg: <::abstract_api::mock::MockApiContract as ::abstract_sdk::base::InstantiateEndpoint>::InstantiateMsg,
) -> Result<::cosmwasm_std::Response, <::abstract_api::mock::MockApiContract as ::abstract_sdk::base::Handler>::Error> {
use ::abstract_sdk::base::InstantiateEndpoint;
MOCK_API.instantiate(deps, env, info, msg)
}
fn execute(
deps: ::cosmwasm_std::DepsMut,
env: ::cosmwasm_std::Env,
info: ::cosmwasm_std::MessageInfo,
msg: <::abstract_api::mock::MockApiContract as ::abstract_sdk::base::ExecuteEndpoint>::ExecuteMsg,
) -> Result<::cosmwasm_std::Response, <::abstract_api::mock::MockApiContract as ::abstract_sdk::base::Handler>::Error> {
use ::abstract_sdk::base::ExecuteEndpoint;
MOCK_API.execute(deps, env, info, msg)
}
fn query(
deps: ::cosmwasm_std::Deps,
env: ::cosmwasm_std::Env,
msg: <::abstract_api::mock::MockApiContract as ::abstract_sdk::base::QueryEndpoint>::QueryMsg,
) -> Result<::cosmwasm_std::Binary, <::abstract_api::mock::MockApiContract as ::abstract_sdk::base::Handler>::Error> {
use ::abstract_sdk::base::QueryEndpoint;
MOCK_API.query(deps, env, msg)
}
type Exec = ::abstract_core::api::ExecuteMsg<MockExecMsg, MockReceiveMsg>;
type Query = ::abstract_core::api::QueryMsg<MockQueryMsg>;
type Init = ::abstract_core::api::InstantiateMsg<MockInitMsg>;
#[boot_core::boot_contract(Init, Exec, Query, Empty)]
pub struct $name ;
impl<Chain: ::boot_core::BootEnvironment> ::abstract_boot::ApiDeployer<Chain, MockInitMsg> for $name <Chain> {}
impl<Chain: ::boot_core::BootEnvironment> $name <Chain> {
pub fn new(chain: Chain) -> Self {
Self(
::boot_core::Contract::new($id, chain).with_mock(Box::new(::boot_core::ContractWrapper::<
Exec,
_,
_,
_,
_,
_,
>::new_with_empty(
self::execute,
self::instantiate,
self::query,
))),
)
}
}
};
}
}