currency structs
This commit is contained in:
parent
deff46d143
commit
0470578739
@ -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)]
|
#[derive(Clone)]
|
||||||
struct Symbol;
|
struct Symbol {
|
||||||
|
name: &'static str
|
||||||
|
}
|
||||||
|
|
||||||
impl Symbol {
|
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 {
|
pub fn name(&self) -> &str {
|
||||||
&self.name
|
&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)]
|
#[derive(Clone)]
|
||||||
struct Balance {
|
struct Balance {
|
||||||
currency: Currency,
|
pair: SymbolPair,
|
||||||
quote: Symbol,
|
base_price: f64,
|
||||||
|
base_amount: f64,
|
||||||
quote_equivalent: f64,
|
quote_equivalent: f64,
|
||||||
wallet: WalletKind,
|
wallet: WalletKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Balance {
|
impl Balance {
|
||||||
pub fn new(currency: Currency, quote: Symbol, quote_equivalent: f64, wallet: WalletKind) -> Self {
|
pub fn new(pair: SymbolPair, base_price: f64, base_amount: f64, wallet: WalletKind) -> Self {
|
||||||
Balance { currency, quote, quote_equivalent, wallet }
|
Balance { pair, base_price, base_amount, quote_equivalent: base_amount * base_price, wallet }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn currency(&self) -> &Currency {
|
pub fn pair(&self) -> &SymbolPair {
|
||||||
&self.currency
|
&self.pair
|
||||||
}
|
}
|
||||||
pub fn quote(&self) -> &Symbol {
|
pub fn base_price(&self) -> f64 {
|
||||||
&self.quote
|
self.base_price
|
||||||
|
}
|
||||||
|
pub fn base_amount(&self) -> f64 {
|
||||||
|
self.base_amount
|
||||||
}
|
}
|
||||||
pub fn quote_equivalent(&self) -> f64 {
|
pub fn quote_equivalent(&self) -> f64 {
|
||||||
self.quote_equivalent
|
self.quote_equivalent
|
||||||
@ -77,14 +89,13 @@ impl Balance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct BalanceGroup {
|
struct BalanceGroup {
|
||||||
quote: Symbol,
|
|
||||||
quote_equivalent: f64,
|
quote_equivalent: f64,
|
||||||
balances: Vec<Balance>,
|
balances: Vec<Balance>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BalanceGroup {
|
impl BalanceGroup {
|
||||||
pub fn new(quote: Symbol) -> Self {
|
pub fn new() -> Self {
|
||||||
BalanceGroup { quote, balances: Vec::new(), quote_equivalent: 0f64 }
|
BalanceGroup { balances: Vec::new(), quote_equivalent: 0f64 }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_balance(&mut self, balance: &Balance) {
|
pub fn add_balance(&mut self, balance: &Balance) {
|
||||||
@ -95,13 +106,10 @@ impl BalanceGroup {
|
|||||||
|
|
||||||
pub fn currency_names(&self) -> Vec<String> {
|
pub fn currency_names(&self) -> Vec<String> {
|
||||||
self.balances.iter()
|
self.balances.iter()
|
||||||
.map(|x| x.currency().name().into())
|
.map(|x| x.pair().base().name().into())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn quote(&self) -> &Symbol {
|
|
||||||
&self.quote
|
|
||||||
}
|
|
||||||
pub fn balances(&self) -> &Vec<Balance> {
|
pub fn balances(&self) -> &Vec<Balance> {
|
||||||
&self.balances
|
&self.balances
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user