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
use std::env;

pub use abstract_std::ans_host::ExecuteMsgFns;
use abstract_std::{
    ans_host::*,
    objects::{
        pool_metadata::ResolvedPoolMetadata, AnsAsset, AnsEntryConvertor, AssetEntry, ChannelEntry,
        ContractEntry, DexAssetPairing, LpToken, PoolMetadata, PoolReference, UniquePoolId,
    },
    ANS_HOST,
};
use cw_address_like::AddressLike;
use cw_asset::{Asset, AssetInfo, AssetInfoBase, AssetInfoUnchecked, AssetUnchecked};
use cw_orch::{interface, prelude::*};

#[interface(InstantiateMsg, ExecuteMsg, QueryMsg, MigrateMsg)]
pub struct AnsHost<Chain>;

impl<Chain: CwEnv> AnsHost<Chain> {
    pub fn resolve<R: ClientResolve<Chain>>(&self, item: &R) -> Result<R::Output, CwOrchError> {
        item.resolve(self)
    }
}

impl<Chain: CwEnv> Uploadable for AnsHost<Chain> {
    #[cfg(feature = "integration")]
    fn wrapper() -> <Mock as ::cw_orch::environment::TxHandler>::ContractSource {
        Box::new(
            ContractWrapper::new_with_empty(
                ::ans_host::contract::execute,
                ::ans_host::contract::instantiate,
                ::ans_host::contract::query,
            )
            .with_migrate(::ans_host::contract::migrate),
        )
    }
    fn wasm(_chain: &ChainInfoOwned) -> WasmPath {
        artifacts_dir_from_workspace!()
            .find_wasm_path("ans_host")
            .unwrap()
    }
}

impl<Chain: CwEnv> AnsHost<Chain>
where
    TxResponse<Chain>: IndexResponse,
{
    pub fn load(chain: Chain, address: &Addr) -> Self {
        let contract = cw_orch::contract::Contract::new(ANS_HOST, chain);
        contract.set_address(address);
        Self(contract)
    }
}

pub trait ClientResolve<Chain: CwEnv> {
    /// Result of resolving an entry.
    type Output;
    /// Resolve an entry into its value.
    fn resolve(&self, ans_host: &AnsHost<Chain>) -> Result<Self::Output, CwOrchError>;
}

// cw-multi-test doesn't support raw queries, so we will have to do smart queries instead

impl<Chain: CwEnv> ClientResolve<Chain> for AssetEntry {
    type Output = AssetInfo;

    fn resolve(&self, ans_host: &AnsHost<Chain>) -> Result<Self::Output, CwOrchError> {
        let mut assets: AssetsResponse = ans_host.query(&QueryMsg::Assets {
            names: vec![self.to_string()],
        })?;
        Ok(assets.assets.pop().unwrap().1)
    }
}

impl<Chain: CwEnv> ClientResolve<Chain> for LpToken {
    type Output = AssetInfo;

    fn resolve(&self, ans_host: &AnsHost<Chain>) -> Result<Self::Output, CwOrchError> {
        let asset_entry = AnsEntryConvertor::new(self.clone()).asset_entry();
        asset_entry.resolve(ans_host)
    }
}

impl<Chain: CwEnv> ClientResolve<Chain> for ContractEntry {
    type Output = Addr;

    fn resolve(&self, ans_host: &AnsHost<Chain>) -> Result<Self::Output, CwOrchError> {
        let mut contracts: ContractsResponse = ans_host.query(&QueryMsg::Contracts {
            entries: vec![self.clone()],
        })?;
        Ok(contracts.contracts.pop().unwrap().1)
    }
}

impl<Chain: CwEnv> ClientResolve<Chain> for ChannelEntry {
    type Output = String;

    fn resolve(&self, ans_host: &AnsHost<Chain>) -> Result<Self::Output, CwOrchError> {
        let mut channels: ChannelsResponse = ans_host.query(&QueryMsg::Channels {
            entries: vec![self.clone()],
        })?;
        Ok(channels.channels.pop().unwrap().1)
    }
}

impl<Chain: CwEnv> ClientResolve<Chain> for DexAssetPairing {
    type Output = Vec<PoolReference>;

    fn resolve(&self, ans_host: &AnsHost<Chain>) -> Result<Self::Output, CwOrchError> {
        let mut pool_address_list: PoolAddressListResponse =
            ans_host.query(&QueryMsg::PoolList {
                filter: Some(AssetPairingFilter {
                    asset_pair: Some((self.asset_x().clone(), self.asset_y().clone())),
                    dex: Some(self.dex().to_owned()),
                }),
                start_after: None,
                limit: None,
            })?;
        let found = pool_address_list
            .pools
            .pop()
            .ok_or(CwOrchError::StdErr(format!(
                "Pool reference for {self} not found"
            )))?;
        Ok(found.1.clone())
    }
}

impl<Chain: CwEnv> ClientResolve<Chain> for UniquePoolId {
    type Output = PoolMetadata;

    fn resolve(&self, ans_host: &AnsHost<Chain>) -> Result<Self::Output, CwOrchError> {
        let mut pool_metadatas: PoolMetadatasResponse =
            ans_host.query(&QueryMsg::PoolMetadatas { ids: vec![*self] })?;
        Ok(pool_metadatas.metadatas.pop().unwrap().1)
    }
}

impl<Chain: CwEnv> ClientResolve<Chain> for AnsAsset {
    type Output = Asset;

    fn resolve(&self, ans_host: &AnsHost<Chain>) -> Result<Self::Output, CwOrchError> {
        Ok(Asset::new(self.name.resolve(ans_host)?, self.amount))
    }
}

impl<Chain: CwEnv, T: AddressLike> ClientResolve<Chain> for AssetInfoBase<T> {
    type Output = AssetEntry;

    fn resolve(&self, ans_host: &AnsHost<Chain>) -> Result<Self::Output, CwOrchError> {
        let asset_info_unchecked = match self {
            AssetInfoBase::Native(native) => AssetInfoUnchecked::native(native),
            AssetInfoBase::Cw20(cw20) => AssetInfoUnchecked::cw20(cw20.to_string()),
            _ => panic!("Not supported asset type"),
        };
        let mut assets: AssetInfosResponse = ans_host.query(&QueryMsg::AssetInfos {
            infos: vec![asset_info_unchecked],
        })?;
        Ok(assets.infos.pop().unwrap().1)
    }
}

impl<Chain: CwEnv> ClientResolve<Chain> for AssetUnchecked {
    type Output = AnsAsset;

    fn resolve(&self, ans_host: &AnsHost<Chain>) -> Result<Self::Output, CwOrchError> {
        Ok(AnsAsset {
            name: self.info.resolve(ans_host)?,
            amount: self.amount,
        })
    }
}

impl<Chain: CwEnv> ClientResolve<Chain> for PoolMetadata {
    type Output = ResolvedPoolMetadata;

    fn resolve(&self, ans_host: &AnsHost<Chain>) -> Result<Self::Output, CwOrchError> {
        Ok(ResolvedPoolMetadata {
            assets: self.assets.resolve(ans_host)?,
            dex: self.dex.clone(),
            pool_type: self.pool_type,
        })
    }
}

impl<Chain: CwEnv, T> ClientResolve<Chain> for Vec<T>
where
    T: ClientResolve<Chain>,
{
    type Output = Vec<T::Output>;

    fn resolve(&self, ans_host: &AnsHost<Chain>) -> Result<Self::Output, CwOrchError> {
        self.iter().map(|entry| entry.resolve(ans_host)).collect()
    }
}