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>>,
quote_updates: &State<watch::Receiver<bitmex_price_feed::Quote>>,
) -> Result<status::Accepted<()>, status::BadRequest<String>> {
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");
}

64
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<Dlc>,
}
pub struct Commit {
pub order_id: OrderId,
}
enum SetupState {
Active {
sender: mpsc::UnboundedSender<SetupMsg>,
@ -610,16 +610,23 @@ impl Handler<TakeOffer> for Actor {
}
#[async_trait]
impl Handler<ProposeSettlement> for Actor {
async fn handle(&mut self, msg: ProposeSettlement, _ctx: &mut Context<Self>) {
log_error!(self.handle_propose_settlement(msg.order_id, msg.current_price));
}
}
impl Handler<CfdAction> for Actor {
async fn handle(&mut self, msg: CfdAction, _ctx: &mut Context<Self>) {
use CfdAction::*;
#[async_trait]
impl Handler<ProposeRollOver> for Actor {
async fn handle(&mut self, msg: ProposeRollOver, _ctx: &mut Context<Self>) {
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<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 {
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 {}

Loading…
Cancel
Save