v_exchanges 0.17.1

Implementations of HTTP/HTTPS/WebSocket API methods for some crypto exchanges, using [crypto-botters](<https://github.com/negi-grass/crypto-botters>) framework
Documentation
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use std::collections::{BTreeMap, VecDeque};

use jiff::Timestamp;
use serde::{Deserialize, Serialize};
use serde_json::json;
use serde_with::{DisplayFromStr, serde_as};
use v_exchanges_adapters::kucoin::{KucoinHttpUrl, KucoinOption};
use v_utils::trades::{Kline, Ohlc, Pair};

use crate::{
	ExchangeResult, RequestRange, Symbol,
	core::{ExchangeInfo, Klines, PairInfo},
	kucoin::KucoinTimeframe,
};

// price {{{
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TickerResponse {
	pub code: String,
	pub data: TickerData,
}
#[serde_as]
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TickerData {
	pub time: i64,
	pub sequence: String,
	#[serde_as(as = "DisplayFromStr")]
	pub price: f64,
	#[serde_as(as = "DisplayFromStr")]
	pub size: f64,
	#[serde_as(as = "DisplayFromStr")]
	pub best_bid: f64,
	#[serde_as(as = "DisplayFromStr")]
	pub best_bid_size: f64,
	#[serde_as(as = "DisplayFromStr")]
	pub best_ask: f64,
	#[serde_as(as = "DisplayFromStr")]
	pub best_ask_size: f64,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct AllTickersResponse {
	pub code: String,
	pub data: AllTickersData,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct AllTickersData {
	pub time: i64,
	pub ticker: Vec<TickerInfo>,
}
#[serde_as]
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TickerInfo {
	pub symbol: String,
	pub symbol_name: Option<String>,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub buy: Option<f64>,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub sell: Option<f64>,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub change_rate: Option<f64>,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub change_price: Option<f64>,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub high: Option<f64>,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub low: Option<f64>,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub vol: Option<f64>,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub vol_value: Option<f64>,
	#[serde_as(as = "DisplayFromStr")]
	pub last: f64,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub average_price: Option<f64>,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub taker_fee_rate: Option<f64>,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub maker_fee_rate: Option<f64>,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub taker_coef_ficient: Option<f64>,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub maker_coef_ficient: Option<f64>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct KlineResponse {
	pub code: String,
	pub data: Vec<Vec<String>>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct SymbolsResponse {
	pub code: String,
	pub data: Vec<KucoinSymbol>,
}
#[serde_as]
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct KucoinSymbol {
	pub symbol: String,
	pub name: String,
	pub base_currency: String,
	pub quote_currency: String,
	pub fee_currency: String,
	pub market: String,
	#[serde_as(as = "DisplayFromStr")]
	pub base_min_size: f64,
	#[serde_as(as = "DisplayFromStr")]
	pub quote_min_size: f64,
	#[serde_as(as = "DisplayFromStr")]
	pub base_max_size: f64,
	#[serde_as(as = "DisplayFromStr")]
	pub quote_max_size: f64,
	#[serde_as(as = "DisplayFromStr")]
	pub base_increment: f64,
	#[serde_as(as = "DisplayFromStr")]
	pub quote_increment: f64,
	#[serde_as(as = "DisplayFromStr")]
	pub price_increment: f64,
	pub price_limit_rate: Option<String>,
	#[serde_as(as = "Option<DisplayFromStr>")]
	pub min_funds: Option<f64>,
	pub is_margin_enabled: bool,
	pub enable_trading: bool,
}
pub mod futures {
	use std::collections::{BTreeMap, VecDeque};

	use jiff::Timestamp;
	use serde::{Deserialize, Serialize};
	use serde_json::json;
	use serde_with::{DisplayFromStr, serde_as};
	use v_exchanges_adapters::kucoin::{KucoinHttpUrl, KucoinOption};
	use v_utils::trades::{Kline, Ohlc, Pair};

	use crate::{
		ExchangeResult, RequestRange, Symbol,
		core::{ExchangeInfo, Klines, PairInfo},
		kucoin::KucoinTimeframe,
	};

	/// Kucoin futures uses XBT instead of BTC
	fn to_kucoin_futures_base(base: &str) -> &str {
		match base {
			"BTC" => "XBT",
			other => other,
		}
	}

	/// Convert Kucoin futures base currency back to standard format
	fn from_kucoin_futures_base(base: &str) -> &str {
		match base {
			"XBT" => "BTC",
			other => other,
		}
	}

	// price {{{
	pub(in crate::kucoin) async fn price(client: &v_exchanges_adapters::Client, pair: Pair, _recv_window: Option<std::time::Duration>) -> ExchangeResult<f64> {
		// Kucoin futures symbol format: XBTUSDTM (base + quote + "M" for perpetual)
		let base = to_kucoin_futures_base(pair.base().as_ref());
		let symbol = format!("{base}{}M", pair.quote());
		let params = json!({
			"symbol": symbol,
		});
		let options = vec![KucoinOption::HttpUrl(KucoinHttpUrl::Futures)];
		let response: FuturesTickerResponse = client.get("/api/v1/ticker", &params, options).await?;
		Ok(response.data.price)
	}

	#[derive(Debug, Deserialize, Serialize)]
	#[serde(rename_all = "camelCase")]
	pub struct FuturesTickerResponse {
		pub code: String,
		pub data: FuturesTickerData,
	}

	#[serde_as]
	#[derive(Debug, Deserialize, Serialize)]
	#[serde(rename_all = "camelCase")]
	pub struct FuturesTickerData {
		pub sequence: i64,
		pub symbol: String,
		pub side: String,
		pub size: i64,
		pub trade_id: String,
		#[serde_as(as = "DisplayFromStr")]
		pub price: f64,
		#[serde_as(as = "DisplayFromStr")]
		pub best_bid_price: f64,
		pub best_bid_size: i64,
		#[serde_as(as = "DisplayFromStr")]
		pub best_ask_price: f64,
		pub best_ask_size: i64,
		pub ts: i64,
	}
	//,}}}

	// prices {{{
	pub(in crate::kucoin) async fn prices(client: &v_exchanges_adapters::Client, pairs: Option<Vec<Pair>>, _recv_window: Option<std::time::Duration>) -> ExchangeResult<BTreeMap<Pair, f64>> {
		let options = vec![KucoinOption::HttpUrl(KucoinHttpUrl::Futures)];
		let response: ContractsActiveResponse = client.get("/api/v1/contracts/active", &json!({}), options).await?;

		let mut price_map = BTreeMap::new();

		for contract in response.data {
			// Parse symbol: XBTUSDTM -> BTC-USDT
			let symbol = &contract.symbol;
			if !symbol.ends_with('M') {
				continue;
			}

			// Convert XBT -> BTC
			let base = from_kucoin_futures_base(&contract.base_currency);
			let pair = Pair::new(base, contract.quote_currency.as_str());

			// If pairs filter is specified, only include those pairs
			if let Some(ref requested_pairs) = pairs
				&& !requested_pairs.contains(&pair)
			{
				continue;
			}

			price_map.insert(pair, contract.last_trade_price);
		}

		Ok(price_map)
	}

	#[derive(Debug, Deserialize, Serialize)]
	pub struct ContractsActiveResponse {
		pub code: String,
		pub data: Vec<ContractInfo>,
	}

	#[derive(Debug, Deserialize, Serialize)]
	#[serde(rename_all = "camelCase")]
	pub struct ContractInfo {
		pub symbol: String,
		pub base_currency: String,
		pub quote_currency: String,
		pub settle_currency: String,
		#[serde(rename = "type")]
		pub contract_type: String,
		pub status: String,
		pub multiplier: f64,
		pub tick_size: f64,
		pub lot_size: f64,
		pub max_leverage: i32,
		pub last_trade_price: f64,
	}
	//,}}}

	// klines {{{
	pub(in crate::kucoin) async fn klines(
		client: &v_exchanges_adapters::Client,
		symbol: Symbol,
		tf: KucoinTimeframe,
		range: RequestRange,
		_recv_window: Option<std::time::Duration>,
	) -> ExchangeResult<Klines> {
		// Kucoin futures symbol format: XBTUSDTM
		let base = to_kucoin_futures_base(symbol.pair.base().as_ref());
		let kucoin_symbol = format!("{base}{}M", symbol.pair.quote());

		// granularity is in minutes for futures API
		let granularity = (tf.duration().as_secs() / 60) as u32;

		let (from_ts, to_ts) = match range {
			RequestRange::Span { since, until } => {
				let start = since.as_millisecond();
				let end = until.map(|t| t.as_millisecond()).unwrap_or_else(|| Timestamp::now().as_millisecond());
				(start, end)
			}
			RequestRange::Limit(_) => {
				let end = Timestamp::now();
				let start = end - tf.duration() * 200; // Futures API returns max 200 candles
				(start.as_millisecond(), end.as_millisecond())
			}
		};

		let params = json!({
			"symbol": kucoin_symbol,
			"granularity": granularity,
			"from": from_ts,
			"to": to_ts,
		});

		let options = vec![KucoinOption::HttpUrl(KucoinHttpUrl::Futures)];
		let response: FuturesKlineResponse = client.get("/api/v1/kline/query", &params, options).await?;

		let mut klines_vec = VecDeque::new();

		// Futures klines: [timestamp_ms, open, high, low, close, volume, turnover]
		for kline_data in response.data {
			if kline_data.len() >= 7 {
				let timestamp_ms = kline_data[0] as i64;

				let ohlc = Ohlc {
					open: kline_data[1],
					high: kline_data[2],
					low: kline_data[3],
					close: kline_data[4],
				};

				klines_vec.push_back(Kline {
					open_time: Timestamp::from_millisecond(timestamp_ms).map_err(|e| eyre::eyre!("Invalid timestamp: {e}"))?,
					ohlc,
					volume_quote: kline_data[6],
					trades: None,
					taker_buy_volume_quote: None,
				});
			}
		}

		Ok(Klines::new(klines_vec, *tf))
	}

	#[derive(Debug, Deserialize, Serialize)]
	pub struct FuturesKlineResponse {
		pub code: String,
		pub data: Vec<Vec<f64>>,
	}
	//,}}}

	// exchange_info {{{
	pub(in crate::kucoin) async fn exchange_info(client: &v_exchanges_adapters::Client, _recv_window: Option<std::time::Duration>) -> ExchangeResult<ExchangeInfo> {
		let options = vec![KucoinOption::HttpUrl(KucoinHttpUrl::Futures)];
		let response: ContractsActiveResponse = client.get("/api/v1/contracts/active", &json!({}), options).await?;

		let mut pairs = BTreeMap::new();

		for contract in response.data {
			// Only include active contracts
			if contract.status != "Open" {
				continue;
			}

			// Convert XBT -> BTC
			let base = from_kucoin_futures_base(&contract.base_currency);
			let pair = Pair::new(base, contract.quote_currency.as_str());

			// Calculate price precision from tick_size
			let price_precision = if contract.tick_size == 0.0 {
				0
			} else {
				(-contract.tick_size.log10()).max(0.0).round() as u8
			};

			let pair_info = PairInfo { price_precision, delivery_date: None };
			pairs.insert(pair, pair_info);
		}

		Ok(ExchangeInfo {
			server_time: Timestamp::now(),
			pairs,
		})
	}
	//,}}}
}
pub(super) async fn price(client: &v_exchanges_adapters::Client, pair: Pair, _recv_window: Option<std::time::Duration>) -> ExchangeResult<f64> {
	let symbol = format!("{}-{}", pair.base(), pair.quote());
	let params = json!({
		"symbol": symbol,
	});
	let options = vec![KucoinOption::HttpUrl(KucoinHttpUrl::Spot)];
	let response: TickerResponse = client.get("/api/v1/market/orderbook/level1", &params, options).await?;
	Ok(response.data.price)
}

//,}}}

// prices {{{
pub(super) async fn prices(client: &v_exchanges_adapters::Client, pairs: Option<Vec<Pair>>, _recv_window: Option<std::time::Duration>) -> ExchangeResult<BTreeMap<Pair, f64>> {
	let options = vec![KucoinOption::HttpUrl(KucoinHttpUrl::Spot)];
	let response: AllTickersResponse = client.get("/api/v1/market/allTickers", &json!({}), options).await?;

	let mut price_map = BTreeMap::new();

	for ticker in response.data.ticker {
		// Parse Kucoin symbol format (e.g., "BTC-USDT" -> Pair)
		if let Some((base, quote)) = ticker.symbol.split_once('-') {
			let pair = Pair::new(base, quote);

			// If pairs filter is specified, only include those pairs
			if let Some(ref requested_pairs) = pairs
				&& !requested_pairs.contains(&pair)
			{
				continue;
			}

			price_map.insert(pair, ticker.last);
		}
	}

	Ok(price_map)
}

//,}}}

// klines {{{
pub(super) async fn klines(
	client: &v_exchanges_adapters::Client,
	symbol: Symbol,
	tf: KucoinTimeframe,
	range: RequestRange,
	_recv_window: Option<std::time::Duration>,
) -> ExchangeResult<Klines> {
	let kucoin_symbol = format!("{}-{}", symbol.pair.base(), symbol.pair.quote());

	// Convert from v_utils format (1h, 1d, 1w) to Kucoin API format (1hour, 1day, 1week)
	let tf_str = tf.to_string();
	let type_param = tf_str.replace("m", "min").replace("h", "hour").replace("d", "day").replace("w", "week");

	let mut params = vec![("symbol", kucoin_symbol.as_str()), ("type", type_param.as_str())];

	let (start_at, end_at) = match range {
		RequestRange::Span { since, until } => {
			let start = since.as_second().to_string();
			let end = until.map(|t| t.as_second().to_string()).unwrap_or_else(|| Timestamp::now().as_second().to_string());
			(start, end)
		}
		RequestRange::Limit(_) => {
			// Kucoin doesn't support limit directly, so we'll use a large time range
			let end = Timestamp::now();
			let start = end - tf.duration() * 1500; // Max 1500 candles
			(start.as_second().to_string(), end.as_second().to_string())
		}
	};

	params.push(("startAt", &start_at));
	params.push(("endAt", &end_at));

	let options = vec![KucoinOption::HttpUrl(KucoinHttpUrl::Spot)];
	let response: KlineResponse = client.get("/api/v1/market/candles", &params, options).await?;

	let mut klines_vec = VecDeque::new();

	// Kucoin returns klines in descending order (newest first), so we need to reverse
	for kline_data in response.data.iter().rev() {
		// kline_data format: [time, open, close, high, low, volume, turnover]
		if kline_data.len() >= 7 {
			let timestamp_str = &kline_data[0];
			let timestamp_secs: i64 = timestamp_str.parse().map_err(|e| eyre::eyre!("Failed to parse timestamp: {e}"))?;

			let ohlc = Ohlc {
				open: kline_data[1].parse().map_err(|e| eyre::eyre!("Failed to parse open: {e}"))?,
				high: kline_data[3].parse().map_err(|e| eyre::eyre!("Failed to parse high: {e}"))?,
				low: kline_data[4].parse().map_err(|e| eyre::eyre!("Failed to parse low: {e}"))?,
				close: kline_data[2].parse().map_err(|e| eyre::eyre!("Failed to parse close: {e}"))?,
			};

			klines_vec.push_back(Kline {
				open_time: Timestamp::from_second(timestamp_secs).map_err(|e| eyre::eyre!("Invalid timestamp: {e}"))?,
				ohlc,
				volume_quote: kline_data[6].parse().map_err(|e| eyre::eyre!("Failed to parse turnover: {e}"))?,
				trades: None,
				taker_buy_volume_quote: None,
			});
		}
	}

	Ok(Klines::new(klines_vec, *tf))
}

//,}}}

// exchange_info {{{
pub(super) async fn exchange_info(client: &v_exchanges_adapters::Client, _recv_window: Option<std::time::Duration>) -> ExchangeResult<ExchangeInfo> {
	let options = vec![KucoinOption::HttpUrl(KucoinHttpUrl::Spot)];
	let response: SymbolsResponse = client.get("/api/v2/symbols", &json!({}), options).await?;

	let mut pairs = BTreeMap::new();

	for symbol in response.data {
		// Only include enabled trading pairs
		if symbol.enable_trading
			&& let Some((base, quote)) = symbol.symbol.split_once('-')
		{
			let pair = Pair::new(base, quote);
			// Calculate price precision from priceIncrement
			// e.g., 0.0001 -> 4, 0.001 -> 3, 1.0 -> 0
			let price_precision = if symbol.price_increment == 0.0 {
				0
			} else {
				(-symbol.price_increment.log10()).max(0.0).round() as u8
			};
			let pair_info = PairInfo { price_precision, delivery_date: None };
			pairs.insert(pair, pair_info);
		}
	}

	Ok(ExchangeInfo {
		server_time: Timestamp::now(), // Kucoin doesn't return server time in this endpoint
		pairs,
	})
}

//,}}}

// ============================================================================
// Futures Market Data
// ============================================================================