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
use query_params::QueryParams;
use serde::{Deserialize, Serialize};
use serde_with::chrono::NaiveDateTime;
use serde_with::DefaultOnNull;
use serde_with::TimestampMilliSeconds;
use typed_builder::TypedBuilder;

/// Private API with authentication.
pub mod private;
/// Public API without authentication.
pub mod public;
/// Public streaming API.
pub mod stream;

use serde_with::{serde_as, DisplayFromStr};

/// Asset pair
/// - 0: base asset
/// - 1: quote asset
#[derive(derive_more::Display, Debug, Clone)]
#[display(fmt = "{_0}_{_1}")]
pub struct Pair(pub Asset, pub Asset);

impl std::str::FromStr for Pair {
    type Err = anyhow::Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // Find the _ between the pair.
        let sep = s
            .char_indices()
            .find(|(_, c)| *c == '_')
            .map(|(idx, _)| idx)
            .ok_or(anyhow::anyhow!("failed to parse pair"))?;

        let x = s[0..sep].parse()?;
        let y = s[sep + 1..].parse()?;

        Ok(Self(x, y))
    }
}

/// Asset type
#[derive(strum::EnumString, strum::Display, Debug, Clone)]
#[strum(serialize_all = "snake_case")]
pub enum Asset {
    XRP,
    JPY,
    BTC,
    LTC,
    ETH,
    MONA,
    BCC,
    XLM,
    QTUM,
    BAT,
    OMG,
    XYM,
    LINK,
    MKR,
    BOBA,
    ENJ,
    MATIC,
    DOT,
    DOGE,
    ASTR,
    ADA,
    AVAX,
    AXS,
    FLR,
    SAND,
    GALA,
    APE,
    CHZ,
    OAS,
}
#[cfg(test)]
pub use Asset::*;

/// desc or asc
#[derive(strum::EnumString, strum::Display, Debug, Clone)]
#[strum(serialize_all = "snake_case")]
pub enum SortOrder {
    Desc,
    Asc,
}

/// buy or sell
#[derive(strum::EnumString, strum::Display, Debug, Clone)]
#[strum(serialize_all = "snake_case")]
pub enum Side {
    Buy,
    Sell,
}

/// limit or market or stop or stop limit
#[derive(strum::EnumString, strum::Display, Debug, Clone)]
#[strum(serialize_all = "snake_case")]
pub enum OrderType {
    Limit,
    Market,
    Stop,
    StopLimit,
}

/// maker or taker
#[derive(strum::EnumString, Debug, Clone)]
#[strum(serialize_all = "snake_case")]
pub enum MakerTaker {
    Maker,
    Taker,
}

/// Status of order
#[derive(strum::EnumString, Debug, Clone)]
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
pub enum OrderStatus {
    Inactive,
    Unfilled,
    ParitallyFilled,
    FullyFilled,
    CanceledUnfilled,
    CanceledPartiallyFilled,
}

#[derive(Deserialize, Debug)]
struct Response {
    success: u16,
    data: serde_json::Value,
}

impl Response {
    fn result(self) -> anyhow::Result<serde_json::Value> {
        if self.success == 1 {
            Ok(self.data)
        } else {
            let e: ResponseError = serde_json::from_value(self.data)?;
            Err(ApiError { code: e.code }.into())
        }
    }
}

#[derive(thiserror::Error, Debug)]
#[error("bitbank API error (code={code})")]
struct ApiError {
    code: u16,
}

#[derive(Deserialize, Debug)]
struct ResponseError {
    code: u16,
}