v_exchanges_adapters 0.19.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
#![cfg_attr(docsrs, feature(doc_cfg))]
#![feature(default_field_values)]
#![feature(duration_constructors)]
#![feature(try_blocks)]
pub extern crate v_exchanges_api_generics as generics;
use std::sync::Arc;

pub use exchanges::*;
use serde::Serialize;
use traits::*;
use ustr::Ustr;
use v_exchanges_api_generics::{
	RateLimiter,
	http::{self, *},
	ratelimiter::clock::MonotonicClock,
	ws::*,
};

mod exchanges;
pub mod traits;

// very long type, make it a macro
macro_rules! request_ret {
    ($lt:lifetime, $Response:ty, $Options:ty,  $Body:ty) => {
        Result<
            <<$Options as HttpOption<$lt, $Response, $Body>>::RequestHandler as RequestHandler<$Body>>::Successful,
            RequestError,
        >
    };
}

/// Core HTTP transport interface.
pub trait HttpClient {
	fn http_client(&self) -> &http::Client;
	fn http_client_mut(&mut self) -> &mut http::Client;
}
pub trait GetOptions<O: HandlerOptions> {
	fn default_options(&self) -> &O;
	fn default_options_mut(&mut self) -> &mut O;
	fn is_authenticated(&self) -> bool {
		self.default_options().is_authenticated()
	}
}
#[derive(Clone, Debug)]
pub enum Client {
	True(ClientInner),
	Mock(ClientInner),
}
impl Client {
	fn inner(&self) -> &ClientInner {
		match self {
			Client::True(inner) | Client::Mock(inner) => inner,
		}
	}

	fn inner_mut(&mut self) -> &mut ClientInner {
		match self {
			Client::True(inner) | Client::Mock(inner) => inner,
		}
	}

	pub fn new_mock() -> Self {
		let mut inner = ClientInner::default();
		inner.client.config.mock_cache_dir = Some(v_utils::xdg_cache_dir!("mock_calls"));
		Client::Mock(inner)
	}

	/// Sets the rate limiter for this client.
	///
	/// The rate limiter is shared across clones of this client (Arc). After calling this, the
	/// limiter is also wired into the underlying `http::Client` so that `request()` calls
	/// automatically wait on it.
	pub fn set_rate_limiter(&mut self, rl: RateLimiter<Ustr, MonotonicClock>) {
		let rl = Arc::new(rl);
		self.inner_mut().client.rate_limiter = Some(Arc::clone(&rl));
		self.inner_mut().rate_limiter = rl;
	}

	/// Update the default options for this [Client]
	pub fn update_default_option<O>(&mut self, option: O)
	where
		O: HandlerOption,
		Self: GetOptions<O::Options>, {
		self.default_options_mut().update(option);
	}

	pub fn is_authenticated<O>(&self) -> bool
	where
		O: HandlerOption,
		Self: GetOptions<O::Options>, {
		self.default_options().is_authenticated()
	}

	#[inline]
	fn merged_options<O>(&self, options: impl IntoIterator<Item = O>) -> O::Options
	where
		O: HandlerOption,
		Self: GetOptions<O::Options>, {
		let mut default_options = self.default_options().clone();
		for option in options {
			default_options.update(option);
		}
		default_options
	}

	/// see [http::Client::request()]
	pub async fn request<'a, R, O, Q, B>(&self, method: Method, url: &str, query: Option<&Q>, body: Option<B>, options: impl IntoIterator<Item = O>) -> request_ret!('a, R, O, B)
	where
		O: HttpOption<'a, R, B>,
		O::RequestHandler: RequestHandler<B>,
		Self: GetOptions<O::Options>,
		Q: Serialize + ?Sized + std::fmt::Debug, {
		self.http_client().request(method, url, query, body, &O::request_handler(self.merged_options(options))).await
	}

	/// see [http::Client::get()]
	pub async fn get<'a, R, O, Q>(&self, url: &str, query: &Q, options: impl IntoIterator<Item = O>) -> request_ret!('a, R, O, ())
	where
		O: HttpOption<'a, R, ()>,
		O::RequestHandler: RequestHandler<()>,
		Self: GetOptions<O::Options>,
		Q: Serialize + ?Sized + std::fmt::Debug, {
		self.http_client().get(url, query, &O::request_handler(self.merged_options(options))).await
	}

	/// see [http::Client::get_no_query()]
	pub async fn get_no_query<'a, R, O>(&self, url: &str, options: impl IntoIterator<Item = O>) -> request_ret!('a, R, O, ())
	where
		O: HttpOption<'a, R, ()>,
		O::RequestHandler: RequestHandler<()>,
		Self: GetOptions<O::Options>, {
		self.http_client().get_no_query(url, &O::request_handler(self.merged_options(options))).await
	}

	/// see [http::Client::post()]
	pub async fn post<'a, R, O, B>(&self, url: &str, body: B, options: impl IntoIterator<Item = O>) -> request_ret!('a, R, O, B)
	where
		O: HttpOption<'a, R, B>,
		O::RequestHandler: RequestHandler<B>,
		Self: GetOptions<O::Options>, {
		self.http_client().post(url, body, &O::request_handler(self.merged_options(options))).await
	}

	/// see [http::Client::post_no_body()]
	pub async fn post_no_body<'a, R, O>(&self, url: &str, options: impl IntoIterator<Item = O>) -> request_ret!('a, R, O, ())
	where
		O: HttpOption<'a, R, ()>,
		O::RequestHandler: RequestHandler<()>,
		Self: GetOptions<O::Options>, {
		self.http_client().post_no_body(url, &O::request_handler(self.merged_options(options))).await
	}

	/// see [http::Client::put()]
	pub async fn put<'a, R, O, B>(&self, url: &str, body: B, options: impl IntoIterator<Item = O>) -> request_ret!('a, R, O, B)
	where
		O: HttpOption<'a, R, B>,
		O::RequestHandler: RequestHandler<B>,
		Self: GetOptions<O::Options>, {
		self.http_client().put(url, body, &O::request_handler(self.merged_options(options))).await
	}

	/// see [http::Client::put_no_body()]
	pub async fn put_no_body<'a, R, O>(&self, url: &str, options: impl IntoIterator<Item = O>) -> request_ret!('a, R, O, ())
	where
		O: HttpOption<'a, R, ()>,
		O::RequestHandler: RequestHandler<()>,
		Self: GetOptions<O::Options>, {
		self.http_client().put_no_body(url, &O::request_handler(self.merged_options(options))).await
	}

	/// see [http::Client::delete()]
	pub async fn delete<'a, R, O, Q>(&self, url: &str, query: &Q, options: impl IntoIterator<Item = O>) -> request_ret!('a, R, O, ())
	where
		O: HttpOption<'a, R, ()>,
		O::RequestHandler: RequestHandler<()>,
		Self: GetOptions<O::Options>,
		Q: Serialize + ?Sized + std::fmt::Debug, {
		self.http_client().delete(url, query, &O::request_handler(self.merged_options(options))).await
	}

	/// see [http::Client::delete_no_query()]
	pub async fn delete_no_query<'a, R, O>(&self, url: &str, options: impl IntoIterator<Item = O>) -> request_ret!('a, R, O, ())
	where
		O: HttpOption<'a, R, ()>,
		O::RequestHandler: RequestHandler<()>,
		Self: GetOptions<O::Options>, {
		self.http_client().delete_no_query(url, &O::request_handler(self.merged_options(options))).await
	}

	pub fn ws_connection<O>(&self, url: &str, options: impl IntoIterator<Item = O>) -> Result<WsConnection<O::WsHandler>, WsError>
	where
		O: WsOption,
		O::WsHandler: WsHandler,
		Self: GetOptions<O::Options>, {
		WsConnection::try_new(url, O::ws_handler(self.merged_options(options)))
	}
}

#[derive(Clone, Debug)]
pub struct ClientInner {
	pub client: http::Client,
	/// Rate limiter shared across clones (same exchange instance).
	pub rate_limiter: Arc<RateLimiter<Ustr, MonotonicClock>>,
	#[cfg(feature = "binance")]
	binance: binance::BinanceOptions,
	#[cfg(feature = "bitflyer")]
	bitflyer: bitflyer::BitFlyerOptions,
	#[cfg(feature = "bybit")]
	bybit: bybit::BybitOptions,
	#[cfg(feature = "coincheck")]
	coincheck: coincheck::CoincheckOptions,
	#[cfg(feature = "kucoin")]
	kucoin: kucoin::KucoinOptions,
	#[cfg(feature = "mexc")]
	mexc: mexc::MexcOptions,
}
impl Default for ClientInner {
	fn default() -> Self {
		Self {
			client: http::Client::default(),
			rate_limiter: Arc::new(RateLimiter::new_with_quota(None, vec![])),
			#[cfg(feature = "binance")]
			binance: binance::BinanceOptions::default(),
			#[cfg(feature = "bitflyer")]
			bitflyer: bitflyer::BitFlyerOptions::default(),
			#[cfg(feature = "bybit")]
			bybit: bybit::BybitOptions::default(),
			#[cfg(feature = "coincheck")]
			coincheck: coincheck::CoincheckOptions::default(),
			#[cfg(feature = "kucoin")]
			kucoin: kucoin::KucoinOptions::default(),
			#[cfg(feature = "mexc")]
			mexc: mexc::MexcOptions::default(),
		}
	}
}
impl Default for Client {
	fn default() -> Self {
		Client::True(ClientInner::default())
	}
}

impl HttpClient for ClientInner {
	fn http_client(&self) -> &http::Client {
		&self.client
	}

	fn http_client_mut(&mut self) -> &mut http::Client {
		&mut self.client
	}
}

impl HttpClient for Client {
	fn http_client(&self) -> &http::Client {
		&self.inner().client
	}

	fn http_client_mut(&mut self) -> &mut http::Client {
		&mut self.inner_mut().client
	}
}

// GetOptions impls for ClientInner {{{
#[cfg(feature = "binance")]
#[cfg_attr(docsrs, doc(cfg(feature = "binance")))]
impl GetOptions<binance::BinanceOptions> for ClientInner {
	fn default_options(&self) -> &binance::BinanceOptions {
		&self.binance
	}

	fn default_options_mut(&mut self) -> &mut binance::BinanceOptions {
		&mut self.binance
	}
}

#[cfg(feature = "bitflyer")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitflyer")))]
impl GetOptions<bitflyer::BitFlyerOptions> for ClientInner {
	fn default_options(&self) -> &bitflyer::BitFlyerOptions {
		&self.bitflyer
	}

	fn default_options_mut(&mut self) -> &mut bitflyer::BitFlyerOptions {
		&mut self.bitflyer
	}
}

#[cfg(feature = "bybit")]
#[cfg_attr(docsrs, doc(cfg(feature = "bybit")))]
impl GetOptions<bybit::BybitOptions> for ClientInner {
	fn default_options(&self) -> &bybit::BybitOptions {
		&self.bybit
	}

	fn default_options_mut(&mut self) -> &mut bybit::BybitOptions {
		&mut self.bybit
	}
}

#[cfg(feature = "coincheck")]
#[cfg_attr(docsrs, doc(cfg(feature = "coincheck")))]
impl GetOptions<coincheck::CoincheckOptions> for ClientInner {
	fn default_options(&self) -> &coincheck::CoincheckOptions {
		&self.coincheck
	}

	fn default_options_mut(&mut self) -> &mut coincheck::CoincheckOptions {
		&mut self.coincheck
	}
}
#[cfg(feature = "kucoin")]
#[cfg_attr(docsrs, doc(cfg(feature = "kucoin")))]
impl GetOptions<kucoin::KucoinOptions> for ClientInner {
	fn default_options(&self) -> &kucoin::KucoinOptions {
		&self.kucoin
	}

	fn default_options_mut(&mut self) -> &mut kucoin::KucoinOptions {
		&mut self.kucoin
	}
}
#[cfg(feature = "mexc")]
#[cfg_attr(docsrs, doc(cfg(feature = "mexc")))]
impl GetOptions<mexc::MexcOptions> for ClientInner {
	fn default_options(&self) -> &mexc::MexcOptions {
		&self.mexc
	}

	fn default_options_mut(&mut self) -> &mut mexc::MexcOptions {
		&mut self.mexc
	}
}
//,}}}

// GetOptions impls for Client: delegate to inner {{{
#[cfg(feature = "binance")]
#[cfg_attr(docsrs, doc(cfg(feature = "binance")))]
impl GetOptions<binance::BinanceOptions> for Client {
	fn default_options(&self) -> &binance::BinanceOptions {
		self.inner().default_options()
	}

	fn default_options_mut(&mut self) -> &mut binance::BinanceOptions {
		self.inner_mut().default_options_mut()
	}
}

#[cfg(feature = "bitflyer")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitflyer")))]
impl GetOptions<bitflyer::BitFlyerOptions> for Client {
	fn default_options(&self) -> &bitflyer::BitFlyerOptions {
		self.inner().default_options()
	}

	fn default_options_mut(&mut self) -> &mut bitflyer::BitFlyerOptions {
		self.inner_mut().default_options_mut()
	}
}

#[cfg(feature = "bybit")]
#[cfg_attr(docsrs, doc(cfg(feature = "bybit")))]
impl GetOptions<bybit::BybitOptions> for Client {
	fn default_options(&self) -> &bybit::BybitOptions {
		self.inner().default_options()
	}

	fn default_options_mut(&mut self) -> &mut bybit::BybitOptions {
		self.inner_mut().default_options_mut()
	}
}

#[cfg(feature = "coincheck")]
#[cfg_attr(docsrs, doc(cfg(feature = "coincheck")))]
impl GetOptions<coincheck::CoincheckOptions> for Client {
	fn default_options(&self) -> &coincheck::CoincheckOptions {
		self.inner().default_options()
	}

	fn default_options_mut(&mut self) -> &mut coincheck::CoincheckOptions {
		self.inner_mut().default_options_mut()
	}
}
#[cfg(feature = "kucoin")]
#[cfg_attr(docsrs, doc(cfg(feature = "kucoin")))]
impl GetOptions<kucoin::KucoinOptions> for Client {
	fn default_options(&self) -> &kucoin::KucoinOptions {
		self.inner().default_options()
	}

	fn default_options_mut(&mut self) -> &mut kucoin::KucoinOptions {
		self.inner_mut().default_options_mut()
	}
}
#[cfg(feature = "mexc")]
#[cfg_attr(docsrs, doc(cfg(feature = "mexc")))]
impl GetOptions<mexc::MexcOptions> for Client {
	fn default_options(&self) -> &mexc::MexcOptions {
		self.inner().default_options()
	}

	fn default_options_mut(&mut self) -> &mut mexc::MexcOptions {
		self.inner_mut().default_options_mut()
	}
}
//,}}}