|
| 1 | +use std::collections::{BTreeMap, VecDeque}; |
| 2 | + |
1 | 3 | use adapters::{ |
2 | 4 | Client, |
3 | 5 | mexc::{MexcHttpUrl, MexcOption}, |
4 | 6 | }; |
5 | | -use v_utils::prelude::*; |
| 7 | +use jiff::Timestamp; |
| 8 | +use serde_json::json; |
| 9 | +use v_utils::{ |
| 10 | + prelude::*, |
| 11 | + trades::{Kline, Ohlc}, |
| 12 | +}; |
6 | 13 |
|
7 | | -use crate::ExchangeResult; |
| 14 | +use crate::{ |
| 15 | + ExchangeResult, RequestRange, Symbol, |
| 16 | + core::{ExchangeInfo, Klines, PairInfo}, |
| 17 | + mexc::MexcTimeframe, |
| 18 | +}; |
8 | 19 |
|
9 | 20 | //TODO: impl spot |
10 | 21 | pub async fn price(client: &Client, pair: Pair) -> ExchangeResult<f64> { |
11 | 22 | let endpoint = format!("/api/v1/contract/index_price/{}", pair.fmt_mexc()); |
12 | 23 | let options = vec![MexcOption::HttpUrl(MexcHttpUrl::Futures)]; |
13 | | - let r: PriceResponse = client.get_no_query(&endpoint, options).await.unwrap(); |
| 24 | + let r: PriceResponse = client.get_no_query(&endpoint, options).await?; |
14 | 25 | Ok(r.data.into()) |
15 | 26 | } |
16 | 27 |
|
17 | | -#[allow(unused)] |
18 | | -#[derive(Clone, Debug, Default, Deserialize, derive_new::new)] |
| 28 | +#[derive(Clone, Debug, Default, Deserialize)] |
19 | 29 | struct PriceResponse { |
20 | | - pub code: i32, |
21 | 30 | pub data: PriceData, |
22 | | - pub success: bool, |
23 | 31 | } |
24 | 32 |
|
25 | | -#[allow(unused)] |
26 | 33 | #[derive(Clone, Debug, Default, Deserialize)] |
27 | 34 | #[serde(rename_all = "camelCase")] |
28 | 35 | struct PriceData { |
29 | 36 | index_price: f64, |
30 | | - symbol: String, |
31 | | - timestamp: i64, |
32 | 37 | } |
33 | 38 | impl From<PriceData> for f64 { |
34 | 39 | fn from(data: PriceData) -> f64 { |
35 | 40 | data.index_price |
36 | 41 | } |
37 | 42 | } |
| 43 | + |
| 44 | +// klines {{{ |
| 45 | +pub async fn klines(client: &Client, symbol: Symbol, tf: MexcTimeframe, range: RequestRange) -> ExchangeResult<Klines> { |
| 46 | + let mexc_symbol = symbol.pair.fmt_mexc(); |
| 47 | + |
| 48 | + // Convert timeframe to Mexc format: 1m -> Min1, 5m -> Min5, 1h -> Min60, 4h -> Hour4, 1d -> Day1 |
| 49 | + let tf_str = tf.to_string(); |
| 50 | + let interval = match tf_str.as_str() { |
| 51 | + "1m" => "Min1", |
| 52 | + "5m" => "Min5", |
| 53 | + "15m" => "Min15", |
| 54 | + "30m" => "Min30", |
| 55 | + "60m" => "Min60", |
| 56 | + "4h" => "Hour4", |
| 57 | + "1d" => "Day1", |
| 58 | + "1W" => "Week1", |
| 59 | + "1M" => "Month1", |
| 60 | + _ => return Err(eyre::eyre!("Unsupported timeframe: {}", tf_str).into()), |
| 61 | + }; |
| 62 | + |
| 63 | + let (start, end) = match range { |
| 64 | + RequestRange::Span { since, until } => { |
| 65 | + let s = since.as_second(); |
| 66 | + let e = until.map(|t| t.as_second()).unwrap_or_else(|| Timestamp::now().as_second()); |
| 67 | + (s, e) |
| 68 | + } |
| 69 | + RequestRange::Limit(n) => { |
| 70 | + let end = Timestamp::now(); |
| 71 | + let start = end - tf.duration() * n as u32; |
| 72 | + (start.as_second(), end.as_second()) |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + let endpoint = format!("/api/v1/contract/kline/{}", mexc_symbol); |
| 77 | + let params = json!({ |
| 78 | + "interval": interval, |
| 79 | + "start": start, |
| 80 | + "end": end, |
| 81 | + }); |
| 82 | + let options = vec![MexcOption::HttpUrl(MexcHttpUrl::Futures)]; |
| 83 | + let response: KlineResponse = client.get(&endpoint, ¶ms, options).await?; |
| 84 | + |
| 85 | + let mut klines_vec = VecDeque::new(); |
| 86 | + let data = response.data; |
| 87 | + |
| 88 | + // Mexc returns separate arrays for each field |
| 89 | + for i in 0..data.time.len() { |
| 90 | + let ohlc = Ohlc { |
| 91 | + open: data.open[i], |
| 92 | + high: data.high[i], |
| 93 | + low: data.low[i], |
| 94 | + close: data.close[i], |
| 95 | + }; |
| 96 | + |
| 97 | + klines_vec.push_back(Kline { |
| 98 | + open_time: Timestamp::from_second(data.time[i]).map_err(|e| eyre::eyre!("Invalid timestamp: {}", e))?, |
| 99 | + ohlc, |
| 100 | + volume_quote: data.amount[i], |
| 101 | + trades: None, |
| 102 | + taker_buy_volume_quote: None, |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + Ok(Klines::new(klines_vec, *tf)) |
| 107 | +} |
| 108 | + |
| 109 | +#[derive(Debug, Deserialize)] |
| 110 | +struct KlineResponse { |
| 111 | + data: KlineData, |
| 112 | +} |
| 113 | + |
| 114 | +#[derive(Debug, Deserialize)] |
| 115 | +struct KlineData { |
| 116 | + time: Vec<i64>, |
| 117 | + open: Vec<f64>, |
| 118 | + close: Vec<f64>, |
| 119 | + high: Vec<f64>, |
| 120 | + low: Vec<f64>, |
| 121 | + vol: Vec<f64>, |
| 122 | + amount: Vec<f64>, |
| 123 | +} |
| 124 | +//,}}} |
| 125 | + |
| 126 | +// exchange_info {{{ |
| 127 | +pub async fn exchange_info(client: &Client) -> ExchangeResult<ExchangeInfo> { |
| 128 | + let options = vec![MexcOption::HttpUrl(MexcHttpUrl::Futures)]; |
| 129 | + let response: ContractDetailResponse = client.get_no_query("/api/v1/contract/detail", options).await?; |
| 130 | + |
| 131 | + let mut pairs = BTreeMap::new(); |
| 132 | + |
| 133 | + for contract in response.data { |
| 134 | + // state 0 = active |
| 135 | + if contract.state != 0 { |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + let pair = Pair::new(contract.base_coin.as_str(), contract.quote_coin.as_str()); |
| 140 | + |
| 141 | + // priceScale is number of decimal places |
| 142 | + let price_precision = contract.price_scale as u8; |
| 143 | + |
| 144 | + let pair_info = PairInfo { price_precision }; |
| 145 | + pairs.insert(pair, pair_info); |
| 146 | + } |
| 147 | + |
| 148 | + Ok(ExchangeInfo { |
| 149 | + server_time: Timestamp::now(), |
| 150 | + pairs, |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | +#[derive(Debug, Deserialize)] |
| 155 | +struct ContractDetailResponse { |
| 156 | + data: Vec<ContractInfo>, |
| 157 | +} |
| 158 | + |
| 159 | +#[derive(Debug, Deserialize)] |
| 160 | +#[serde(rename_all = "camelCase")] |
| 161 | +struct ContractInfo { |
| 162 | + symbol: String, |
| 163 | + base_coin: String, |
| 164 | + quote_coin: String, |
| 165 | + price_scale: i32, |
| 166 | + state: i32, |
| 167 | +} |
| 168 | +//,}}} |
0 commit comments