trading_types/
liq.rs

1use super::{Amount, Price, Worth};
2use serde::{Deserialize, Serialize};
3
4/// Liq (liquidity) is a complex data type that contains all necessary origin information,
5/// suitable for further processing. Thus, depth (order book) is a vec of Liqs
6#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, PartialOrd)]
7pub struct Liq {
8    pub(super) p: Price,
9    pub(super) a: Amount,
10    pub(super) w: Worth,
11}
12
13impl Liq {
14    pub fn from_pa(p: Price, a: Amount) -> Self {
15        Self {
16            p,
17            a,
18            w: Worth::from_pa(p, a),
19        }
20    }
21
22    pub fn from_pw(p: Price, w: Worth) -> Self {
23        Self {
24            p,
25            a: Amount::from_pw(p, w),
26            w,
27        }
28    }
29
30    pub fn price(&self) -> Price {
31        self.p
32    }
33
34    pub fn amount(&self) -> Amount {
35        self.a
36    }
37
38    pub fn worth(&self) -> Worth {
39        self.w
40    }
41}
42
43impl From<(Price, Amount, Worth)> for Liq {
44    fn from((p, a, w): (Price, Amount, Worth)) -> Self {
45        Self { p, a, w }
46    }
47}
48
49impl From<(Price, Amount)> for Liq {
50    fn from((p, a): (Price, Amount)) -> Self {
51        Self::from_pa(p, a)
52    }
53}
54
55impl From<(Price, Worth)> for Liq {
56    fn from((p, w): (Price, Worth)) -> Self {
57        Self::from_pw(p, w)
58    }
59}
60
61impl From<&[String]> for Liq {
62    fn from(pa: &[String]) -> Self {
63        // must panic on bad incoming data, because this must be fixed on dev stage
64        let p = Price(pa[0].parse().unwrap());
65        let a = Amount(pa[1].parse().unwrap());
66        Self::from((p, a))
67    }
68}
69
70impl From<&Vec<String>> for Liq {
71    fn from(pa: &Vec<String>) -> Self {
72        // must panic on bad incoming data, because this must be fixed on dev stage
73        let p = Price(pa[0].parse().unwrap());
74        let a = Amount(pa[1].parse().unwrap());
75        Self::from((p, a))
76    }
77}
78
79impl From<&[f64]> for Liq {
80    fn from(pa: &[f64]) -> Self {
81        let p = Price(pa[0]);
82        let a = Amount(pa[1]);
83        Self::from((p, a))
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use serde_json::json;
90
91    use super::*;
92
93    #[test]
94    fn test_liq() {
95        let p = Price(10.0);
96        let a = Amount(0.5);
97        let liq = Liq::from_pa(p, a);
98        assert_eq!(liq.worth(), Worth(5.0));
99    }
100
101    #[test]
102    fn test_liq2() {
103        let p = Price(10.0);
104        let a = Amount(0.5);
105        let liq = Liq::from((p, a));
106        assert_eq!(liq.worth(), Worth(5.0));
107    }
108
109    #[test]
110    fn test_show_liq() {
111        let liq = Liq::from_pa(Price(10.0), Amount(0.5));
112        dbg!(liq);
113        assert!(true);
114    }
115
116    #[test]
117    fn test_json_to_liq() {
118        let r: Liq = serde_json::from_str(r#"{"p":1,"a":2,"w":2}"#).unwrap();
119        assert_eq!(r.worth(), Worth(2.0));
120    }
121
122    #[test]
123    fn test_liq_to_json() {
124        // https://serde.rs/derive.html
125        let r = Liq::from_pa(Price(1.0), Amount(10.0));
126        let v = json!(r);
127        assert_eq!(v.to_string(), r#"{"a":10.0,"p":1.0,"w":10.0}"#);
128    }
129}