diff --git a/rustybot/Cargo.toml b/rustybot/Cargo.toml index 06577b5..8b3ea3c 100644 --- a/rustybot/Cargo.toml +++ b/rustybot/Cargo.toml @@ -10,4 +10,4 @@ edition = "2018" bitfinex = { path= "/home/giulio/dev/bitfinex-rs" } tokio = { version = "0.2", features=["full"]} tokio-tungstenite = "*" -futures-util = { version = "0.3", default-features = false, features = ["async-await", "sink", "std"] } \ No newline at end of file +futures-util = { version = "0.3", default-features = false, features = ["async-await", "sink", "std"] } diff --git a/rustybot/src/bot.rs b/rustybot/src/bot.rs index be4a9e3..3b2fa7b 100644 --- a/rustybot/src/bot.rs +++ b/rustybot/src/bot.rs @@ -23,18 +23,18 @@ impl BfxWrapper { } pub async fn current_prices(&self, pair: &SymbolPair) -> Result { - let ticker: TradingPairTicker = self.bfx.ticker.trading_pair(pair.trading_repr()).await?; + let ticker: TradingPairTicker = self.bfx.ticker.trading_pair(pair.clone()).await?; Ok(ticker) } } -pub struct BfxBot<'a> { +pub struct BfxBot { pub bfx: BfxWrapper, ticker: Ticker, - pair_status: Vec>, - quote: Symbol<'a>, - trading_symbols: Vec>, + pair_status: Vec, + quote: Symbol, + trading_symbols: Vec, // account_info: String, // ledger: String, } @@ -56,6 +56,16 @@ impl BfxBot { } } + pub async fn current_prices(&self, symbol: Symbol) -> Result { + let trading_pair = SymbolPair::new(self.quote.clone(), symbol.clone()); + + if !self.trading_symbols.contains(&symbol){ + return Err("Symbol not supported.".into()); + } + + self.bfx.current_prices(&trading_pair).await + } + pub async fn update(&mut self) { println!("Updating..."); delay_for(self.ticker.duration()).await; diff --git a/rustybot/src/currency.rs b/rustybot/src/currency.rs index d31dc05..c057b6e 100644 --- a/rustybot/src/currency.rs +++ b/rustybot/src/currency.rs @@ -1,30 +1,34 @@ use core::fmt; +use std::borrow::Cow; use std::fmt::{Display, Formatter}; -pub const XMR: Symbol = Symbol { name: "XMR" }; -pub const BTC: Symbol = Symbol { name: "BTC" }; -pub const ETH: Symbol = Symbol { name: "ETH" }; -pub const LTC: Symbol = Symbol { name: "LTC" }; -pub const USD: Symbol = Symbol { name: "USD" }; -pub const GBP: Symbol = Symbol { name: "GBP" }; -pub const EUR: Symbol = Symbol { name: "EUR" }; - - -#[derive(Clone)] -pub struct Symbol<'a> { - name: &'a str +#[derive(Clone, PartialEq, Hash)] +pub struct Symbol { + name: Cow<'static, str> } -impl From for Symbol where S:Into { +impl From for Symbol where S: Into { fn from(item: S) -> Self { - Symbol { name: &item.into() } + Symbol::new(item.into()) } } impl Symbol { - pub fn new(name: &str) -> Self { - Symbol { name } + pub const XMR: Symbol = Symbol::new_static("XMR"); + pub const BTC: Symbol = Symbol::new_static("BTC"); + pub const ETH: Symbol = Symbol::new_static("ETH"); + pub const LTC: Symbol = Symbol::new_static("LTC"); + pub const USD: Symbol = Symbol::new_static("USD"); + pub const GBP: Symbol = Symbol::new_static("GBP"); + pub const EUR: Symbol = Symbol::new_static("EUR"); + + pub fn new(name: String) -> Self { + Symbol { name: Cow::from(name) } + } + + pub const fn new_static(name: &'static str) -> Self { + Symbol { name: Cow::Borrowed(name) } } pub fn name(&self) -> &str { @@ -39,24 +43,21 @@ impl Display for Symbol { } #[derive(Clone)] -pub struct SymbolPair<'a> { - quote: Symbol<'a>, - base: Symbol<'a>, +pub struct SymbolPair { + quote: Symbol, + base: Symbol, } impl SymbolPair { pub fn new(quote: Symbol, base: Symbol) -> Self { SymbolPair { quote, base } } - pub fn trading_repr(&self) -> String { format!("t{}{}", self.quote, self.base) } - pub fn funding_repr(&self) -> String { format!("f{}{}", self.quote, self.base) } - pub fn quote(&self) -> &Symbol { &self.quote } @@ -65,6 +66,18 @@ impl SymbolPair { } } +impl Into for SymbolPair { + fn into(self) -> String { + format!("{}{}", self.base, self.quote) + } +} + +impl Display for SymbolPair { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}{}", self.base, self.quote) + } +} + #[derive(Clone)] enum WalletKind { Margin, @@ -73,8 +86,8 @@ enum WalletKind { } #[derive(Clone)] -struct Balance<'a> { - pair: SymbolPair<'a>, +struct Balance { + pair: SymbolPair, base_price: f64, base_amount: f64, quote_equivalent: f64, @@ -103,9 +116,9 @@ impl Balance { } } -struct BalanceGroup<'a> { +struct BalanceGroup { quote_equivalent: f64, - balances: Vec>, + balances: Vec, } impl BalanceGroup { diff --git a/rustybot/src/main.rs b/rustybot/src/main.rs index bf3d1d9..97cda11 100644 --- a/rustybot/src/main.rs +++ b/rustybot/src/main.rs @@ -1,9 +1,9 @@ use bitfinex::api::Bitfinex; use bitfinex::ticker::TradingPairTicker; -use tokio::time::Duration; +use tokio::time::{Duration, delay_for}; use crate::bot::BfxBot; -use crate::currency::{BTC, ETH, SymbolPair, USD, XMR}; +use crate::currency::{Symbol, SymbolPair}; mod ticker; mod events; @@ -21,17 +21,14 @@ async fn main() -> Result<(), BoxError> { let test_api_key = "P1EVE68DJByDAkGQvpIkTwfrbYXd2Vo2ZaIhTYb9vx2"; let test_api_secret = "1nicg8z0zKVEt5Rb7ZDpIYjVYVTgvCaCPMZqB0niFli"; - let mut bot = BfxBot::new(test_api_key, test_api_secret, vec![BTC, XMR, ETH], USD, Duration::new(20, 0)); - let btcusd = SymbolPair::new("USD".into(), "BTC".into()); + let mut bot = BfxBot::new(test_api_key, test_api_secret, vec![Symbol::BTC, Symbol::ETH, Symbol::XMR], Symbol::USD, Duration::new(20, 0)); loop { + let ticker = bot.current_prices("ETH".into()).await?; bot.update().await; - let ticker: TradingPairTicker = bot.bfx.current_prices(&btcusd).await?; + // let ticker = bot.current_prices("ETH".into()).await?; println!("{:?}", ticker); - if (2 < 1) { - break; - } } Ok(()) diff --git a/rustybot/src/pairs.rs b/rustybot/src/pairs.rs index 06d391a..cefd369 100644 --- a/rustybot/src/pairs.rs +++ b/rustybot/src/pairs.rs @@ -7,8 +7,8 @@ use crate::positions::PositionWrapper; use crate::strategy::Strategy; use crate::currency::SymbolPair; -pub struct PairStatus<'a> { - pair: SymbolPair<'a>, +pub struct PairStatus { + pair: SymbolPair, dispatcher: EventDispatcher, prices: HashMap, events: Vec,