use serde::{Deserialize, Serialize};
use novax_data::Address;
use crate::errors::{AccountError, NovaXError};
#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct AccountInfosAccountData {
pub address: String,
pub nonce: u64,
pub balance: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub owner_address: Option<String>
}
#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub struct AccountInfosData {
pub account: AccountInfosAccountData
}
#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub struct AccountInfos {
pub data: AccountInfosData
}
impl AccountInfos {
pub async fn from_address(gateway_url: &str, address: &Address) -> Result<AccountInfos, NovaXError> {
let bech32 = address.to_bech32_string()?;
let url = format!("{}/address/{}", gateway_url, bech32);
let response = reqwest::get(&url).await.map_err(|_| AccountError::CannotFetchAccountInfos)?;
let response_text = response.text().await.map_err(|_| AccountError::CannotFetchAccountInfos)?;
let result = serde_json::from_str(&response_text).map_err(|_| AccountError::CannotParseAccountInfos)?;
Ok(result)
}
}