Browse Source

Introduce taker_cfd::CfdAction enum

To reduce boilerplate and allow us to use an
`xtra::MessageChannel<taker_cfd::CfdAction>` in the (very near)
future.
testing
Lucas Soriano del Pino 3 years ago
parent
commit
e2f926388a
No known key found for this signature in database GPG Key ID: EE611E973A1530E7
  1. 7
      daemon/src/routes_taker.rs
  2. 64
      daemon/src/taker_cfd.rs

7
daemon/src/routes_taker.rs

@ -122,6 +122,7 @@ pub async fn post_cfd_action(
cfd_actor_address: &State<Address<taker_cfd::Actor>>, cfd_actor_address: &State<Address<taker_cfd::Actor>>,
quote_updates: &State<watch::Receiver<bitmex_price_feed::Quote>>, quote_updates: &State<watch::Receiver<bitmex_price_feed::Quote>>,
) -> Result<status::Accepted<()>, status::BadRequest<String>> { ) -> Result<status::Accepted<()>, status::BadRequest<String>> {
use taker_cfd::CfdAction::*;
match action { match action {
CfdAction::AcceptOrder CfdAction::AcceptOrder
| CfdAction::RejectOrder | CfdAction::RejectOrder
@ -133,14 +134,14 @@ pub async fn post_cfd_action(
} }
CfdAction::Commit => { CfdAction::Commit => {
cfd_actor_address cfd_actor_address
.do_send_async(taker_cfd::Commit { order_id: id }) .do_send_async(Commit { order_id: id })
.await .await
.map_err(|e| status::BadRequest(Some(e.to_string())))?; .map_err(|e| status::BadRequest(Some(e.to_string())))?;
} }
CfdAction::Settle => { CfdAction::Settle => {
let current_price = quote_updates.borrow().for_taker(); let current_price = quote_updates.borrow().for_taker();
cfd_actor_address cfd_actor_address
.do_send_async(taker_cfd::ProposeSettlement { .do_send_async(ProposeSettlement {
order_id: id, order_id: id,
current_price, current_price,
}) })
@ -149,7 +150,7 @@ pub async fn post_cfd_action(
} }
CfdAction::RollOver => { CfdAction::RollOver => {
cfd_actor_address cfd_actor_address
.do_send_async(taker_cfd::ProposeRollOver { order_id: id }) .do_send_async(ProposeRollOver { order_id: id })
.await .await
.expect("actor to always be available"); .expect("actor to always be available");
} }

64
daemon/src/taker_cfd.rs

@ -26,13 +26,17 @@ pub struct TakeOffer {
pub quantity: Usd, pub quantity: Usd,
} }
pub struct ProposeSettlement { pub enum CfdAction {
pub order_id: OrderId, ProposeSettlement {
pub current_price: Usd, order_id: OrderId,
} current_price: Usd,
},
pub struct ProposeRollOver { ProposeRollOver {
pub order_id: OrderId, order_id: OrderId,
},
Commit {
order_id: OrderId,
},
} }
pub struct MakerStreamMessage { pub struct MakerStreamMessage {
@ -49,10 +53,6 @@ pub struct CfdRollOverCompleted {
pub dlc: Result<Dlc>, pub dlc: Result<Dlc>,
} }
pub struct Commit {
pub order_id: OrderId,
}
enum SetupState { enum SetupState {
Active { Active {
sender: mpsc::UnboundedSender<SetupMsg>, sender: mpsc::UnboundedSender<SetupMsg>,
@ -610,16 +610,23 @@ impl Handler<TakeOffer> for Actor {
} }
#[async_trait] #[async_trait]
impl Handler<ProposeSettlement> for Actor { impl Handler<CfdAction> for Actor {
async fn handle(&mut self, msg: ProposeSettlement, _ctx: &mut Context<Self>) { async fn handle(&mut self, msg: CfdAction, _ctx: &mut Context<Self>) {
log_error!(self.handle_propose_settlement(msg.order_id, msg.current_price)); use CfdAction::*;
}
}
#[async_trait] if let Err(e) = match msg {
impl Handler<ProposeRollOver> for Actor { Commit { order_id } => self.handle_commit(order_id).await,
async fn handle(&mut self, msg: ProposeRollOver, _ctx: &mut Context<Self>) { ProposeSettlement {
log_error!(self.handle_propose_roll_over(msg.order_id)); 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<oracle::Attestation> for Actor {
} }
} }
#[async_trait]
impl Handler<Commit> for Actor {
async fn handle(&mut self, msg: Commit, _ctx: &mut Context<Self>) {
log_error!(self.handle_commit(msg.order_id))
}
}
impl Message for TakeOffer { impl Message for TakeOffer {
type Result = (); type Result = ();
} }
impl Message for ProposeSettlement { impl Message for CfdAction {
type Result = ();
}
impl Message for ProposeRollOver {
type Result = (); type Result = ();
} }
@ -736,8 +732,4 @@ impl Message for CfdRollOverCompleted {
type Result = (); type Result = ();
} }
impl Message for Commit {
type Result = ();
}
impl xtra::Actor for Actor {} impl xtra::Actor for Actor {}

Loading…
Cancel
Save