rust #10

Merged
peperunas merged 127 commits from rust into master 2021-02-18 09:42:16 +00:00
Showing only changes of commit 0470578739 - Show all commits

View File

@ -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<f64>,
}
impl Currency {
pub fn new<S: Into<String>, F: Into<f64>>(name: S, amount: F, price: Option<F>) -> 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<f64> {
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<Balance>,
}
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<String> {
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<Balance> {
&self.balances
}