use crate::data::{
account::{Account, AccountResponse},
account_storage::AccountStorageResponse,
address::Address,
dcdt::{DcdtBalance, DcdtBalanceResponse, DcdtRolesResponse},
};
use anyhow::{anyhow, Result};
use std::collections::HashMap;
use super::GatewayProxy;
const ACCOUNT_ENDPOINT: &str = "address/";
const KEYS_ENDPOINT: &str = "/keys/";
impl GatewayProxy {
pub async fn get_account(&self, address: &Address) -> Result<Account> {
if !address.is_valid() {
return Err(anyhow!("invalid address"));
}
let endpoint = ACCOUNT_ENDPOINT.to_string() + address.to_string().as_str();
let endpoint = self.get_endpoint(endpoint.as_str());
let resp = self
.client
.get(endpoint)
.send()
.await?
.json::<AccountResponse>()
.await?;
match resp.data {
None => Err(anyhow!("{}", resp.error)),
Some(b) => Ok(b.account),
}
}
pub async fn get_account_dcdt_roles(
&self,
address: &Address,
) -> Result<HashMap<String, Vec<String>>> {
if !address.is_valid() {
return Err(anyhow!("invalid address"));
}
let endpoint = ACCOUNT_ENDPOINT.to_string() + address.to_string().as_str() + "/dcdts/roles";
let endpoint = self.get_endpoint(endpoint.as_str());
let resp = self
.client
.get(endpoint)
.send()
.await?
.json::<DcdtRolesResponse>()
.await?;
match resp.data {
None => Err(anyhow!("{}", resp.error)),
Some(b) => Ok(b.roles),
}
}
pub async fn get_account_dcdt_tokens(
&self,
address: &Address,
) -> Result<HashMap<String, DcdtBalance>> {
if !address.is_valid() {
return Err(anyhow!("invalid address"));
}
let endpoint = ACCOUNT_ENDPOINT.to_string() + address.to_string().as_str() + "/dcdt";
let endpoint = self.get_endpoint(endpoint.as_str());
let resp = self
.client
.get(endpoint)
.send()
.await?
.json::<DcdtBalanceResponse>()
.await?;
match resp.data {
None => Err(anyhow!("{}", resp.error)),
Some(b) => Ok(b.dcdts),
}
}
pub async fn get_account_storage_keys(
&self,
address: &Address,
) -> Result<HashMap<String, String>> {
if !address.is_valid() {
return Err(anyhow!("invalid address"));
}
let endpoint = ACCOUNT_ENDPOINT.to_string() + address.to_string().as_str() + KEYS_ENDPOINT;
let endpoint = self.get_endpoint(endpoint.as_str());
let resp = self
.client
.get(endpoint)
.send()
.await?
.json::<AccountStorageResponse>()
.await?;
match resp.data {
None => Err(anyhow!("{}", resp.error)),
Some(b) => Ok(b.pairs),
}
}
}