core/rustybot/src/currency.rs

120 lines
2.4 KiB
Rust
Raw Normal View History

2021-01-03 15:54:36 +00:00
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" };
2021-01-02 19:01:39 +00:00
#[derive(Clone)]
2021-01-03 15:54:36 +00:00
struct Symbol {
name: &'static str
}
2021-01-02 19:01:39 +00:00
impl Symbol {
2021-01-03 15:54:36 +00:00
pub fn name(&self) -> &str {
&self.name
}
2021-01-02 19:01:39 +00:00
}
2021-01-03 15:54:36 +00:00
impl Display for Symbol {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name)
}
2021-01-02 19:01:39 +00:00
}
#[derive(Clone)]
2021-01-03 15:54:36 +00:00
struct SymbolPair {
quote: Symbol,
base: Symbol,
2021-01-02 19:01:39 +00:00
}
2021-01-03 15:54:36 +00:00
impl SymbolPair {
fn trading_repr(&self) -> String {
format!("t{}{}", self.quote, self.base)
2021-01-02 19:01:39 +00:00
}
2021-01-03 15:54:36 +00:00
fn funding_repr(&self) -> String {
format!("f{}{}", self.quote, self.base)
2021-01-02 19:01:39 +00:00
}
2021-01-03 15:54:36 +00:00
pub fn quote(&self) -> &Symbol {
&self.quote
2021-01-02 19:01:39 +00:00
}
2021-01-03 15:54:36 +00:00
pub fn base(&self) -> &Symbol {
&self.base
2021-01-02 19:01:39 +00:00
}
}
#[derive(Clone)]
enum WalletKind {
Margin,
Exchange,
Funding,
}
#[derive(Clone)]
struct Balance {
2021-01-03 15:54:36 +00:00
pair: SymbolPair,
base_price: f64,
base_amount: f64,
2021-01-02 19:01:39 +00:00
quote_equivalent: f64,
wallet: WalletKind,
}
impl Balance {
2021-01-03 15:54:36 +00:00
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 }
2021-01-02 19:01:39 +00:00
}
2021-01-03 15:54:36 +00:00
pub fn pair(&self) -> &SymbolPair {
&self.pair
2021-01-02 19:01:39 +00:00
}
2021-01-03 15:54:36 +00:00
pub fn base_price(&self) -> f64 {
self.base_price
}
pub fn base_amount(&self) -> f64 {
self.base_amount
2021-01-02 19:01:39 +00:00
}
pub fn quote_equivalent(&self) -> f64 {
self.quote_equivalent
}
pub fn wallet(&self) -> &WalletKind {
&self.wallet
}
}
struct BalanceGroup {
quote_equivalent: f64,
balances: Vec<Balance>,
}
impl BalanceGroup {
2021-01-03 15:54:36 +00:00
pub fn new() -> Self {
BalanceGroup { balances: Vec::new(), quote_equivalent: 0f64 }
2021-01-02 19:01:39 +00:00
}
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<String> {
self.balances.iter()
2021-01-03 15:54:36 +00:00
.map(|x| x.pair().base().name().into())
2021-01-02 19:01:39 +00:00
.collect()
}
pub fn balances(&self) -> &Vec<Balance> {
&self.balances
}
}