From e2f926388a5f1a691b9d9402f0e187f56725c639 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Fri, 15 Oct 2021 14:38:28 +1100 Subject: [PATCH] Introduce taker_cfd::CfdAction enum To reduce boilerplate and allow us to use an `xtra::MessageChannel` in the (very near) future. --- daemon/src/routes_taker.rs | 7 +++-- daemon/src/taker_cfd.rs | 64 +++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 39 deletions(-) diff --git a/daemon/src/routes_taker.rs b/daemon/src/routes_taker.rs index 1537942..556846b 100644 --- a/daemon/src/routes_taker.rs +++ b/daemon/src/routes_taker.rs @@ -122,6 +122,7 @@ pub async fn post_cfd_action( cfd_actor_address: &State>, quote_updates: &State>, ) -> Result, status::BadRequest> { + use taker_cfd::CfdAction::*; match action { CfdAction::AcceptOrder | CfdAction::RejectOrder @@ -133,14 +134,14 @@ pub async fn post_cfd_action( } CfdAction::Commit => { cfd_actor_address - .do_send_async(taker_cfd::Commit { order_id: id }) + .do_send_async(Commit { order_id: id }) .await .map_err(|e| status::BadRequest(Some(e.to_string())))?; } CfdAction::Settle => { let current_price = quote_updates.borrow().for_taker(); cfd_actor_address - .do_send_async(taker_cfd::ProposeSettlement { + .do_send_async(ProposeSettlement { order_id: id, current_price, }) @@ -149,7 +150,7 @@ pub async fn post_cfd_action( } CfdAction::RollOver => { cfd_actor_address - .do_send_async(taker_cfd::ProposeRollOver { order_id: id }) + .do_send_async(ProposeRollOver { order_id: id }) .await .expect("actor to always be available"); } diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index 4c3c69c..f1c5ce4 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -26,13 +26,17 @@ pub struct TakeOffer { pub quantity: Usd, } -pub struct ProposeSettlement { - pub order_id: OrderId, - pub current_price: Usd, -} - -pub struct ProposeRollOver { - pub order_id: OrderId, +pub enum CfdAction { + ProposeSettlement { + order_id: OrderId, + current_price: Usd, + }, + ProposeRollOver { + order_id: OrderId, + }, + Commit { + order_id: OrderId, + }, } pub struct MakerStreamMessage { @@ -49,10 +53,6 @@ pub struct CfdRollOverCompleted { pub dlc: Result, } -pub struct Commit { - pub order_id: OrderId, -} - enum SetupState { Active { sender: mpsc::UnboundedSender, @@ -610,16 +610,23 @@ impl Handler for Actor { } #[async_trait] -impl Handler for Actor { - async fn handle(&mut self, msg: ProposeSettlement, _ctx: &mut Context) { - log_error!(self.handle_propose_settlement(msg.order_id, msg.current_price)); - } -} +impl Handler for Actor { + async fn handle(&mut self, msg: CfdAction, _ctx: &mut Context) { + use CfdAction::*; -#[async_trait] -impl Handler for Actor { - async fn handle(&mut self, msg: ProposeRollOver, _ctx: &mut Context) { - log_error!(self.handle_propose_roll_over(msg.order_id)); + if let Err(e) = match msg { + Commit { order_id } => self.handle_commit(order_id).await, + ProposeSettlement { + order_id, + current_price, + } => { + self.handle_propose_settlement(order_id, current_price) + .await + } + ProposeRollOver { order_id } => self.handle_propose_roll_over(order_id).await, + } { + tracing::error!("Message handler failed: {:#}", e); + } } } @@ -704,22 +711,11 @@ impl Handler for Actor { } } -#[async_trait] -impl Handler for Actor { - async fn handle(&mut self, msg: Commit, _ctx: &mut Context) { - log_error!(self.handle_commit(msg.order_id)) - } -} - impl Message for TakeOffer { type Result = (); } -impl Message for ProposeSettlement { - type Result = (); -} - -impl Message for ProposeRollOver { +impl Message for CfdAction { type Result = (); } @@ -736,8 +732,4 @@ impl Message for CfdRollOverCompleted { type Result = (); } -impl Message for Commit { - type Result = (); -} - impl xtra::Actor for Actor {}