use serde::Deserialize;
use crate::client::Network;
use crate::client::http::{ApiError, Transport};
use crate::earn::pools::reads::{PoolFilter, PublicPool, account_row, public_pool};
use crate::earn::staking::actions::staking_pool_index;
use crate::utils::ids::whole;
use crate::utils::{AccountIndex, AssetId, Dec};
pub async fn staking_pool(transport: &impl Transport, network: Network) -> Result<Option<PublicPool>, ApiError> {
match staking_pool_index(network) {
Some(index) => public_pool(transport, index, PoolFilter::Stake).await,
None => Ok(None),
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct PendingUnlock {
#[serde(deserialize_with = "whole")]
pub unlock_timestamp: u64,
#[serde(rename = "asset_index")]
pub asset: AssetId,
pub amount: Dec,
}
pub async fn pending_unlocks(
transport: &impl Transport,
account: AccountIndex,
) -> Result<Vec<PendingUnlock>, ApiError> {
Ok(account_row(transport, account).await?.map(|row| row.pending_unlocks).unwrap_or_default())
}
pub async fn staked_shares(
transport: &impl Transport,
network: Network,
account: AccountIndex,
) -> Result<u64, ApiError> {
let Some(pool) = staking_pool_index(network) else { return Ok(0) };
let shares = account_row(transport, account).await?.map(|row| row.shares).unwrap_or_default();
Ok(shares.iter().find(|share| share.pool == pool).map_or(0, |share| share.shares))
}
#[cfg(test)]
#[path = "tests/reads.rs"]
mod tests;