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
use abstract_std::{
    ans_host::{
        AssetPairingFilter, AssetPairingMapEntry, PoolAddressListResponse, QueryMsg,
        RegisteredDexesResponse,
    },
    objects::{ans_host::AnsHost, DexAssetPairing},
};
use cosmwasm_std::Deps;

use super::ModuleIdentification;
use crate::apis::{AbstractApi, ApiIdentification};
use crate::{ans_resolve::Resolve, cw_helpers::ApiQuery, AbstractSdkResult};

/// ANCHOR: ans
/// Accessor to the Abstract Name Service.
pub trait AbstractNameService: Sized {
    /// Get the ANS host address.
    fn ans_host(&self, deps: Deps) -> AbstractSdkResult<AnsHost>;

    /// Construct the name service client.
    fn name_service<'a>(&'a self, deps: Deps<'a>) -> AbstractNameServiceClient<Self> {
        AbstractNameServiceClient {
            base: self,
            deps,
            host: self.ans_host(deps).unwrap(),
        }
    }
}
/// ANCHOR_END: ans

#[derive(Clone)]
pub struct AbstractNameServiceClient<'a, T: AbstractNameService> {
    base: &'a T,
    deps: Deps<'a>,
    /// Abstract Name Service Contract
    pub host: AnsHost,
}

impl<'a, T: ModuleIdentification + AbstractNameService> AbstractApi<T>
    for AbstractNameServiceClient<'a, T>
{
    fn base(&self) -> &T {
        self.base
    }
    fn deps(&self) -> Deps {
        self.deps
    }
}

impl<'a, T: ModuleIdentification + AbstractNameService> ApiIdentification
    for AbstractNameServiceClient<'a, T>
{
    fn api_id() -> String {
        "AbstractNameServiceClient".to_owned()
    }
}

impl<'a, T: ModuleIdentification + AbstractNameService> AbstractNameServiceClient<'a, T> {
    /// Query ans entry
    pub fn query<R: Resolve>(&self, entry: &R) -> AbstractSdkResult<R::Output> {
        entry
            .resolve(&self.deps.querier, &self.host)
            .map_err(|error| self.wrap_query_error(error))
    }

    /// Returns if the entry is registered on the ANS.
    /// Will return an Err if the query failed for technical reasons (like a wrong address or state parsing error).
    /// Will return true if found.
    /// Will return false if not found.
    pub fn is_registered(&self, entry: &impl Resolve) -> bool {
        entry.is_registered(&self.deps.querier, &self.host)
    }
    /// Assert that an entry is registered on the ANS. Will return an Err if the entry is not registered.
    pub fn assert_registered(&self, entry: &impl Resolve) -> AbstractSdkResult<()> {
        entry
            .assert_registered(&self.deps.querier, &self.host)
            .map_err(|error| self.wrap_query_error(error))
    }

    /// Get AnsHost
    pub fn host(&self) -> &AnsHost {
        &self.host
    }
    /// Smart-query the available trading pools.
    pub fn pool_list(
        &self,
        filter: Option<AssetPairingFilter>,
        page_limit: Option<u8>,
        start_after: Option<DexAssetPairing>,
    ) -> AbstractSdkResult<Vec<AssetPairingMapEntry>> {
        let resp: PoolAddressListResponse = self.smart_query(
            &self.host.address,
            &QueryMsg::PoolList {
                filter,
                start_after,
                limit: page_limit,
            },
        )?;
        Ok(resp.pools)
    }
    /// Raw-query the available dexes on the chain.
    pub fn registered_dexes(&self) -> AbstractSdkResult<RegisteredDexesResponse> {
        self.host
            .query_registered_dexes(&self.deps.querier)
            .map_err(|error| self.wrap_query_error(error))
    }
}