Skip to main content

zcash_client_backend/tor/http/cryptex/
coin_ex.rs

1use hyper::StatusCode;
2use rust_decimal::Decimal;
3use serde::Deserialize;
4
5use super::{Exchange, ExchangeData, RETRY_LIMIT, retry_filter};
6use crate::tor::{Client, Error, http::HttpError};
7
8/// Querier for the CoinEx exchange.
9pub struct CoinEx {
10    _private: (),
11}
12
13impl CoinEx {
14    /// Prepares for unauthenticated connections to CoinEx.
15    pub fn unauthenticated() -> Self {
16        Self { _private: () }
17    }
18}
19
20#[derive(Debug, Deserialize)]
21#[allow(dead_code)]
22struct CoinExDepth {
23    asks: Vec<(Decimal, Decimal)>,
24    bids: Vec<(Decimal, Decimal)>,
25    checksum: i32,
26    last: Decimal,
27    updated_at: u64,
28}
29
30#[derive(Debug, Deserialize)]
31#[allow(dead_code)]
32struct CoinExData {
33    market: String,
34    is_full: bool,
35    depth: CoinExDepth,
36}
37
38#[derive(Debug, Deserialize)]
39#[allow(dead_code)]
40struct CoinExResponse {
41    code: u32,
42    message: String,
43    data: CoinExData,
44}
45
46impl Exchange for CoinEx {
47    async fn query_zec_to_usd(&self, client: &Client) -> Result<ExchangeData, Error> {
48        // API documentation:
49        // https://docs.coinex.com/api/v2/spot/market/http/list-market-depth
50        let res = client
51            .http_get_json::<CoinExResponse>(
52                "https://api.coinex.com/v2/spot/depth?market=ZECUSDT&limit=5&interval=0"
53                    .parse()
54                    .unwrap(),
55                |b| b,
56                RETRY_LIMIT,
57                retry_filter,
58            )
59            .await?;
60        let data = res.into_body().data.depth;
61        Ok(ExchangeData {
62            bid: data
63                .bids
64                .first()
65                .ok_or(Error::Http(HttpError::Unsuccessful(
66                    StatusCode::SERVICE_UNAVAILABLE,
67                )))?
68                .0,
69            ask: data
70                .asks
71                .first()
72                .ok_or(Error::Http(HttpError::Unsuccessful(
73                    StatusCode::SERVICE_UNAVAILABLE,
74                )))?
75                .0,
76        })
77    }
78}