abstract_testing/
lib.rs

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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
pub(crate) mod abstract_mock_querier;
pub mod map_tester;
pub mod mock_ans;
pub(crate) mod mock_querier;

use abstract_std::account::{ConfigResponse as AccountConfigResponse, QueryMsg as AccountQueryMsg};
use abstract_std::objects::ABSTRACT_ACCOUNT_ID;
use abstract_std::registry;
use abstract_std::{
    account::state::ACCOUNT_ID,
    account::state::ACCOUNT_MODULES,
    objects::{
        module::{ModuleInfo, ModuleVersion},
        module_reference::ModuleReference,
        ownership,
    },
    registry::state::{ACCOUNT_ADDRESSES, REGISTERED_MODULES},
    ACCOUNT,
};
use cosmwasm_std::{
    from_json,
    testing::{MockApi, MockQuerier, MockStorage},
    to_json_binary, Addr, Binary, Empty, OwnedDeps,
};
use cosmwasm_std::{ContractInfo, Env};
pub use mock_ans::MockAnsHost;
pub use mock_querier::{
    map_key, raw_map_key, wrap_querier, MockQuerierBuilder, MockQuerierOwnership,
};
use module::{TEST_MODULE_ID, TEST_MODULE_RESPONSE};
use prelude::*;
pub type MockDeps = OwnedDeps<MockStorage, MockApi, MockQuerier>;

pub fn abstract_mock_querier_builder(mock_api: MockApi) -> MockQuerierBuilder {
    let raw_handler = move |contract: &Addr, key: &Binary| {
        let abstr = AbstractMockAddrs::new(mock_api);

        if contract == abstr.account.addr() {
            // Return the default value
            Ok(Binary::default())
        } else if contract == abstr.registry {
            // Default value
            Ok(Binary::default())
        } else {
            let str_key = std::str::from_utf8(key.as_slice()).unwrap();

            Err(format!(
                "attempt to query {} with key {}",
                contract, str_key
            ))
        }
    };
    let abstr = AbstractMockAddrs::new(mock_api);

    MockQuerierBuilder::new(mock_api)
        .with_fallback_raw_handler(raw_handler)
        .with_contract_map_entry(
            &abstr.registry,
            ACCOUNT_ADDRESSES,
            (&ABSTRACT_ACCOUNT_ID, abstr.account.clone()),
        )
        .with_contract_item(
            &abstr.registry,
            registry::state::CONFIG,
            &registry::Config {
                security_enabled: false,
                namespace_registration_fee: None,
            },
        )
        .with_contract_map_entry(
            &abstr.registry,
            REGISTERED_MODULES,
            (
                &ModuleInfo::from_id(ACCOUNT, ModuleVersion::Version(TEST_VERSION.into())).unwrap(),
                ModuleReference::Account(1),
            ),
        )
        .with_contract_map_entry(
            // Adding a map module inside the registry
            &abstr.registry,
            REGISTERED_MODULES,
            (
                &ModuleInfo::from_id(TEST_MODULE_ID, ModuleVersion::Version(TEST_VERSION.into()))
                    .unwrap(),
                ModuleReference::App(2),
            ),
        )
        .with_contract_item(abstr.account.addr(), ACCOUNT_ID, &ABSTRACT_ACCOUNT_ID)
        .with_contract_version(abstr.account.addr(), ACCOUNT, TEST_VERSION)
        .with_smart_handler(&abstr.module_address, |msg| {
            let Empty {} = from_json(msg).unwrap();
            Ok(to_json_binary(TEST_MODULE_RESPONSE).unwrap())
        })
        .with_contract_map_entry(
            abstr.account.addr(),
            ACCOUNT_MODULES,
            (TEST_MODULE_ID, abstr.module_address),
        )
        .with_smart_handler(abstr.account.addr(), move |msg| {
            let abstr = AbstractMockAddrs::new(mock_api);
            match from_json(msg).unwrap() {
                AccountQueryMsg::Config {} => {
                    let resp = AccountConfigResponse {
                        registry_address: abstr.registry,
                        module_factory_address: abstr.module_factory,
                        account_id: ABSTRACT_ACCOUNT_ID, // mock value, not used
                        is_suspended: false,
                        whitelisted_addresses: vec![],
                    };
                    Ok(to_json_binary(&resp).unwrap())
                }
                AccountQueryMsg::Ownership {} => {
                    let resp = ownership::Ownership {
                        owner: ownership::GovernanceDetails::Monarchy {
                            monarch: abstr.owner,
                        },
                        pending_expiry: None,
                        pending_owner: None,
                    };
                    Ok(to_json_binary(&resp).unwrap())
                }
                _ => panic!("unexpected message"),
            }
        })
        .with_owner(abstr.account.addr(), Some(&abstr.owner))
}

/// A mock querier that returns the following responses for the following **RAW** contract -> queries:
/// - ABSTRACT_ACCOUNT
///   - "admin" -> TEST_OWNER
///   - "modules:TEST_MODULE_ID" -> TEST_MODULE_ADDRESS
///   - "account_id" -> ABSTRACT_ACCOUNT_ID
/// - REGISTRY
///   - "account" -> { ABSTRACT_ACCOUNT }
///
/// Also it returns query responses for
/// - [`cosmwasm_std::WasmQuery::ContractInfo`]
///   - $contract_address -> ContractInfoResponse { creator: api.addr_make([`crate::OWNER`]), code_id: 1, admin: $contract_address}
/// - [`cosmwasm_std::WasmQuery::CodeInfo`]
///   - $code_id -> CodeInfoResponse { code_id: $code_id, creator: api.addr_make([`crate::OWNER`]), checksum: [`abstract_std::native_addrs::BLOB_CHECKSUM`]}
pub fn abstract_mock_querier(mock_api: MockApi) -> MockQuerier {
    abstract_mock_querier_builder(mock_api).build()
}

/// cosmwasm_std::mock_env_validated(deps.api), but address generated with MockApi
pub fn mock_env_validated(mock_api: MockApi) -> Env {
    Env {
        contract: ContractInfo {
            address: mock_api.addr_make(cosmwasm_std::testing::MOCK_CONTRACT_ADDR),
        },
        ..cosmwasm_std::testing::mock_env()
    }
}

/// use the package version as test version, breaks tests otherwise.
pub const TEST_VERSION: &str = env!("CARGO_PKG_VERSION");
pub mod addresses {
    use abstract_std::{native_addrs, registry::Account};
    use cosmwasm_std::{instantiate2_address, testing::MockApi, Addr, Api};

    // Test addr makers
    const ADMIN_ACCOUNT: &str = "admin_account_address";
    const TEST_ACCOUNT: &str = "account_address";

    pub fn admin_account(mock_api: MockApi) -> Account {
        Account::new(mock_api.addr_make(ADMIN_ACCOUNT))
    }

    pub fn test_account(mock_api: MockApi) -> Account {
        Account::new(mock_api.addr_make(TEST_ACCOUNT))
    }

    impl AbstractMockAddrs {
        pub fn new(mock_api: MockApi) -> AbstractMockAddrs {
            let owner = mock_api.addr_make(crate::OWNER);
            let owner_canon = mock_api.addr_canonicalize(owner.as_str()).unwrap();
            let ans_host = instantiate2_address(
                &native_addrs::BLOB_CHECKSUM,
                &owner_canon,
                native_addrs::ANS_HOST_SALT,
            )
            .unwrap();
            let registry = instantiate2_address(
                &native_addrs::BLOB_CHECKSUM,
                &owner_canon,
                native_addrs::REGISTRY_SALT,
            )
            .unwrap();
            let module_factory = instantiate2_address(
                &native_addrs::BLOB_CHECKSUM,
                &owner_canon,
                native_addrs::MODULE_FACTORY_SALT,
            )
            .unwrap();

            AbstractMockAddrs {
                owner,
                ans_host: mock_api.addr_humanize(&ans_host).unwrap(),
                registry: mock_api.addr_humanize(&registry).unwrap(),
                module_factory: mock_api.addr_humanize(&module_factory).unwrap(),
                account: admin_account(mock_api),
                module_address: mock_api.addr_make("module"),
            }
        }
    }

    #[derive(Debug, Clone)]
    pub struct AbstractMockAddrs {
        pub owner: Addr,
        pub ans_host: Addr,
        pub registry: Addr,
        pub module_factory: Addr,
        pub module_address: Addr,
        pub account: Account,
    }
}

pub mod ans {
    pub const TEST_CHAIN: &str = "chain";
    pub const TEST_DEX: &str = "test_dex";
    pub const TEST_ASSET_1: &str = "chain>asset1";
    pub const TEST_ASSET_2: &str = "chain>asset2";
    pub const TEST_LP_TOKEN_NAME: &str = "test_dex/chain>asset1,chain>asset2";
    pub const TEST_UNIQUE_ID: u64 = 69u64;
    pub const TTOKEN: &str = "test_token";
    pub const EUR_USD_PAIR: &str = "dex:eur_usd_pair";
    pub const EUR_USD_LP: &str = "dex/eur,usd";
    pub const TTOKEN_EUR_PAIR: &str = "dex:wynd_eur_pair";
    pub const TTOKEN_EUR_LP: &str = "dex/wynd,eur";
    pub const EUR: &str = "eur";
    pub const USD: &str = "usd";
}

pub mod module {
    pub const TEST_MODULE_ID: &str = "tester:test-module-id";
    pub const TEST_WITH_DEP_MODULE_ID: &str = "tester-dependency:test-depending-module-id";
    pub const TEST_WITH_DEP_NAMESPACE: &str = "tester-dependency";
    pub const TEST_MODULE_NAME: &str = "test-module-id";
    pub const TEST_NAMESPACE: &str = "tester";

    pub const TEST_MODULE_RESPONSE: &str = "test_module_response";
}
pub mod prelude {
    pub use super::{abstract_mock_querier, abstract_mock_querier_builder, mock_env_validated};
    pub use abstract_mock_querier::AbstractMockQuerier;
    use abstract_std::objects::{AccountId, AccountTrace};
    pub use addresses::*;
    pub use ans::*;
    pub use cosmwasm_std::{
        from_json,
        testing::{MockApi as CwMockApi, MockQuerier, MockStorage},
        to_json_binary,
    };
    pub use mock_querier::{map_key, raw_map_key, wrap_querier, MockQuerierBuilder};
    pub use module::*;

    use super::*;
    pub use super::{MockAnsHost, MockDeps, TEST_VERSION};
    pub const OWNER: &str = "owner";
    pub const TEST_ACCOUNT_ID: AccountId = AccountId::const_new(1, AccountTrace::Local);
}