From d51facc0b2f502e878d2bd815cd10a788d66292e Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Mon, 25 Jan 2021 13:17:13 +0000 Subject: [PATCH] added multiple api keys to handle asynchronous signed requests (conflicting nonces) --- rustybot/src/connectors.rs | 57 +++++++++++++++++++++++++------------- rustybot/src/strategy.rs | 5 +--- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/rustybot/src/connectors.rs b/rustybot/src/connectors.rs index 8ad4e46..2a74e83 100644 --- a/rustybot/src/connectors.rs +++ b/rustybot/src/connectors.rs @@ -22,29 +22,48 @@ pub enum Exchange { #[derive(Eq, PartialEq, Hash, Clone, Debug)] pub enum ExchangeDetails { - Bitfinex { api_key: String, api_secret: String }, + Bitfinex { + prices_api_key: String, + prices_api_secret: String, + orders_api_key: String, + orders_api_secret: String, + positions_api_key: String, + positions_api_secret: String, + }, } /// You do **not** have to wrap the `Client` in an [`Rc`] or [`Arc`] to **reuse** it, /// because it already uses an [`Arc`] internally. #[derive(Clone, Debug)] pub struct Client { - exchange: ExchangeDetails, - inner: Arc>, + exchange: Exchange, + prices_connector: Arc>, + orders_connector: Arc>, + positions_connector: Arc>, } impl Client { pub fn new(exchange: &ExchangeDetails) -> Self { - let inner = match &exchange { + match exchange { ExchangeDetails::Bitfinex { - api_key, - api_secret, - } => BitfinexConnector::new(&api_key, &api_secret), - }; - - Client { - exchange: exchange.clone(), - inner: Arc::new(Box::new(inner)), + prices_api_key, + prices_api_secret, + orders_api_key, + orders_api_secret, + positions_api_key, + positions_api_secret, + } => Self { + exchange: Exchange::Bitfinex, + prices_connector: Arc::new(Box::new( + (BitfinexConnector::new(prices_api_key, prices_api_secret)), + )), + orders_connector: Arc::new(Box::new( + (BitfinexConnector::new(orders_api_key, orders_api_secret)), + )), + positions_connector: Arc::new(Box::new( + (BitfinexConnector::new(positions_api_key, positions_api_secret)), + )), + }, } } @@ -54,8 +73,8 @@ impl Client { ) -> Result>, BoxError> { // retrieving open positions and order book to calculate effective profit/loss let (positions, order_book) = tokio::join!( - self.inner.active_positions(pair), - self.inner.order_book(pair) + self.positions_connector.active_positions(pair), + self.orders_connector.order_book(pair) ); let (mut positions, order_book) = (positions?, order_book?); @@ -75,23 +94,23 @@ impl Client { } pub async fn current_prices(&self, pair: &SymbolPair) -> Result { - self.inner.current_prices(pair).await + self.prices_connector.current_prices(pair).await } pub async fn active_orders(&self, pair: &SymbolPair) -> Result, BoxError> { - self.inner.active_orders(pair).await + self.orders_connector.active_orders(pair).await } pub async fn submit_order(&self, order: &OrderForm) -> Result { - self.inner.submit_order(order).await + self.orders_connector.submit_order(order).await } pub async fn order_book(&self, pair: &SymbolPair) -> Result { - self.inner.order_book(pair).await + self.orders_connector.order_book(pair).await } pub async fn cancel_order(&self, order: &ActiveOrder) -> Result { - self.inner.cancel_order(order).await + self.orders_connector.cancel_order(order).await } } diff --git a/rustybot/src/strategy.rs b/rustybot/src/strategy.rs index c9edd88..923a67d 100644 --- a/rustybot/src/strategy.rs +++ b/rustybot/src/strategy.rs @@ -239,10 +239,7 @@ impl PositionStrategy for TrailingStop { self.update_stop_percentage(&position); - println!( - "Stop percentage: {:0.2}", - self.stop_percentages.get(&position.id()).unwrap() - ); + println!("Stop percentage: {:?}", self.stop_percentages); (position, None, None) }