use core::fmt; use std::fmt::{Display, Formatter}; const XMR: Symbol = Symbol { name: "XMR" }; const BTC: Symbol = Symbol { name: "BTC" }; const ETH: Symbol = Symbol { name: "ETH" }; const LTC: Symbol = Symbol { name: "LTC" }; const USD: Symbol = Symbol { name: "USD" }; const GBP: Symbol = Symbol { name: "GBP" }; const EUR: Symbol = Symbol { name: "EUR" }; #[derive(Clone)] struct Symbol { name: &'static str } impl Symbol { pub fn name(&self) -> &str { &self.name } } impl Display for Symbol { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "{}", self.name) } } #[derive(Clone)] struct SymbolPair { quote: Symbol, base: Symbol, } impl SymbolPair { fn trading_repr(&self) -> String { format!("t{}{}", self.quote, self.base) } fn funding_repr(&self) -> String { format!("f{}{}", self.quote, self.base) } pub fn quote(&self) -> &Symbol { &self.quote } pub fn base(&self) -> &Symbol { &self.base } } #[derive(Clone)] enum WalletKind { Margin, Exchange, Funding, } #[derive(Clone)] struct Balance { pair: SymbolPair, base_price: f64, base_amount: f64, quote_equivalent: f64, wallet: WalletKind, } impl Balance { pub fn new(pair: SymbolPair, base_price: f64, base_amount: f64, wallet: WalletKind) -> Self { Balance { pair, base_price, base_amount, quote_equivalent: base_amount * base_price, wallet } } pub fn pair(&self) -> &SymbolPair { &self.pair } pub fn base_price(&self) -> f64 { self.base_price } pub fn base_amount(&self) -> f64 { self.base_amount } pub fn quote_equivalent(&self) -> f64 { self.quote_equivalent } pub fn wallet(&self) -> &WalletKind { &self.wallet } } struct BalanceGroup { quote_equivalent: f64, balances: Vec, } impl BalanceGroup { pub fn new() -> Self { BalanceGroup { balances: Vec::new(), quote_equivalent: 0f64 } } pub fn add_balance(&mut self, balance: &Balance) { self.balances.push(balance.clone()); self.quote_equivalent += balance.quote_equivalent() } pub fn currency_names(&self) -> Vec { self.balances.iter() .map(|x| x.pair().base().name().into()) .collect() } pub fn balances(&self) -> &Vec { &self.balances } }