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};
pub trait AbstractNameService: Sized {
fn ans_host(&self, deps: Deps) -> AbstractSdkResult<AnsHost>;
fn name_service<'a>(&'a self, deps: Deps<'a>) -> AbstractNameServiceClient<Self> {
AbstractNameServiceClient {
base: self,
deps,
host: self.ans_host(deps).unwrap(),
}
}
}
#[derive(Clone)]
pub struct AbstractNameServiceClient<'a, T: AbstractNameService> {
base: &'a T,
deps: Deps<'a>,
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> {
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))
}
pub fn is_registered(&self, entry: &impl Resolve) -> bool {
entry.is_registered(&self.deps.querier, &self.host)
}
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))
}
pub fn host(&self) -> &AnsHost {
&self.host
}
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)
}
pub fn registered_dexes(&self) -> AbstractSdkResult<RegisteredDexesResponse> {
self.host
.query_registered_dexes(&self.deps.querier)
.map_err(|error| self.wrap_query_error(error))
}
}