abstract_testing/
abstract_mock_querier.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
use abstract_std::{
    account::state::{ACCOUNT_ID, CALLING_TO_AS_ADMIN},
    ans_host::state::{ASSET_ADDRESSES, CHANNELS, CONTRACT_ADDRESSES},
    objects::{
        gov_type::GovernanceDetails, ownership::Ownership,
        storage_namespaces::OWNERSHIP_STORAGE_KEY, AccountId, AssetEntry, ChannelEntry,
        ContractEntry,
    },
    registry::{state::ACCOUNT_ADDRESSES, Account},
};
use cosmwasm_std::Addr;
use cw_asset::AssetInfo;
use cw_storage_plus::Item;

use crate::prelude::*;

pub trait AbstractMockQuerier {
    /// Mock the existence of an Account by setting the Account id for the account along with registering the account to registry.
    fn account(self, account: &Account, account_id: AccountId) -> Self;

    /// Add mock assets into ANS
    fn assets(self, assets: Vec<(&AssetEntry, AssetInfo)>) -> Self;

    fn set_account_admin_call_to(self, account: &Account) -> Self;

    fn contracts(self, contracts: Vec<(&ContractEntry, Addr)>) -> Self;

    fn channels(self, channels: Vec<(&ChannelEntry, String)>) -> Self;

    fn addrs(&self) -> AbstractMockAddrs;
}

impl AbstractMockQuerier for MockQuerierBuilder {
    /// Mock the existence of an Account by setting the Account id for the account along with registering the account to registry.
    fn account(self, account: &Account, account_id: AccountId) -> Self {
        let abstract_addrs = self.addrs();
        self.with_contract_item(account.addr(), ACCOUNT_ID, &account_id)
            // Setup the account owner as the test owner
            .with_contract_item(
                account.addr(),
                Item::new(OWNERSHIP_STORAGE_KEY),
                &Some(Ownership {
                    owner: GovernanceDetails::Monarchy {
                        monarch: abstract_addrs.owner.clone(),
                    },
                    pending_owner: None,
                    pending_expiry: None,
                }),
            )
            .with_contract_map_entry(
                &abstract_addrs.registry,
                ACCOUNT_ADDRESSES,
                (&account_id, account.clone()),
            )
            .with_contract_map_entry(
                account.addr(),
                abstract_std::account::state::ACCOUNT_MODULES,
                (TEST_MODULE_ID, abstract_addrs.module_address),
            )
    }

    fn assets(self, assets: Vec<(&AssetEntry, AssetInfo)>) -> Self {
        let abstract_addrs = self.addrs();
        self.with_contract_map_entries(&abstract_addrs.ans_host, ASSET_ADDRESSES, assets)
    }

    fn contracts(self, contracts: Vec<(&ContractEntry, Addr)>) -> Self {
        let abstract_addrs = self.addrs();

        self.with_contract_map_entries(&abstract_addrs.ans_host, CONTRACT_ADDRESSES, contracts)
    }

    fn channels(self, channels: Vec<(&ChannelEntry, String)>) -> Self {
        let abstract_addrs = self.addrs();

        self.with_contract_map_entries(&abstract_addrs.ans_host, CHANNELS, channels)
    }

    fn addrs(&self) -> AbstractMockAddrs {
        AbstractMockAddrs::new(self.api)
    }

    fn set_account_admin_call_to(self, account: &Account) -> Self {
        let env = mock_env_validated(self.api);
        self.with_contract_item(account.addr(), CALLING_TO_AS_ADMIN, &env.contract.address)
    }
}