1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use cosmwasm_std::Addr;
use injective_math::FPDecimal;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

use crate::exchange::types::{MarketId, SubaccountId};

use super::types::ShortSubaccountId;

#[derive(Serialize_repr, Deserialize_repr, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[repr(u8)]
pub enum OrderSide {
    Unspecified = 0,
    Buy = 1,
    Sell = 2,
}

#[derive(Serialize_repr, Deserialize_repr, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[repr(u8)]
pub enum OrderType {
    Undefined = 0,
    Buy = 1,
    Sell = 2,
    BuyPo = 7,
    SellPo = 8,
    BuyAtomic = 9,
    SellAtomic = 10,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct OrderData {
    pub market_id: MarketId,
    pub subaccount_id: SubaccountId,
    pub order_hash: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct ShortOrderData {
    pub market_id: MarketId,
    pub subaccount_id: ShortSubaccountId,
    pub order_hash: String,
}

impl From<OrderData> for ShortOrderData {
    fn from(order: OrderData) -> Self {
        ShortOrderData {
            market_id: order.market_id,
            subaccount_id: order.subaccount_id.into(),
            order_hash: order.order_hash,
        }
    }
}

pub fn order_data_to_short(order_data: Vec<OrderData>) -> Vec<ShortOrderData> {
    order_data.into_iter().map(|item| item.into()).collect()
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct OrderInfo {
    pub subaccount_id: SubaccountId,
    #[serde(default)]
    pub fee_recipient: Option<Addr>,
    pub price: FPDecimal,
    pub quantity: FPDecimal,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct ShortOrderInfo {
    pub subaccount_id: ShortSubaccountId,
    #[serde(default)]
    pub fee_recipient: Option<Addr>,
    pub price: FPDecimal,
    pub quantity: FPDecimal,
}

impl From<OrderInfo> for ShortOrderInfo {
    fn from(order_info: OrderInfo) -> Self {
        ShortOrderInfo {
            subaccount_id: order_info.subaccount_id.into(),
            fee_recipient: order_info.fee_recipient,
            price: order_info.price,
            quantity: order_info.quantity,
        }
    }
}

pub trait GenericOrder {
    fn get_order_type(&self) -> &OrderType;
    fn get_order_info(&self) -> &OrderInfo;
    fn get_trigger_price(&self) -> Option<FPDecimal>;
    fn is_buy(&self) -> bool;
    fn is_sell(&self) -> bool;
}

pub trait GenericTrimmedOrder {
    fn get_price(&self) -> FPDecimal;
    fn get_fillable_quantity(&self) -> FPDecimal;
    fn is_buy(&self) -> bool;
    fn is_sell(&self) -> bool;
    fn get_order_hash(&self) -> String;
}

#[cfg(test)]
mod tests {
    use crate::exchange::order::OrderType;

    #[test]
    fn order_type_serialization() {
        let types = vec![OrderType::Undefined, OrderType::Buy, OrderType::SellPo, OrderType::SellAtomic];
        assert_eq!(serde_json_wasm::to_string(&types).unwrap(), "[0,1,8,10]");
    }
}