abstract_sdk/base/features/
abstract_name_service.rs

1use abstract_std::{
2    ans_host::{
3        AssetPairingFilter, AssetPairingMapEntry, PoolAddressListResponse, QueryMsg,
4        RegisteredDexesResponse,
5    },
6    objects::{ans_host::AnsHost, DexAssetPairing},
7};
8use cosmwasm_std::Deps;
9
10use super::ModuleIdentification;
11use crate::apis::AbstractApi;
12use crate::{ans_resolve::Resolve, cw_helpers::ApiQuery, AbstractSdkResult};
13
14/// ANCHOR: ans
15/// Accessor to the Abstract Name Service.
16pub trait AbstractNameService: Sized {
17    /// Get the ANS host address.
18    fn ans_host(&self, deps: Deps) -> AbstractSdkResult<AnsHost>;
19
20    /// Construct the name service client.
21    fn name_service<'a>(&'a self, deps: Deps<'a>) -> AbstractNameServiceClient<'a, Self> {
22        AbstractNameServiceClient {
23            base: self,
24            deps,
25            host: self.ans_host(deps).unwrap(),
26        }
27    }
28}
29/// ANCHOR_END: ans
30
31#[derive(Clone)]
32pub struct AbstractNameServiceClient<'a, T: AbstractNameService> {
33    base: &'a T,
34    deps: Deps<'a>,
35    /// Abstract Name Service Contract
36    pub host: AnsHost,
37}
38
39impl<T: ModuleIdentification + AbstractNameService> AbstractApi<T>
40    for AbstractNameServiceClient<'_, T>
41{
42    const API_ID: &'static str = "AbstractNameServiceClient";
43
44    fn base(&self) -> &T {
45        self.base
46    }
47    fn deps(&self) -> Deps {
48        self.deps
49    }
50}
51
52impl<T: ModuleIdentification + AbstractNameService> AbstractNameServiceClient<'_, T> {
53    /// Query ans entry
54    pub fn query<R: Resolve>(&self, entry: &R) -> AbstractSdkResult<R::Output> {
55        entry
56            .resolve(&self.deps.querier, &self.host)
57            .map_err(|error| self.wrap_query_error(error))
58    }
59
60    /// Returns if the entry is registered on the ANS.
61    /// Will return an Err if the query failed for technical reasons (like a wrong address or state parsing error).
62    /// Will return true if found.
63    /// Will return false if not found.
64    pub fn is_registered(&self, entry: &impl Resolve) -> bool {
65        entry.is_registered(&self.deps.querier, &self.host)
66    }
67    /// Assert that an entry is registered on the ANS. Will return an Err if the entry is not registered.
68    pub fn assert_registered(&self, entry: &impl Resolve) -> AbstractSdkResult<()> {
69        entry
70            .assert_registered(&self.deps.querier, &self.host)
71            .map_err(|error| self.wrap_query_error(error))
72    }
73
74    /// Get AnsHost
75    pub fn host(&self) -> &AnsHost {
76        &self.host
77    }
78    /// Smart-query the available trading pools.
79    pub fn pool_list(
80        &self,
81        filter: Option<AssetPairingFilter>,
82        page_limit: Option<u8>,
83        start_after: Option<DexAssetPairing>,
84    ) -> AbstractSdkResult<Vec<AssetPairingMapEntry>> {
85        let resp: PoolAddressListResponse = self.smart_query(
86            &self.host.address,
87            &QueryMsg::PoolList {
88                filter,
89                start_after,
90                limit: page_limit,
91            },
92        )?;
93        Ok(resp.pools)
94    }
95    /// Raw-query the available dexes on the chain.
96    pub fn registered_dexes(&self) -> AbstractSdkResult<RegisteredDexesResponse> {
97        self.host
98            .query_registered_dexes(&self.deps.querier)
99            .map_err(|error| self.wrap_query_error(error))
100    }
101}