Skip to main content

rustrade_execution/exchange/mock/
mod.rs

1use crate::{
2    AccountEventKind, InstrumentAccountSnapshot, UnindexedAccountEvent, UnindexedAccountSnapshot,
3    balance::AssetBalance,
4    client::mock::MockExecutionConfig,
5    error::{ApiError, UnindexedApiError, UnindexedOrderError},
6    exchange::mock::{
7        account::AccountState,
8        request::{MarketPrices, MockExchangeRequest, MockExchangeRequestKind},
9    },
10    fee::{FeeModel, FeeModelConfig},
11    fill::{FillModel, SimFillConfig},
12    order::{
13        Order, OrderKey, OrderKind, UnindexedOrder,
14        id::OrderId,
15        request::{OrderRequestCancel, OrderRequestOpen, UnindexedOrderResponseCancel},
16        state::{Cancelled, Filled, OrderState, UnindexedOrderState},
17    },
18    trade::{AssetFees, Trade, TradeId},
19};
20use chrono::{DateTime, TimeDelta, Utc};
21use fnv::FnvHashMap;
22use futures::stream::BoxStream;
23use itertools::Itertools;
24use rust_decimal::Decimal;
25use rustrade_instrument::{
26    Side,
27    asset::name::AssetNameExchange,
28    exchange::ExchangeId,
29    instrument::{Instrument, name::InstrumentNameExchange},
30};
31use rustrade_integration::collection::snapshot::Snapshot;
32use smol_str::ToSmolStr;
33use std::fmt::Debug;
34use tokio::sync::{broadcast, mpsc, oneshot};
35use tokio_stream::{StreamExt, wrappers::BroadcastStream};
36use tracing::{error, info};
37
38pub mod account;
39pub mod request;
40
41#[derive(Debug)]
42pub struct MockExchange {
43    pub exchange: ExchangeId,
44    pub latency_ms: u64,
45    pub fee_model: FeeModelConfig,
46    pub fill_model: SimFillConfig,
47    pub request_rx: mpsc::UnboundedReceiver<MockExchangeRequest>,
48    pub event_tx: broadcast::Sender<UnindexedAccountEvent>,
49    pub instruments: FnvHashMap<InstrumentNameExchange, Instrument<ExchangeId, AssetNameExchange>>,
50    pub account: AccountState,
51    pub order_sequence: u64,
52    pub time_exchange_latest: DateTime<Utc>,
53}
54
55impl MockExchange {
56    pub fn new(
57        config: MockExecutionConfig,
58        request_rx: mpsc::UnboundedReceiver<MockExchangeRequest>,
59        event_tx: broadcast::Sender<UnindexedAccountEvent>,
60        instruments: FnvHashMap<InstrumentNameExchange, Instrument<ExchangeId, AssetNameExchange>>,
61    ) -> Self {
62        Self {
63            exchange: config.mocked_exchange,
64            latency_ms: config.latency_ms,
65            fee_model: config.fee_model,
66            fill_model: config.fill_model,
67            request_rx,
68            event_tx,
69            instruments,
70            account: AccountState::from(config.initial_state),
71            order_sequence: 0,
72            time_exchange_latest: Default::default(),
73        }
74    }
75
76    pub async fn run(mut self) {
77        while let Some(request) = self.request_rx.recv().await {
78            self.update_time_exchange(request.time_request);
79
80            match request.kind {
81                MockExchangeRequestKind::FetchAccountSnapshot { response_tx } => {
82                    let snapshot = self.account_snapshot();
83                    self.respond_with_latency(response_tx, snapshot);
84                }
85                MockExchangeRequestKind::FetchBalances {
86                    response_tx,
87                    assets,
88                } => {
89                    // Empty slice means "return all" (consistent with account_snapshot behavior).
90                    let balances = self
91                        .account
92                        .balances()
93                        .filter(|balance| assets.is_empty() || assets.contains(&balance.asset))
94                        .cloned()
95                        .collect();
96                    self.respond_with_latency(response_tx, balances);
97                }
98                MockExchangeRequestKind::FetchOrdersOpen {
99                    response_tx,
100                    instruments,
101                } => {
102                    // Empty slice means "return all" (consistent with account_snapshot behavior).
103                    let orders_open = self
104                        .account
105                        .orders_open()
106                        .filter(|order| {
107                            instruments.is_empty() || instruments.contains(&order.key.instrument)
108                        })
109                        .cloned()
110                        .collect();
111                    self.respond_with_latency(response_tx, orders_open);
112                }
113                MockExchangeRequestKind::FetchTrades {
114                    response_tx,
115                    time_since,
116                } => {
117                    let trades = self.account.trades(time_since).cloned().collect();
118                    self.respond_with_latency(response_tx, trades);
119                }
120                MockExchangeRequestKind::CancelOrder {
121                    response_tx,
122                    request,
123                } => {
124                    // MockExchange only supports Market orders which fill immediately,
125                    // so there are never any open orders to cancel. Send a rejection
126                    // response so the caller doesn't hang waiting on the oneshot.
127                    error!(
128                        exchange = %self.exchange,
129                        ?request,
130                        "MockExchange received cancel request but only Market orders are supported"
131                    );
132                    let key = OrderKey {
133                        exchange: request.key.exchange,
134                        instrument: request.key.instrument,
135                        strategy: request.key.strategy,
136                        cid: request.key.cid,
137                    };
138                    let _ = response_tx.send(UnindexedOrderResponseCancel {
139                        key,
140                        state: Err(UnindexedOrderError::Rejected(ApiError::OrderRejected(
141                            "MockExchange does not support CancelOrder (only Market orders which fill immediately)".into(),
142                        ))),
143                    });
144                }
145                MockExchangeRequestKind::OpenOrder {
146                    response_tx,
147                    request,
148                    market_prices,
149                } => {
150                    let (response, notifications) = self.open_order(request, market_prices);
151                    self.respond_with_latency(response_tx, response);
152
153                    if let Some(notifications) = notifications {
154                        self.account.ack_trade(notifications.trade.clone());
155                        self.send_notifications_with_latency(notifications);
156                    }
157                }
158            }
159        }
160
161        info!(exchange = %self.exchange, "MockExchange shutting down");
162    }
163
164    fn update_time_exchange(&mut self, time_request: DateTime<Utc>) {
165        let client_to_exchange_latency = self.latency_ms / 2;
166
167        self.time_exchange_latest = time_request
168            .checked_add_signed(TimeDelta::milliseconds(client_to_exchange_latency as i64))
169            .unwrap_or(time_request);
170
171        self.account.update_time_exchange(self.time_exchange_latest)
172    }
173
174    pub fn time_exchange(&self) -> DateTime<Utc> {
175        self.time_exchange_latest
176    }
177
178    pub fn account_snapshot(&self) -> UnindexedAccountSnapshot {
179        let balances = self.account.balances().cloned().collect();
180
181        let orders_open = self
182            .account
183            .orders_open()
184            .cloned()
185            .map(UnindexedOrder::from);
186
187        let orders_cancelled = self
188            .account
189            .orders_cancelled()
190            .cloned()
191            .map(UnindexedOrder::from);
192
193        let orders_all = orders_open.chain(orders_cancelled);
194        let orders_all = orders_all.sorted_unstable_by_key(|order| order.key.instrument.clone());
195        let orders_by_instrument = orders_all.chunk_by(|order| order.key.instrument.clone());
196
197        let instruments = orders_by_instrument
198            .into_iter()
199            .map(|(instrument, orders)| InstrumentAccountSnapshot {
200                instrument,
201                orders: orders.into_iter().collect(),
202                position: None,
203            })
204            .collect();
205
206        UnindexedAccountSnapshot {
207            exchange: self.exchange,
208            balances,
209            instruments,
210        }
211    }
212
213    /// Sends the provided `Response` via the [`oneshot::Sender`] after waiting for the latency
214    /// [`Duration`].
215    ///
216    /// Used to simulate network latency between the exchange and client.
217    fn respond_with_latency<Response>(
218        &self,
219        response_tx: oneshot::Sender<Response>,
220        response: Response,
221    ) where
222        Response: Send + 'static,
223    {
224        let exchange = self.exchange;
225        let latency = std::time::Duration::from_millis(self.latency_ms);
226
227        tokio::spawn(async move {
228            tokio::time::sleep(latency).await;
229            if response_tx.send(response).is_err() {
230                error!(
231                    %exchange,
232                    kind = std::any::type_name::<Response>(),
233                    "MockExchange failed to send oneshot response to client"
234                );
235            }
236        });
237    }
238
239    /// Sends the provided `OpenOrderNotifications` via the `MockExchanges`
240    /// `broadcast::Sender<UnindexedAccountEvent>` after waiting for the latency
241    /// [`Duration`].
242    ///
243    /// Used to simulate network latency between the exchange and client.
244    fn send_notifications_with_latency(&self, notifications: OpenOrderNotifications) {
245        let balance = self.build_account_event(notifications.balance);
246        let trade = self.build_account_event(notifications.trade);
247
248        let exchange = self.exchange;
249        let latency = std::time::Duration::from_millis(self.latency_ms);
250        let tx = self.event_tx.clone();
251        tokio::spawn(async move {
252            tokio::time::sleep(latency).await;
253
254            if tx.send(balance).is_err() {
255                error!(
256                    %exchange,
257                    kind = "Snapshot<AssetBalance<AssetNameExchange>",
258                    "MockExchange failed to send AccountEvent notification to client"
259                );
260            }
261
262            if tx.send(trade).is_err() {
263                error!(
264                    %exchange,
265                    kind = "Trade<AssetNameExchange, InstrumentNameExchange>",
266                    "MockExchange failed to send AccountEvent notification to client"
267                );
268            }
269        });
270    }
271
272    pub fn account_stream(&self) -> BoxStream<'static, UnindexedAccountEvent> {
273        futures::StreamExt::boxed(BroadcastStream::new(self.event_tx.subscribe()).map_while(
274            |result| match result {
275                Ok(event) => Some(event),
276                Err(error) => {
277                    error!(
278                        ?error,
279                        "MockExchange Broadcast AccountStream lagged - terminating"
280                    );
281                    None
282                }
283            },
284        ))
285    }
286
287    pub fn cancel_order(
288        &mut self,
289        _: OrderRequestCancel<ExchangeId, InstrumentNameExchange>,
290    ) -> Order<ExchangeId, InstrumentNameExchange, Result<Cancelled, UnindexedOrderError>> {
291        unimplemented!()
292    }
293
294    pub fn open_order(
295        &mut self,
296        request: OrderRequestOpen<ExchangeId, InstrumentNameExchange>,
297        market_prices: MarketPrices,
298    ) -> (
299        Order<ExchangeId, InstrumentNameExchange, UnindexedOrderState>,
300        Option<OpenOrderNotifications>,
301    ) {
302        if let Err(error) = self.validate_order_kind_supported(request.state.kind) {
303            return (build_open_order_err_response(request, error), None);
304        }
305
306        let underlying = match self.find_instrument_data(&request.key.instrument) {
307            Ok(instrument) => instrument.underlying.clone(),
308            Err(error) => return (build_open_order_err_response(request, error), None),
309        };
310
311        // Compute fill price via the configured FillModel.
312        //
313        // For limit orders, pass the limit price as `order_price`; for market orders pass `None`
314        // so the model can select the best available market price (bid/ask/last).  When no market
315        // data is present (standard MockExecution passes all-None MarketPrices), `request.state.price`
316        // is used as the `last_price` fallback so behaviour is identical to the pre-FillModel path.
317        //
318        // Invariant: `fill_price` is only called for marketable orders. `validate_order_kind_supported`
319        // (called above) currently rejects Limit orders, ensuring FillModel::fill_price never receives
320        // a non-marketable limit order. If Limit support is added later, the fill model must enforce
321        // limit-price semantics (e.g. a limit buy must not fill above the limit price).
322        let fill_price = self
323            .fill_model
324            .fill_price(
325                request.state.side,
326                match request.state.kind {
327                    // unreachable: validate_order_kind_supported (called above) already
328                    // rejects Limit orders with Err, so this arm is never reached.
329                    // Kept for exhaustiveness; passes the limit price so fill models that
330                    // gain Limit support in future behave correctly without a separate change.
331                    OrderKind::Limit => Some(request.state.price),
332                    OrderKind::Market => None,
333                },
334                market_prices.best_bid,
335                market_prices.best_ask,
336                market_prices.last_price.or(Some(request.state.price)),
337            )
338            .unwrap_or(request.state.price);
339
340        let time_exchange = self.time_exchange();
341
342        // Compute fee using the configured FeeModel. For spot, contract_size = 1.
343        let order_fees_quote =
344            self.fee_model
345                .compute_fee(fill_price, request.state.quantity, Decimal::ONE);
346
347        let balance_change_result = match request.state.side {
348            Side::Buy => {
349                // Buying Instrument requires sufficient QuoteAsset Balance
350                #[allow(clippy::expect_used)]
351                // Invariant: MockExchange - balances exist for all configured instruments
352                let current = self
353                    .account
354                    .balance_mut(&underlying.quote)
355                    .expect("MockExchange has Balance for all configured Instrument assets");
356
357                // Currently we only supported MarketKind orders, so they should be identical
358                assert_eq!(current.balance.total, current.balance.free);
359
360                let order_value_quote = fill_price * request.state.quantity.abs();
361                let quote_required = order_value_quote + order_fees_quote;
362
363                let maybe_new_balance = current.balance.free - quote_required;
364
365                if maybe_new_balance >= Decimal::ZERO {
366                    current.balance.free = maybe_new_balance;
367                    current.balance.total = maybe_new_balance;
368                    current.time_exchange = time_exchange;
369
370                    Ok((
371                        current.clone(),
372                        AssetFees::new(
373                            underlying.quote.clone(),
374                            order_fees_quote,
375                            Some(order_fees_quote),
376                        ),
377                    ))
378                } else {
379                    Err(ApiError::BalanceInsufficient(
380                        underlying.quote.clone(),
381                        format!(
382                            "Available Balance: {}, Required Balance inc. fees: {}",
383                            current.balance.free, quote_required
384                        ),
385                    ))
386                }
387            }
388            Side::Sell => {
389                // Selling Instrument requires sufficient BaseAsset Balance
390                #[allow(clippy::expect_used)]
391                // Invariant: MockExchange - balances exist for all configured instruments
392                let current = self
393                    .account
394                    .balance_mut(&underlying.base)
395                    .expect("MockExchange has Balance for all configured Instrument assets");
396
397                // Currently we only supported MarketKind orders, so they should be identical
398                assert_eq!(current.balance.total, current.balance.free);
399
400                let order_value_base = request.state.quantity.abs();
401                // Fee is quote-denominated; convert to base for deduction.
402                // Note: For PerContractFeeModel this conversion is nonsensical (flat USD / price),
403                // but MockExchange is spot-only so PerContract isn't used in practice.
404                debug_assert!(
405                    !matches!(self.fee_model, FeeModelConfig::PerContract(_)),
406                    "PerContractFeeModel produces nonsensical base-denominated fees on sell path"
407                );
408                let order_fees_base = if fill_price.is_zero() {
409                    Decimal::ZERO
410                } else {
411                    order_fees_quote / fill_price
412                };
413                let base_required = order_value_base + order_fees_base;
414
415                let maybe_new_balance = current.balance.free - base_required;
416
417                if maybe_new_balance >= Decimal::ZERO {
418                    current.balance.free = maybe_new_balance;
419                    current.balance.total = maybe_new_balance;
420                    current.time_exchange = time_exchange;
421
422                    Ok((
423                        current.clone(),
424                        AssetFees::new(
425                            underlying.quote.clone(),
426                            order_fees_quote,
427                            Some(order_fees_quote),
428                        ),
429                    ))
430                } else {
431                    Err(ApiError::BalanceInsufficient(
432                        underlying.base,
433                        format!(
434                            "Available Balance: {}, Required Balance inc. fees: {}",
435                            current.balance.free, base_required
436                        ),
437                    ))
438                }
439            }
440        };
441
442        let (balance_snapshot, fees) = match balance_change_result {
443            Ok((balance_snapshot, fees)) => (Snapshot(balance_snapshot), fees),
444            Err(error) => return (build_open_order_err_response(request, error), None),
445        };
446
447        let order_id = self.order_id_sequence_fetch_add();
448        let trade_id = TradeId(order_id.0.clone());
449
450        let order_response = Order {
451            key: request.key.clone(),
452            side: request.state.side,
453            price: fill_price,
454            quantity: request.state.quantity,
455            kind: request.state.kind,
456            time_in_force: request.state.time_in_force,
457            state: OrderState::fully_filled(Filled::new(
458                order_id.clone(),
459                self.time_exchange(),
460                request.state.quantity,
461                Some(fill_price),
462            )),
463        };
464
465        let notifications = OpenOrderNotifications {
466            balance: balance_snapshot,
467            trade: Trade {
468                id: trade_id,
469                order_id: order_id.clone(),
470                instrument: request.key.instrument,
471                strategy: request.key.strategy,
472                time_exchange: self.time_exchange(),
473                side: request.state.side,
474                price: fill_price,
475                quantity: request.state.quantity,
476                fees,
477            },
478        };
479
480        (order_response, Some(notifications))
481    }
482
483    pub fn validate_order_kind_supported(
484        &self,
485        order_kind: OrderKind,
486    ) -> Result<(), UnindexedOrderError> {
487        if order_kind == OrderKind::Market {
488            Ok(())
489        } else {
490            Err(UnindexedOrderError::Rejected(ApiError::OrderRejected(
491                format!("MockExchange does not supported OrderKind: {order_kind}"),
492            )))
493        }
494    }
495
496    pub fn find_instrument_data(
497        &self,
498        instrument: &InstrumentNameExchange,
499    ) -> Result<&Instrument<ExchangeId, AssetNameExchange>, UnindexedApiError> {
500        self.instruments.get(instrument).ok_or_else(|| {
501            ApiError::InstrumentInvalid(
502                instrument.clone(),
503                format!("MockExchange is not set-up for managing: {instrument}"),
504            )
505        })
506    }
507
508    fn order_id_sequence_fetch_add(&mut self) -> OrderId {
509        let sequence = self.order_sequence;
510        self.order_sequence += 1;
511        OrderId::new(sequence.to_smolstr())
512    }
513
514    fn build_account_event<Kind>(&self, kind: Kind) -> UnindexedAccountEvent
515    where
516        Kind: Into<AccountEventKind<ExchangeId, AssetNameExchange, InstrumentNameExchange>>,
517    {
518        UnindexedAccountEvent {
519            exchange: self.exchange,
520            kind: kind.into(),
521        }
522    }
523}
524
525fn build_open_order_err_response<E>(
526    request: OrderRequestOpen<ExchangeId, InstrumentNameExchange>,
527    error: E,
528) -> Order<ExchangeId, InstrumentNameExchange, UnindexedOrderState>
529where
530    E: Into<UnindexedOrderError>,
531{
532    Order {
533        key: request.key,
534        side: request.state.side,
535        price: request.state.price,
536        quantity: request.state.quantity,
537        kind: request.state.kind,
538        time_in_force: request.state.time_in_force,
539        state: OrderState::inactive(error.into()),
540    }
541}
542
543#[derive(Debug)]
544pub struct OpenOrderNotifications {
545    pub balance: Snapshot<AssetBalance<AssetNameExchange>>,
546    pub trade: Trade<AssetNameExchange, InstrumentNameExchange>,
547}
548
549#[cfg(test)]
550#[allow(clippy::unwrap_used, clippy::expect_used)] // Test code: panics on bad input are acceptable
551mod tests {
552    use super::*;
553    use crate::{
554        UnindexedAccountSnapshot,
555        balance::{AssetBalance, Balance},
556        error::ApiError,
557        exchange::mock::request::MarketPrices,
558        fee::{FeeModelConfig, PercentageFeeModel},
559        fill::{BidAskFillModel, SimFillConfig},
560        order::{
561            OrderEvent, OrderKey, OrderKind, TimeInForce,
562            id::{ClientOrderId, StrategyId},
563            request::RequestOpen,
564            state::InactiveOrderState,
565        },
566    };
567    use chrono::Utc;
568    use rust_decimal::Decimal;
569    use rustrade_instrument::{
570        Side, Underlying,
571        asset::name::AssetNameExchange,
572        exchange::ExchangeId,
573        instrument::{
574            Instrument,
575            kind::InstrumentKind,
576            name::{InstrumentNameExchange, InstrumentNameInternal},
577            quote::InstrumentQuoteAsset,
578        },
579    };
580    use tokio::sync::{broadcast, mpsc};
581
582    fn d(s: &str) -> Decimal {
583        s.parse().unwrap()
584    }
585
586    const EXCHANGE: ExchangeId = ExchangeId::BinanceSpot;
587
588    fn base() -> AssetNameExchange {
589        AssetNameExchange::new("BTC")
590    }
591
592    fn quote() -> AssetNameExchange {
593        AssetNameExchange::new("USDT")
594    }
595
596    fn instrument_name() -> InstrumentNameExchange {
597        InstrumentNameExchange::new("BTCUSDT")
598    }
599
600    fn make_exchange(btc: &str, usdt: &str) -> MockExchange {
601        make_exchange_with_fee(btc, usdt, FeeModelConfig::default())
602    }
603
604    fn make_exchange_with_fee(btc: &str, usdt: &str, fee_model: FeeModelConfig) -> MockExchange {
605        let btc = d(btc);
606        let usdt = d(usdt);
607        let initial_state = UnindexedAccountSnapshot {
608            exchange: EXCHANGE,
609            balances: vec![
610                AssetBalance {
611                    asset: base(),
612                    balance: Balance {
613                        total: btc,
614                        free: btc,
615                    },
616                    time_exchange: Utc::now(),
617                },
618                AssetBalance {
619                    asset: quote(),
620                    balance: Balance {
621                        total: usdt,
622                        free: usdt,
623                    },
624                    time_exchange: Utc::now(),
625                },
626            ],
627            instruments: vec![],
628        };
629
630        let config = MockExecutionConfig::new(
631            EXCHANGE,
632            initial_state,
633            0, // latency_ms
634            fee_model,
635            SimFillConfig::default(),
636        );
637
638        let (_tx, request_rx) = mpsc::unbounded_channel();
639        let (event_tx, _) = broadcast::channel(1);
640
641        let mut instruments = FnvHashMap::default();
642        instruments.insert(
643            instrument_name(),
644            Instrument {
645                exchange: EXCHANGE,
646                name_internal: InstrumentNameInternal::new("btcusdt"),
647                name_exchange: instrument_name(),
648                underlying: Underlying {
649                    base: base(),
650                    quote: quote(),
651                },
652                quote: InstrumentQuoteAsset::UnderlyingQuote,
653                kind: InstrumentKind::Spot,
654                spec: None,
655            },
656        );
657
658        MockExchange::new(config, request_rx, event_tx, instruments)
659    }
660
661    fn buy_request(
662        quantity: &str,
663        price: &str,
664    ) -> OrderRequestOpen<ExchangeId, InstrumentNameExchange> {
665        let (quantity, price) = (d(quantity), d(price));
666        OrderEvent {
667            key: OrderKey {
668                exchange: EXCHANGE,
669                instrument: instrument_name(),
670                strategy: StrategyId::new("test"),
671                cid: ClientOrderId::new("test-cid"),
672            },
673            state: RequestOpen {
674                side: Side::Buy,
675                price,
676                quantity,
677                kind: OrderKind::Market,
678                time_in_force: TimeInForce::ImmediateOrCancel,
679                position_id: None,
680                reduce_only: false,
681            },
682        }
683    }
684
685    fn sell_request(
686        quantity: &str,
687        price: &str,
688    ) -> OrderRequestOpen<ExchangeId, InstrumentNameExchange> {
689        let (quantity, price) = (d(quantity), d(price));
690        OrderEvent {
691            key: OrderKey {
692                exchange: EXCHANGE,
693                instrument: instrument_name(),
694                strategy: StrategyId::new("test"),
695                cid: ClientOrderId::new("test-cid"),
696            },
697            state: RequestOpen {
698                side: Side::Sell,
699                price,
700                quantity,
701                kind: OrderKind::Market,
702                time_in_force: TimeInForce::ImmediateOrCancel,
703                position_id: None,
704                reduce_only: false,
705            },
706        }
707    }
708
709    #[test]
710    fn sell_order_decrements_base_balance_not_quote() {
711        let mut exchange = make_exchange("1.0", "10000");
712        let initial_usdt = d("10000");
713
714        let (response, notifications) =
715            exchange.open_order(sell_request("0.5", "50000"), MarketPrices::default());
716
717        assert!(
718            response.state.is_accepted(),
719            "sell should succeed: {:?}",
720            response.state
721        );
722        assert!(
723            notifications.is_some(),
724            "successful sell must produce notifications"
725        );
726
727        // Base (BTC) must be decremented by the quantity sold.
728        let btc = exchange.account.balance_mut(&base()).unwrap();
729        assert_eq!(
730            btc.balance.free,
731            d("0.5"),
732            "base balance should decrease by quantity sold"
733        );
734
735        // Quote (USDT) must be unchanged (fees = 0 in this test).
736        let usdt = exchange.account.balance_mut(&quote()).unwrap();
737        assert_eq!(
738            usdt.balance.free, initial_usdt,
739            "quote balance should be unchanged on sell"
740        );
741    }
742
743    #[test]
744    fn sell_order_insufficient_balance_names_base_asset() {
745        // Regression guard for the sell-side balance bug fixed in this branch:
746        // previously `balance_mut(&underlying.quote)` was called for sells, so
747        // BalanceInsufficient would name the quote asset (USDT) instead of the base (BTC).
748        let mut exchange = make_exchange("0.1", "10000");
749
750        let (response, notifications) = exchange.open_order(
751            sell_request("1.0", "50000"), // selling 1 BTC but only 0.1 available
752            MarketPrices::default(),
753        );
754
755        assert!(
756            notifications.is_none(),
757            "failed order must produce no notifications"
758        );
759        match response.state {
760            OrderState::Inactive(InactiveOrderState::OpenFailed(
761                crate::error::OrderError::Rejected(ApiError::BalanceInsufficient(ref asset, _)),
762            )) => {
763                assert_eq!(
764                    *asset,
765                    base(),
766                    "BalanceInsufficient must name the base asset (BTC), not the quote (USDT)"
767                );
768            }
769            other => panic!("expected BalanceInsufficient, got: {other:?}"),
770        }
771    }
772
773    #[test]
774    fn bid_ask_fill_model_fills_at_ask_price_and_deducts_correct_balance() {
775        let mut exchange = make_exchange("0", "10000"); // 0 BTC, 10 000 USDT
776        exchange.fill_model = SimFillConfig::BidAsk(BidAskFillModel);
777
778        let market_prices = MarketPrices {
779            best_bid: Some(d("99.5")),
780            best_ask: Some(d("100.5")),
781            last_price: Some(d("100.0")),
782        };
783
784        // Market buy of 1 BTC; reference price 100 is only used as a fallback
785        // when fill_model returns None — BidAsk returns best_ask so it is not used.
786        let (response, notifications) = exchange.open_order(buy_request("1", "100"), market_prices);
787
788        assert!(
789            response.state.is_accepted(),
790            "buy should succeed: {:?}",
791            response.state
792        );
793        let notifs = notifications.expect("successful buy must produce notifications");
794
795        // BidAskFillModel: market buy fills at best_ask = 100.5, not last_price 100.0.
796        assert_eq!(
797            notifs.trade.price,
798            d("100.5"),
799            "fill price must be best_ask"
800        );
801
802        // Balance deduction: 1 * 100.5 = 100.5 USDT; fee_model = Zero.
803        let usdt = exchange.account.balance_mut(&quote()).unwrap();
804        assert_eq!(
805            usdt.balance.free,
806            d("9899.5"),
807            "quote balance must decrease by fill_price * qty"
808        );
809    }
810
811    #[test]
812    fn percentage_fee_model_deducts_correct_fee_on_buy() {
813        // 0.1% fee rate
814        let fee_model = FeeModelConfig::Percentage(PercentageFeeModel { rate: d("0.001") });
815        let mut exchange = make_exchange_with_fee("0", "10000", fee_model);
816
817        // Buy 10 BTC at price 100 USDT each
818        // Notional = 10 * 100 = 1000 USDT
819        // Fee = 1000 * 0.001 = 1 USDT
820        // Total deducted = 1000 + 1 = 1001 USDT
821        let (response, notifications) =
822            exchange.open_order(buy_request("10", "100"), MarketPrices::default());
823
824        assert!(
825            response.state.is_accepted(),
826            "buy should succeed: {:?}",
827            response.state
828        );
829        let notifs = notifications.expect("successful buy must produce notifications");
830
831        // Trade must report fee in quote denomination
832        assert_eq!(notifs.trade.fees.fees, d("1"), "trade fee must be 1 USDT");
833
834        // Quote balance: 10000 - 1001 = 8999
835        let usdt = exchange.account.balance_mut(&quote()).unwrap();
836        assert_eq!(
837            usdt.balance.free,
838            d("8999"),
839            "quote balance must decrease by notional + fee"
840        );
841    }
842
843    #[test]
844    fn percentage_fee_model_deducts_correct_fee_on_sell() {
845        // 0.1% fee rate
846        let fee_model = FeeModelConfig::Percentage(PercentageFeeModel { rate: d("0.001") });
847        let mut exchange = make_exchange_with_fee("10", "0", fee_model);
848
849        // Sell 1 BTC at price 100 USDT
850        // Notional = 1 * 100 = 100 USDT
851        // Fee (quote) = 100 * 0.001 = 0.1 USDT
852        // Fee (base) = 0.1 / 100 = 0.001 BTC
853        // Total base deducted = 1 + 0.001 = 1.001 BTC
854        let (response, notifications) =
855            exchange.open_order(sell_request("1", "100"), MarketPrices::default());
856
857        assert!(
858            response.state.is_accepted(),
859            "sell should succeed: {:?}",
860            response.state
861        );
862        let notifs = notifications.expect("successful sell must produce notifications");
863
864        // Trade must report fee in quote denomination
865        assert_eq!(
866            notifs.trade.fees.fees,
867            d("0.1"),
868            "trade fee must be 0.1 USDT"
869        );
870
871        // Base balance: 10 - 1.001 = 8.999
872        let btc = exchange.account.balance_mut(&base()).unwrap();
873        assert_eq!(
874            btc.balance.free,
875            d("8.999"),
876            "base balance must decrease by quantity + fee_in_base"
877        );
878    }
879
880    #[test]
881    fn percentage_fee_with_zero_price_returns_zero_fee() {
882        // Edge case: if fill_price is zero, fee computation must not divide by zero
883        let fee_model = FeeModelConfig::Percentage(PercentageFeeModel { rate: d("0.001") });
884        let mut exchange = make_exchange_with_fee("10", "0", fee_model);
885
886        // Sell 1 BTC at price 0 (degenerate case)
887        // Fee (quote) = 0 * 0.001 * 1 = 0
888        // Fee (base) = guarded by is_zero() check, returns 0
889        let (response, notifications) =
890            exchange.open_order(sell_request("1", "0"), MarketPrices::default());
891
892        assert!(
893            response.state.is_accepted(),
894            "sell at zero price should succeed: {:?}",
895            response.state
896        );
897        let notifs = notifications.expect("successful sell must produce notifications");
898
899        // Fee must be zero (not NaN or panic from division by zero)
900        assert_eq!(
901            notifs.trade.fees.fees,
902            Decimal::ZERO,
903            "fee must be zero when price is zero"
904        );
905
906        // Base balance: 10 - 1 = 9 (no fee deducted)
907        let btc = exchange.account.balance_mut(&base()).unwrap();
908        assert_eq!(
909            btc.balance.free,
910            d("9"),
911            "base balance must decrease by quantity only"
912        );
913    }
914}