Skip to main content

zcash_client_backend/tor/http/cryptex/
ku_coin.rs

1use rust_decimal::Decimal;
2use serde::Deserialize;
3
4use super::{Exchange, ExchangeData, RETRY_LIMIT, retry_filter};
5use crate::tor::{Client, Error};
6
7/// Querier for the KuCoin exchange.
8pub struct KuCoin {
9    _private: (),
10}
11
12impl KuCoin {
13    /// Prepares for unauthenticated connections to KuCoin.
14    pub fn unauthenticated() -> Self {
15        Self { _private: () }
16    }
17}
18
19#[derive(Debug, Deserialize)]
20#[allow(dead_code)]
21#[allow(non_snake_case)]
22struct KuCoinData {
23    time: u64,
24    symbol: String,
25    buy: Decimal,
26    sell: Decimal,
27    changeRate: Decimal,
28    changePrice: Decimal,
29    high: Decimal,
30    low: Decimal,
31    vol: Decimal,
32    volValue: Decimal,
33    last: Decimal,
34    averagePrice: Decimal,
35    takerFeeRate: Decimal,
36    makerFeeRate: Decimal,
37    takerCoefficient: Decimal,
38    makerCoefficient: Decimal,
39}
40
41#[derive(Debug, Deserialize)]
42#[allow(dead_code)]
43struct KuCoinResponse {
44    code: String,
45    data: KuCoinData,
46}
47
48impl Exchange for KuCoin {
49    async fn query_zec_to_usd(&self, client: &Client) -> Result<ExchangeData, Error> {
50        // API documentation:
51        // https://www.kucoin.com/docs/rest/spot-trading/market-data/get-24hr-stats
52        let res = client
53            .http_get_json::<KuCoinResponse>(
54                "https://api.kucoin.com/api/v1/market/stats?symbol=ZEC-USDT"
55                    .parse()
56                    .unwrap(),
57                |b| b,
58                RETRY_LIMIT,
59                retry_filter,
60            )
61            .await?;
62        let data = res.into_body().data;
63        Ok(ExchangeData {
64            bid: data.buy,
65            ask: data.sell,
66        })
67    }
68}