maxt 0.2.1

One Rust API for Upbit, Bithumb, Binance, and Hyperliquid market data, accounts, and orders.
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
//! The common API.

use crate::adapter::Adapter;
use crate::error::Result;
use crate::feature::Feature;
use crate::request::{
    CancelOrdersRequest, CandleRequest, DepositAddressRequest, HistoryRequest, MarginRequest,
    OrderHistoryRequest, OrderLookupRequest, OrderRequest, TransferHistoryRequest,
    TransferLookupRequest, WithdrawRequest,
};
use crate::stream::{AccountStream, MarketStream};
use crate::types::{
    AssetNetwork, Balance, CancelOrdersResult, Candle, Deposit, DepositAddress,
    DepositAddressEntry, Exchange, FundingPayment, FundingRate, MarginSummary, Market, MarketInfo,
    MarketKind, Order, OrderBook, OrderRules, Page, Position, StreamConfig, Subscription, Ticker,
    Trade, Withdrawal, WithdrawalQuote,
};

/// The common API over one exchange adapter.
///
/// Exchange-specific operations are available through [`Client::adapter`].
#[derive(Debug, Clone)]
pub struct Client<A> {
    adapter: A,
}

impl<A: Adapter> Client<A> {
    /// Wraps an adapter.
    ///
    /// Credentials are configured on the adapter before it is wrapped.
    pub fn new(adapter: A) -> Self {
        Self { adapter }
    }

    /// Which exchange this client talks to.
    ///
    /// This identifies the exchange, not an exchange-specific venue.
    pub fn exchange(&self) -> Exchange {
        self.adapter.exchange()
    }

    /// Whether this configured client offers a feature.
    ///
    /// Credential-dependent features return `false` until credentials are
    /// configured. A call may still fail for request-specific validation or an
    /// exchange-side rejection.
    pub fn supports(&self, feature: Feature) -> bool {
        self.adapter.supports(feature)
    }

    /// The underlying adapter, for exchange-specific operations.
    pub fn adapter(&self) -> &A {
        &self.adapter
    }

    /// Unwraps this client and returns its adapter.
    pub fn into_adapter(self) -> A {
        self.adapter
    }

    /// Lists the exchange's markets of one kind.
    ///
    /// A venue that lists none of the requested kind returns an empty vector.
    pub async fn markets(&self, kind: MarketKind) -> Result<Vec<MarketInfo>> {
        self.adapter.markets(kind).await
    }

    /// Reads the most recent trades on a market, newest first.
    ///
    /// `limit` caps the number returned. `None` uses the exchange default.
    ///
    /// # Errors
    ///
    /// Built-in adapters return
    /// [`Error::InvalidRequest`](crate::Error::InvalidRequest) when `limit` is
    /// zero or exceeds the exchange's per-request limit.
    pub async fn trades(&self, market: &Market, limit: Option<u32>) -> Result<Vec<Trade>> {
        self.adapter.trades(market, limit).await
    }

    /// Reads an order book snapshot.
    ///
    /// `depth` is the maximum number of levels per side. `None` uses the
    /// exchange default.
    ///
    /// # Errors
    ///
    /// Built-in adapters return
    /// [`Error::InvalidRequest`](crate::Error::InvalidRequest) when `depth` is
    /// zero or unsupported by the exchange.
    ///
    /// Returned bids and asks are best-first; see [`OrderBook`](crate::OrderBook).
    pub async fn order_book(&self, market: &Market, depth: Option<u32>) -> Result<OrderBook> {
        self.adapter.order_book(market, depth).await
    }

    /// Reads a provider ticker summary for one market.
    ///
    /// Fields the exchange does not publish are `None`; see [`Ticker`].
    pub async fn ticker(&self, market: &Market) -> Result<Ticker> {
        self.adapter.ticker(market).await
    }

    /// Reads historical candles, oldest first.
    ///
    /// [`CandleRequest::limit`] may span multiple responses. One request makes
    /// at most 100 exchange calls.
    ///
    /// A request estimated to exceed that bound returns
    /// [`Error::InvalidRequest`](crate::Error::InvalidRequest) before the first
    /// exchange call. Use bounded `from`/`limit` batches for longer histories.
    pub async fn candles(&self, request: &CandleRequest) -> Result<Vec<Candle>> {
        self.adapter.candles(request).await
    }

    /// Opens a live market data subscription with default connection settings.
    ///
    /// Browser WebAssembly uses [`Overflow::DropNewest`](crate::Overflow::DropNewest)
    /// because the browser WebSocket API cannot pause inbound delivery. Native
    /// clients retain the default backpressure policy. Use
    /// [`Client::subscribe_with`] for an explicit policy; browser WebSockets
    /// reject [`Overflow::Backpressure`](crate::Overflow::Backpressure).
    ///
    /// Use [`Client::subscribe_with`] to change reconnect and buffering
    /// behaviour. See [`MarketStream`] for item and termination semantics.
    pub async fn subscribe(&self, subscription: &Subscription) -> Result<MarketStream> {
        self.subscribe_with(subscription, &default_stream_config())
            .await
    }

    /// Opens a live market data subscription with explicit connection settings.
    ///
    /// One subscription may use more than one underlying connection when an
    /// adapter must split feeds across endpoints. Reconnect budgets and notices
    /// then apply per connection.
    pub async fn subscribe_with(
        &self,
        subscription: &Subscription,
        config: &StreamConfig,
    ) -> Result<MarketStream> {
        self.adapter.subscribe(subscription, config).await
    }

    /// Reads the account's balances.
    ///
    /// Requires credentials.
    ///
    /// # Errors
    ///
    /// An adapter built without credentials fails with
    /// [`Error::Auth`](crate::Error::Auth) before anything is sent, on every
    /// exchange.
    pub async fn balances(&self) -> Result<Vec<Balance>> {
        self.adapter.balances().await
    }

    /// Reads current order fees, limits, supported combinations, and balances.
    ///
    /// Requires credentials.
    pub async fn order_rules(&self, market: &Market) -> Result<OrderRules> {
        self.adapter.order_rules(market).await
    }

    /// Reads live deposit and withdrawal rules for one asset.
    pub async fn asset_networks(&self, asset: &str) -> Result<Vec<AssetNetwork>> {
        if asset.trim().is_empty() {
            return Err(crate::Error::invalid_request(
                "asset",
                "asset must not be empty",
            ));
        }
        self.adapter.asset_networks(asset).await
    }

    /// Lists all exchange-issued deposit addresses for this account.
    ///
    /// A provider can omit network metadata, and an address can be absent
    /// while issuance is still pending.
    pub async fn deposit_addresses(&self) -> Result<Vec<DepositAddressEntry>> {
        self.adapter.deposit_addresses().await
    }

    /// Reads an exchange-issued deposit address for one asset and network.
    pub async fn deposit_address(&self, request: &DepositAddressRequest) -> Result<DepositAddress> {
        self.adapter.deposit_address(request).await
    }

    /// Requests creation of an exchange-issued deposit address.
    ///
    /// Exchanges can issue an address asynchronously. A successful request may
    /// therefore return an address with [`DepositAddress::address`] set to
    /// `None`; query [`Self::deposit_address`] until it is available.
    pub async fn create_deposit_address(
        &self,
        request: &DepositAddressRequest,
    ) -> Result<DepositAddress> {
        self.adapter.create_deposit_address(request).await
    }

    /// Checks a withdrawal against current account and network rules.
    pub async fn prepare_withdrawal(&self, request: &WithdrawRequest) -> Result<WithdrawalQuote> {
        validate_withdraw_request(request)?;
        self.adapter.prepare_withdrawal(request).await
    }

    /// Submits one withdrawal without automatic retry.
    pub async fn withdraw(&self, request: &WithdrawRequest) -> Result<Withdrawal> {
        validate_withdraw_request(request)?;
        self.adapter.withdraw(request).await
    }

    /// Looks up one deposit by exchange UUID or transaction ID.
    pub async fn deposit(&self, request: &TransferLookupRequest) -> Result<Deposit> {
        request.reference()?;
        self.adapter.deposit(request).await
    }

    /// Looks up one withdrawal by exchange UUID or transaction ID.
    pub async fn withdrawal(&self, request: &TransferLookupRequest) -> Result<Withdrawal> {
        request.reference()?;
        self.adapter.withdrawal(request).await
    }

    /// Cancels a withdrawal that the exchange still permits to be cancelled.
    ///
    /// A successful return confirms only that the exchange accepted the
    /// cancellation request. Call [`Self::withdrawal`] afterwards to observe
    /// the terminal withdrawal state.
    pub async fn cancel_withdrawal(&self, withdrawal_id: &str) -> Result<()> {
        if withdrawal_id.trim().is_empty() {
            return Err(crate::Error::invalid_request(
                "withdrawal_id",
                "withdrawal ID must not be empty",
            ));
        }
        self.adapter.cancel_withdrawal(withdrawal_id).await
    }

    /// Reads one page of deposit history.
    pub async fn deposits(&self, request: &TransferHistoryRequest) -> Result<Page<Deposit>> {
        self.adapter.deposits(request).await
    }

    /// Reads one page of withdrawal history.
    pub async fn withdrawals(&self, request: &TransferHistoryRequest) -> Result<Page<Withdrawal>> {
        self.adapter.withdrawals(request).await
    }

    /// Reads the account's open orders across every market.
    ///
    /// Requires credentials. A returned order may have completed between the
    /// exchange snapshot and receipt; inspect [`Order::status`](crate::Order::status).
    pub async fn open_orders(&self) -> Result<Vec<Order>> {
        self.adapter.open_orders(None).await
    }

    /// Reads the account's open orders on one market.
    ///
    /// Requires credentials. Results are scoped to `market`; the provider or
    /// adapter applies the market filter.
    pub async fn open_orders_on(&self, market: &Market) -> Result<Vec<Order>> {
        self.adapter.open_orders(Some(market)).await
    }

    /// Reads one order by the exchange's own identifier.
    ///
    /// Requires credentials. `market` prevents an identifier from silently
    /// resolving to an order on a different market.
    pub async fn order(&self, market: &Market, order_id: &str) -> Result<Order> {
        self.adapter.order(market, order_id).await
    }

    /// Reads one order by the caller-assigned identifier used at placement.
    ///
    /// Requires credentials.
    pub async fn order_by_client_id(&self, market: &Market, client_id: &str) -> Result<Order> {
        self.adapter.order_by_client_id(market, client_id).await
    }

    /// Looks up up to 100 orders by exchange or caller-assigned identifiers.
    ///
    /// Requires credentials. Providers may omit unknown, malformed, or
    /// account-inaccessible identifiers instead of returning one result per ID.
    pub async fn orders_by_ids(&self, request: &OrderLookupRequest) -> Result<Vec<Order>> {
        crate::adapters::validate_order_lookup(request)?;
        self.adapter.orders_by_ids(request).await
    }

    /// Reads one newest-first page of completed or cancelled orders.
    ///
    /// Requires credentials. Feed [`Page::next`](crate::Page::next) back into
    /// the request until it is `None`.
    pub async fn order_history(&self, request: &OrderHistoryRequest) -> Result<Page<Order>> {
        self.adapter.order_history(request).await
    }

    /// Opens a live private account subscription with default settings.
    ///
    /// Requires credentials.
    ///
    /// Browser WebAssembly uses [`Overflow::DropNewest`](crate::Overflow::DropNewest)
    /// for the same browser WebSocket limitation as [`Client::subscribe`].
    /// Native clients retain the default backpressure policy.
    ///
    /// See [`AccountStream`] for error, reconnect, and termination semantics.
    pub async fn subscribe_account(&self) -> Result<AccountStream> {
        self.subscribe_account_with(&default_stream_config()).await
    }

    /// Opens a live private account subscription with explicit connection settings.
    ///
    /// Requires credentials. An adapter may raise an idle timeout below the
    /// minimum its exchange can satisfy.
    pub async fn subscribe_account_with(&self, config: &StreamConfig) -> Result<AccountStream> {
        self.adapter.subscribe_account(config).await
    }

    /// Places an order.
    ///
    /// Requires credentials. The returned [`Order`] carries the exchange's own
    /// identifier, which is what [`Client::cancel_order`] takes.
    pub async fn place_order(&self, request: &OrderRequest) -> Result<Order> {
        self.adapter.place_order(request).await
    }

    /// Cancels an order.
    ///
    /// Requires credentials. `Ok(())` confirms that the provider accepted or
    /// confirmed the cancellation response; it is not a final fill snapshot.
    /// Cancellation races execution, so query the order when the final outcome
    /// matters.
    pub async fn cancel_order(&self, market: &Market, order_id: &str) -> Result<()> {
        self.adapter.cancel_order(market, order_id).await
    }

    /// Cancels an order by the caller-assigned identifier used at placement.
    ///
    /// Requires credentials. Cancellation has the same race and reconciliation
    /// requirements as [`Client::cancel_order`].
    pub async fn cancel_order_by_client_id(&self, market: &Market, client_id: &str) -> Result<()> {
        self.adapter
            .cancel_order_by_client_id(market, client_id)
            .await
    }

    /// Cancels multiple orders without treating a partial failure as an atomic failure.
    ///
    /// Requires credentials. Provider limits differ; Upbit accepts at most 20
    /// identifiers and Bithumb accepts at most 30.
    pub async fn cancel_orders(&self, request: &CancelOrdersRequest) -> Result<CancelOrdersResult> {
        crate::adapters::validate_cancel_orders(request)?;
        self.adapter.cancel_orders(request).await
    }

    /// Reads every open position.
    ///
    /// Requires credentials. Derivatives markets only.
    ///
    /// Rows with `quantity == 0` are removed.
    pub async fn positions(&self) -> Result<Vec<Position>> {
        Ok(open_positions(self.adapter.positions(None).await?))
    }

    /// Reads the open position on one market.
    ///
    /// Requires credentials. Derivatives markets only.
    ///
    /// A market the account holds nothing on answers an empty list rather than
    /// one flat position, on the same terms as [`Client::positions`].
    pub async fn positions_on(&self, market: &Market) -> Result<Vec<Position>> {
        Ok(open_positions(self.adapter.positions(Some(market)).await?))
    }

    /// Reads account-wide margin state.
    ///
    /// Requires credentials. Derivatives markets only. Values an exchange does
    /// not publish remain `None`.
    pub async fn margin_summary(&self) -> Result<MarginSummary> {
        self.adapter.margin_summary().await
    }

    /// Reads a market's funding-rate history, one page at a time.
    ///
    /// This public operation needs no account credentials. Continue only when
    /// [`Page::next`](crate::Page::next) is `Some`; item count does not mark the
    /// final page.
    pub async fn funding_rates(&self, request: &HistoryRequest) -> Result<Page<FundingRate>> {
        self.adapter.funding_rates(request).await
    }

    /// Reads the account's funding payment history, one page at a time.
    ///
    /// Requires credentials. Unlike [`Client::funding_rates`], this is what the
    /// account was actually charged or credited. Amounts are signed; negative
    /// means the account paid.
    pub async fn funding_payments(&self, request: &HistoryRequest) -> Result<Page<FundingPayment>> {
        self.adapter.funding_payments(request).await
    }

    /// Changes leverage and/or margin mode on a market.
    ///
    /// Requires credentials. Derivatives markets only. Provider requirements
    /// differ: some accept either field, while others require both. When both
    /// are accepted, the change is not guaranteed to be atomic; one provider
    /// operation may succeed before another fails.
    pub async fn set_margin(&self, request: &MarginRequest) -> Result<()> {
        self.adapter.set_margin(request).await
    }
}

fn default_stream_config() -> StreamConfig {
    #[cfg(target_arch = "wasm32")]
    {
        StreamConfig {
            overflow: crate::types::Overflow::DropNewest,
            ..StreamConfig::default()
        }
    }
    #[cfg(not(target_arch = "wasm32"))]
    {
        StreamConfig::default()
    }
}

fn validate_withdraw_request(request: &WithdrawRequest) -> Result<()> {
    if request.asset.trim().is_empty() {
        return Err(crate::Error::invalid_request(
            "asset",
            "asset must not be empty",
        ));
    }
    if request.amount <= crate::Decimal::ZERO {
        return Err(crate::Error::invalid_request(
            "amount",
            "amount must be greater than zero",
        ));
    }
    if !request.network.same_chain(request.destination.network()) {
        return Err(crate::Error::transfer(
            crate::TransferErrorKind::NetworkMismatch,
            format!(
                "withdrawal network {} differs from destination network {}",
                request.network,
                request.destination.network(),
            ),
        ));
    }
    if !request
        .asset
        .eq_ignore_ascii_case(request.destination.asset())
    {
        return Err(crate::Error::transfer(
            crate::TransferErrorKind::AssetMismatch,
            format!(
                "withdrawal asset {} differs from destination asset {}",
                request.asset,
                request.destination.asset(),
            ),
        ));
    }
    if request.destination.address().trim().is_empty() {
        return Err(crate::Error::invalid_request(
            "destination.address",
            "address must not be empty",
        ));
    }
    Ok(())
}

impl<A: Adapter> From<A> for Client<A> {
    fn from(adapter: A) -> Self {
        Self::new(adapter)
    }
}

/// Keeps the common positions API limited to non-flat rows.
pub(crate) fn open_positions(mut positions: Vec<Position>) -> Vec<Position> {
    positions.retain(|position| !position.is_flat());
    positions
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};

    use super::*;
    use crate::adapter::BoxFuture;
    use crate::{Decimal, Error, Feed, MarketEvent, Overflow, Side};

    #[derive(Debug, Clone)]
    struct PublicOnly;

    impl Adapter for PublicOnly {
        fn exchange(&self) -> Exchange {
            Exchange::Bithumb
        }

        fn supports(&self, feature: Feature) -> bool {
            !feature.needs_credentials()
        }

        fn markets(&self, kind: MarketKind) -> BoxFuture<'_, Result<Vec<MarketInfo>>> {
            let empty = matches!(kind, MarketKind::Perpetual);
            Box::pin(async move {
                Ok(if empty {
                    vec![]
                } else {
                    vec![MarketInfo {
                        market: Market::spot(Exchange::Bithumb, "BTC", "KRW"),
                        native_symbol: "BTC_KRW".to_string(),
                        status: crate::MarketStatus::Active,
                        korean_name: None,
                        english_name: None,
                    }]
                })
            })
        }
    }

    #[test]
    fn supports_answers_without_a_network_call() {
        let client = Client::new(PublicOnly);

        assert!(client.supports(Feature::Ticker));
        assert!(!client.supports(Feature::Trading));
        assert_eq!(client.exchange(), Exchange::Bithumb);
    }

    #[tokio::test]
    async fn a_spot_exchange_reports_no_perpetuals_rather_than_an_error() {
        let client = Client::new(PublicOnly);

        assert_eq!(client.markets(MarketKind::Spot).await.unwrap().len(), 1);
        assert!(
            client
                .markets(MarketKind::Perpetual)
                .await
                .unwrap()
                .is_empty()
        );
    }

    #[tokio::test]
    async fn private_calls_on_a_public_client_name_the_missing_feature() {
        let client = Client::new(PublicOnly);

        let error = client.balances().await.unwrap_err();
        assert!(matches!(
            error,
            Error::Unsupported {
                feature: Feature::Balances,
                exchange: "bithumb",
                ..
            }
        ));
    }

    /// An adapter fixture that includes a flat position.
    #[derive(Debug, Clone)]
    struct ReportsWhatTheVenueSaid;

    impl ReportsWhatTheVenueSaid {
        fn market(quote: &str) -> Market {
            Market::perpetual(Exchange::Binance, "BTC", quote)
        }

        fn position(quantity: Decimal, quote: &str) -> Position {
            Position {
                market: Self::market(quote),
                side: if quantity.is_zero() {
                    None
                } else {
                    Some(Side::Buy)
                },
                quantity,
                entry_price: None,
                mark_price: None,
                notional: Some(Decimal::from(30_000)),
                unrealized_pnl: None,
                leverage: None,
                margin_mode: None,
            }
        }
    }

    impl Adapter for ReportsWhatTheVenueSaid {
        fn exchange(&self) -> Exchange {
            Exchange::Binance
        }

        fn supports(&self, _feature: Feature) -> bool {
            true
        }

        fn positions(&self, _market: Option<&Market>) -> BoxFuture<'_, Result<Vec<Position>>> {
            Box::pin(async move {
                Ok(vec![
                    Self::position(Decimal::ZERO, "USDT"),
                    Self::position(Decimal::ONE, "USDC"),
                ])
            })
        }
    }

    /// The common client removes flat rows from both position queries.
    #[tokio::test]
    async fn a_flat_row_an_adapter_reports_is_not_answered_as_an_open_position() {
        let client = Client::new(ReportsWhatTheVenueSaid);

        assert_eq!(client.adapter().positions(None).await.unwrap().len(), 2);

        let open = client.positions().await.unwrap();
        assert_eq!(open.len(), 1, "a flat row was answered as an open position");
        assert_eq!(open[0].quantity, Decimal::ONE);

        let narrowed = client
            .positions_on(&ReportsWhatTheVenueSaid::market("USDT"))
            .await
            .unwrap();
        assert_eq!(narrowed.len(), 1, "{narrowed:?}");
        assert!(!narrowed[0].is_flat(), "{narrowed:?}");
    }

    #[tokio::test]
    async fn clients_over_boxed_adapters_share_one_type() {
        let clients: Vec<Client<Box<dyn Adapter>>> = vec![Client::new(Box::new(PublicOnly) as _)];

        for client in &clients {
            assert!(!client.markets(MarketKind::Spot).await.unwrap().is_empty());
        }
    }

    #[derive(Debug, Clone, Default)]
    struct RecordsStreamConfig(Arc<Mutex<Vec<StreamConfig>>>);

    impl Adapter for RecordsStreamConfig {
        fn exchange(&self) -> Exchange {
            Exchange::Bithumb
        }

        fn supports(&self, _feature: Feature) -> bool {
            true
        }

        fn subscribe(
            &self,
            _subscription: &Subscription,
            config: &StreamConfig,
        ) -> BoxFuture<'_, Result<MarketStream>> {
            self.0.lock().unwrap().push(config.clone());
            Box::pin(async {
                Ok(MarketStream::new(futures_util::stream::empty::<
                    Result<MarketEvent>,
                >()))
            })
        }

        fn subscribe_account(&self, config: &StreamConfig) -> BoxFuture<'_, Result<AccountStream>> {
            self.0.lock().unwrap().push(config.clone());
            Box::pin(async {
                Ok(AccountStream::new(futures_util::stream::empty::<
                    Result<crate::AccountEvent>,
                >()))
            })
        }
    }

    #[tokio::test]
    async fn default_stream_methods_use_the_platform_supported_overflow() {
        let adapter = RecordsStreamConfig::default();
        let seen = Arc::clone(&adapter.0);
        let client = Client::new(adapter);
        let subscription = Subscription::new()
            .market(Market::spot(Exchange::Bithumb, "BTC", "KRW"))
            .feed(Feed::Trades);

        client.subscribe(&subscription).await.unwrap();
        client.subscribe_account().await.unwrap();

        #[cfg(target_arch = "wasm32")]
        let expected = Overflow::DropNewest;
        #[cfg(not(target_arch = "wasm32"))]
        let expected = Overflow::Backpressure;
        assert_eq!(
            seen.lock()
                .unwrap()
                .iter()
                .map(|config| config.overflow)
                .collect::<Vec<_>>(),
            vec![expected, expected]
        );
    }

    #[tokio::test]
    async fn explicit_stream_methods_preserve_backpressure() {
        let adapter = RecordsStreamConfig::default();
        let seen = Arc::clone(&adapter.0);
        let client = Client::new(adapter);
        let subscription = Subscription::new()
            .market(Market::spot(Exchange::Bithumb, "BTC", "KRW"))
            .feed(Feed::Trades);
        let config = StreamConfig::default();

        client.subscribe_with(&subscription, &config).await.unwrap();
        client.subscribe_account_with(&config).await.unwrap();

        assert!(
            seen.lock()
                .unwrap()
                .iter()
                .all(|config| config.overflow == Overflow::Backpressure)
        );
    }
}