use rustlavel_core::Json;
pub const SYSTEM_TOPUP: &str = "system:topup";
pub const SYSTEM_CONSUMPTION: &str = "system:consumption";
pub const SYSTEM_EXPIRY: &str = "system:expiry";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Account {
pub id: String,
pub owner: String,
pub unit: String,
pub balance: i64,
pub held: i64,
pub created_at: u64,
}
impl Account {
pub fn available(&self) -> i64 {
self.balance - self.held
}
pub fn is_system(&self) -> bool {
self.owner.starts_with("system:")
}
pub fn balance_view(&self) -> Balance {
Balance { total: self.balance, held: self.held, available: self.available() }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Balance {
pub total: i64,
pub held: i64,
pub available: i64,
}
impl Balance {
pub fn to_json(&self) -> Json {
Json::object([
("total", Json::from(self.total)),
("held", Json::from(self.held)),
("available", Json::from(self.available)),
])
}
}