use crate::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Account {
pub address: String,
#[serde(deserialize_with = "Hnt::deserialize")]
pub balance: Hnt,
pub dc_balance: u64,
#[serde(deserialize_with = "Hst::deserialize")]
pub sec_balance: Hst,
pub nonce: u64,
#[serde(default)]
pub speculative_nonce: u64,
#[serde(default)]
pub speculative_sec_nonce: u64,
}
pub fn all(client: &Client) -> Stream<Account> {
client.fetch_stream("/accounts", NO_QUERY)
}
pub async fn get(client: &Client, address: &str) -> Result<Account> {
client
.fetch(&format!("/accounts/{}", address), NO_QUERY)
.await
}
pub fn hotspots(client: &Client, address: &str) -> Stream<hotspots::Hotspot> {
client.fetch_stream(&format!("/accounts/{}/hotspots", address), NO_QUERY)
}
pub fn ouis(client: &Client, address: &str) -> Stream<ouis::Oui> {
client.fetch_stream(&format!("/accounts/{}/ouis", address), NO_QUERY)
}
pub fn validators(client: &Client, address: &str) -> Stream<validators::Validator> {
client.fetch_stream(&format!("/accounts/{}/validators", address), NO_QUERY)
}
pub async fn richest(client: &Client, limit: Option<u32>) -> Result<Vec<Account>> {
client
.fetch(
&format!("/accounts/rich?limit={}", limit.unwrap_or(1000)),
NO_QUERY,
)
.await
}
#[cfg(test)]
mod test {
use super::*;
use tokio::test;
#[test]
async fn all() {
let client = Client::default();
let accounts =
accounts::all(&client)
.take(10)
.fold(vec![], |mut acc, account| async move {
acc.push(account.unwrap().address);
acc
});
assert_eq!(accounts.await.len(), 10);
}
#[test]
async fn get() {
let client = Client::default();
let account = accounts::get(
&client,
"13WRNw4fmssJBvMqMnREwe1eCvUVXfnWXSXGcWXyVvAnQUF3D9R",
)
.await
.expect("account");
assert_eq!(
account.address,
"13WRNw4fmssJBvMqMnREwe1eCvUVXfnWXSXGcWXyVvAnQUF3D9R"
);
}
#[test]
async fn ouis() {
let client = Client::default();
let ouis = accounts::ouis(
&client,
"13tyMLKRFYURNBQqLSqNJg9k41maP1A7Bh8QYxR13oWv7EnFooc",
)
.into_vec()
.await
.expect("oui list");
assert_eq!(ouis.len(), 1);
}
#[test]
async fn hotspots() {
let client = Client::default();
let hotspots = accounts::hotspots(
&client,
"13WRNw4fmssJBvMqMnREwe1eCvUVXfnWXSXGcWXyVvAnQUF3D9R",
)
.into_vec()
.await
.expect("hotspot list");
assert!(hotspots.len() > 0);
}
#[test]
async fn richest() {
let client = Client::default();
let richest = accounts::richest(&client, Some(10))
.await
.expect("richet list");
assert_eq!(richest.len(), 10);
}
}