From 0470578739d72fc2fbe9b56963c5fb9df642e22a Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Sun, 3 Jan 2021 15:54:36 +0000 Subject: [PATCH] currency structs --- rustybot/src/currency.rs | 106 +++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 49 deletions(-) diff --git a/rustybot/src/currency.rs b/rustybot/src/currency.rs index 3d8e17e..a9a67af 100644 --- a/rustybot/src/currency.rs +++ b/rustybot/src/currency.rs @@ -1,44 +1,52 @@ +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; +struct Symbol { + name: &'static str +} impl Symbol { - const XMR: &'static str = "XMR"; - const BTC: &'static str = "BTC"; - const ETH: &'static str = "ETH"; - const USD: &'static str = "USD"; -} - -#[derive(Clone)] -struct TradingPair { - quote: Symbol -} - -impl TradingPair {} - -#[derive(Clone)] -struct Currency { - name: String, - amount: f64, - price: Option, -} - -impl Currency { - pub fn new, F: Into>(name: S, amount: F, price: Option) -> Self { - Currency { - name: name.into(), - amount: amount.into(), - price: price.map(|x| x.into()), - } - } - pub fn name(&self) -> &str { &self.name } - pub fn amount(&self) -> f64 { - self.amount +} + +impl Display for Symbol { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.name) } - pub fn price(&self) -> Option { - self.price +} + +#[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 } } @@ -51,22 +59,26 @@ enum WalletKind { #[derive(Clone)] struct Balance { - currency: Currency, - quote: Symbol, + pair: SymbolPair, + base_price: f64, + base_amount: f64, quote_equivalent: f64, wallet: WalletKind, } impl Balance { - pub fn new(currency: Currency, quote: Symbol, quote_equivalent: f64, wallet: WalletKind) -> Self { - Balance { currency, quote, quote_equivalent, wallet } + 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 currency(&self) -> &Currency { - &self.currency + pub fn pair(&self) -> &SymbolPair { + &self.pair } - pub fn quote(&self) -> &Symbol { - &self.quote + 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 @@ -77,14 +89,13 @@ impl Balance { } struct BalanceGroup { - quote: Symbol, quote_equivalent: f64, balances: Vec, } impl BalanceGroup { - pub fn new(quote: Symbol) -> Self { - BalanceGroup { quote, balances: Vec::new(), quote_equivalent: 0f64 } + pub fn new() -> Self { + BalanceGroup { balances: Vec::new(), quote_equivalent: 0f64 } } pub fn add_balance(&mut self, balance: &Balance) { @@ -95,13 +106,10 @@ impl BalanceGroup { pub fn currency_names(&self) -> Vec { self.balances.iter() - .map(|x| x.currency().name().into()) + .map(|x| x.pair().base().name().into()) .collect() } - pub fn quote(&self) -> &Symbol { - &self.quote - } pub fn balances(&self) -> &Vec { &self.balances }