use crate::trade::order::{Cloid, Order};
use crate::utils::sign::{L1Action, Map, Value};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OrderRef {
Oid(u64),
Cloid(Cloid),
}
impl OrderRef {
#[must_use]
pub fn wire(self) -> Value {
match self {
OrderRef::Oid(oid) => Value::Uint(oid),
OrderRef::Cloid(cloid) => Value::from(cloid.to_hex()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Modify {
target: OrderRef,
order: Order,
always_place: bool,
}
impl Modify {
#[must_use]
pub const fn new(target: OrderRef, order: Order) -> Modify {
Modify { target, order, always_place: order.is_trigger() }
}
#[must_use]
pub const fn always_place(mut self) -> Modify {
self.always_place = true;
self
}
#[must_use]
pub const fn is_always_place(&self) -> bool {
self.always_place
}
#[must_use]
pub const fn target(&self) -> OrderRef {
self.target
}
#[must_use]
pub const fn order(&self) -> &Order {
&self.order
}
#[must_use]
pub fn entry(&self) -> Map {
Map::new().put("oid", self.target.wire()).put("order", self.order.wire())
}
}
impl L1Action for Modify {
fn action(&self) -> Map {
Map::new()
.put("type", "modify")
.put("oid", self.target.wire())
.put("order", self.order.wire())
.put_some("a", self.always_place.then_some(true))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BatchModify {
modifies: Vec<Modify>,
always_place: bool,
}
impl BatchModify {
#[must_use]
pub fn new(modifies: Vec<Modify>) -> BatchModify {
let always_place = modifies.iter().any(|modify| modify.order.is_trigger());
BatchModify { modifies, always_place }
}
#[must_use]
pub fn always_place(mut self) -> BatchModify {
self.always_place = true;
self
}
#[must_use]
pub fn modifies(&self) -> &[Modify] {
&self.modifies
}
#[must_use]
pub const fn is_always_place(&self) -> bool {
self.always_place
}
}
impl L1Action for BatchModify {
fn action(&self) -> Map {
Map::new()
.put("type", "batchModify")
.put("modifies", Value::array(self.modifies.iter().map(Modify::entry)))
.put_some("a", self.always_place.then_some(true))
}
}