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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* Copyright 2023 Architect Financial Technologies LLC. This is free
 * software released under the GNU Affero Public License version 3. */

//! types that are sent on the wire between various parts of the system

use crate::{hcstrid, packed_value, symbology::Symbolic};
use anyhow::{bail, Result};
use netidx::{
    pack::Pack,
    protocol::value::{FromValue, Value},
    utils::pack,
};
use netidx_derive::Pack;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
use serde::{Deserialize, Serialize};

pub mod alerts;
pub mod b2c2;
pub mod coinbase;
pub mod dvchain;
pub mod limits;
pub mod oms_query;
pub mod orderflow;
pub mod secrets;
pub mod symbology;

/// A trading direction
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Pack,
    Serialize,
    Deserialize,
    JsonSchema,
)]
#[repr(i8)]
pub enum Dir {
    Buy = 1,
    Sell = -1,
}

packed_value!(Dir);

impl Dir {
    /// flip the direction Buy -> Sell, Sell -> Buy
    pub fn flip(self) -> Self {
        match self {
            Self::Buy => Self::Sell,
            Self::Sell => Self::Buy,
        }
    }

    pub fn to_str_uppercase(self) -> &'static str {
        match self {
            Self::Buy => "BUY",
            Self::Sell => "SELL",
        }
    }

    pub fn from_str_uppercase(s: &str) -> Result<Self> {
        match s {
            "BUY" => Ok(Self::Buy),
            "SELL" => Ok(Self::Sell),
            _ => bail!("invalid format: {}", s),
        }
    }

    pub fn to_str_lowercase(self) -> &'static str {
        match self {
            Self::Buy => "buy",
            Self::Sell => "sell",
        }
    }

    pub fn from_str_lowercase(s: &str) -> Result<Self> {
        match s {
            "buy" => Ok(Self::Buy),
            "sell" => Ok(Self::Sell),
            _ => bail!("invalid format: {}", s),
        }
    }
}

/// A dirpair is a structure for holding things that depend on trading
/// direction.
///
/// For example one might hold one's position in a particular coin in
/// a `DirPair<Decimal>`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Pack)]
pub struct DirPair<T: 'static> {
    pub buy: T,
    pub sell: T,
}

impl<T: Into<Value> + Pack + 'static> Into<Value> for DirPair<T> {
    fn into(self) -> Value {
        Value::Bytes(pack(&self).unwrap().freeze())
    }
}

impl<T: FromValue + Pack + 'static> FromValue for DirPair<T> {
    fn from_value(v: Value) -> Result<Self> {
        match v {
            Value::Bytes(mut b) => Ok(Pack::decode(&mut b)?),
            _ => bail!("invalid value, expected a bytes {:?}", v),
        }
    }
}

impl<T: Default + 'static> Default for DirPair<T> {
    fn default() -> Self {
        Self { buy: T::default(), sell: T::default() }
    }
}

impl<T: 'static> DirPair<T> {
    /// get a shared reference to the field specified by dir
    pub fn get(&self, dir: Dir) -> &T {
        match dir {
            Dir::Buy => &self.buy,
            Dir::Sell => &self.sell,
        }
    }

    /// get a mutable reference to field side specified by dir
    pub fn get_mut(&mut self, dir: Dir) -> &mut T {
        match dir {
            Dir::Buy => &mut self.buy,
            Dir::Sell => &mut self.sell,
        }
    }
}

impl DirPair<Decimal> {
    /// true if both sides are 0
    pub fn is_empty(&self) -> bool {
        self.buy == dec!(0) && self.sell == dec!(0)
    }

    /// net the buy and the sell side (buy - sell)
    pub fn net(&self) -> Decimal {
        self.buy - self.sell
    }
}

hcstrid!(Desk);
packed_value!(Desk);

hcstrid!(Trader);
packed_value!(Trader);

hcstrid!(Account);
packed_value!(Account);