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    #[allow(clippy::expect_used)] // Mock exchange: panic if test data is incomplete
295    pub fn open_order(
296        &mut self,
297        request: OrderRequestOpen<ExchangeId, InstrumentNameExchange>,
298        market_prices: MarketPrices,
299    ) -> (
300        Order<ExchangeId, InstrumentNameExchange, UnindexedOrderState>,
301        Option<OpenOrderNotifications>,
302    ) {
303        if let Err(error) = self.validate_order_kind_supported(request.state.kind) {
304            return (build_open_order_err_response(request, error), None);
305        }
306
307        let underlying = match self.find_instrument_data(&request.key.instrument) {
308            Ok(instrument) => instrument.underlying.clone(),
309            Err(error) => return (build_open_order_err_response(request, error), None),
310        };
311
312        // Compute fill price via the configured FillModel.
313        //
314        // For limit orders, pass the limit price as `order_price`; for market orders pass `None`
315        // so the model can select the best available market price (bid/ask/last).  When no market
316        // data is present (standard MockExecution passes all-None MarketPrices), `request.state.price`
317        // is used as the `last_price` fallback so behaviour is identical to the pre-FillModel path.
318        //
319        // Invariant: `fill_price` is only called for marketable orders. `validate_order_kind_supported`
320        // (called above) currently rejects Limit orders, ensuring FillModel::fill_price never receives
321        // a non-marketable limit order. If Limit support is added later, the fill model must enforce
322        // limit-price semantics (e.g. a limit buy must not fill above the limit price).
323        let fill_price = self
324            .fill_model
325            .fill_price(
326                request.state.side,
327                match request.state.kind {
328                    // unreachable: validate_order_kind_supported (called above) already
329                    // rejects non-Market orders with Err, so these arms are never reached.
330                    // Kept for exhaustiveness; passes the limit/trigger price so fill models
331                    // that gain support in future behave correctly without a separate change.
332                    OrderKind::Market => None,
333                    OrderKind::Limit
334                    | OrderKind::StopLimit { .. }
335                    | OrderKind::TrailingStopLimit { .. } => request.state.price,
336                    OrderKind::Stop { trigger_price }
337                    | OrderKind::TrailingStop {
338                        offset: trigger_price,
339                        ..
340                    } => Some(trigger_price),
341                },
342                market_prices.best_bid,
343                market_prices.best_ask,
344                market_prices.last_price.or(request.state.price),
345            )
346            .or(request.state.price)
347            .expect("fill_price must be available from market data or request price");
348
349        let time_exchange = self.time_exchange();
350
351        // Compute fee using the configured FeeModel. For spot, contract_size = 1.
352        let order_fees_quote =
353            self.fee_model
354                .compute_fee(fill_price, request.state.quantity, Decimal::ONE);
355
356        let balance_change_result = match request.state.side {
357            Side::Buy => {
358                // Buying Instrument requires sufficient QuoteAsset Balance
359                #[allow(clippy::expect_used)]
360                // Invariant: MockExchange - balances exist for all configured instruments
361                let current = self
362                    .account
363                    .balance_mut(&underlying.quote)
364                    .expect("MockExchange has Balance for all configured Instrument assets");
365
366                // Currently we only supported MarketKind orders, so they should be identical
367                assert_eq!(current.balance.total, current.balance.free);
368
369                let order_value_quote = fill_price * request.state.quantity.abs();
370                let quote_required = order_value_quote + order_fees_quote;
371
372                let maybe_new_balance = current.balance.free - quote_required;
373
374                if maybe_new_balance >= Decimal::ZERO {
375                    current.balance.free = maybe_new_balance;
376                    current.balance.total = maybe_new_balance;
377                    current.time_exchange = time_exchange;
378
379                    Ok((
380                        current.clone(),
381                        AssetFees::new(
382                            underlying.quote.clone(),
383                            order_fees_quote,
384                            Some(order_fees_quote),
385                        ),
386                    ))
387                } else {
388                    Err(ApiError::BalanceInsufficient(
389                        underlying.quote.clone(),
390                        format!(
391                            "Available Balance: {}, Required Balance inc. fees: {}",
392                            current.balance.free, quote_required
393                        ),
394                    ))
395                }
396            }
397            Side::Sell => {
398                // Selling Instrument requires sufficient BaseAsset Balance
399                #[allow(clippy::expect_used)]
400                // Invariant: MockExchange - balances exist for all configured instruments
401                let current = self
402                    .account
403                    .balance_mut(&underlying.base)
404                    .expect("MockExchange has Balance for all configured Instrument assets");
405
406                // Currently we only supported MarketKind orders, so they should be identical
407                assert_eq!(current.balance.total, current.balance.free);
408
409                let order_value_base = request.state.quantity.abs();
410                // Fee is quote-denominated; convert to base for deduction.
411                // Note: For PerContractFeeModel this conversion is nonsensical (flat USD / price),
412                // but MockExchange is spot-only so PerContract isn't used in practice.
413                debug_assert!(
414                    !matches!(self.fee_model, FeeModelConfig::PerContract(_)),
415                    "PerContractFeeModel produces nonsensical base-denominated fees on sell path"
416                );
417                let order_fees_base = if fill_price.is_zero() {
418                    Decimal::ZERO
419                } else {
420                    order_fees_quote / fill_price
421                };
422                let base_required = order_value_base + order_fees_base;
423
424                let maybe_new_balance = current.balance.free - base_required;
425
426                if maybe_new_balance >= Decimal::ZERO {
427                    current.balance.free = maybe_new_balance;
428                    current.balance.total = maybe_new_balance;
429                    current.time_exchange = time_exchange;
430
431                    Ok((
432                        current.clone(),
433                        AssetFees::new(
434                            underlying.quote.clone(),
435                            order_fees_quote,
436                            Some(order_fees_quote),
437                        ),
438                    ))
439                } else {
440                    Err(ApiError::BalanceInsufficient(
441                        underlying.base,
442                        format!(
443                            "Available Balance: {}, Required Balance inc. fees: {}",
444                            current.balance.free, base_required
445                        ),
446                    ))
447                }
448            }
449        };
450
451        let (balance_snapshot, fees) = match balance_change_result {
452            Ok((balance_snapshot, fees)) => (Snapshot(balance_snapshot), fees),
453            Err(error) => return (build_open_order_err_response(request, error), None),
454        };
455
456        let order_id = self.order_id_sequence_fetch_add();
457        let trade_id = TradeId(order_id.0.clone());
458
459        let order_response = Order {
460            key: request.key.clone(),
461            side: request.state.side,
462            price: request.state.price,
463            quantity: request.state.quantity,
464            kind: request.state.kind,
465            time_in_force: request.state.time_in_force,
466            state: OrderState::fully_filled(Filled::new(
467                order_id.clone(),
468                self.time_exchange(),
469                request.state.quantity,
470                Some(fill_price),
471            )),
472        };
473
474        let notifications = OpenOrderNotifications {
475            balance: balance_snapshot,
476            trade: Trade {
477                id: trade_id,
478                order_id: order_id.clone(),
479                instrument: request.key.instrument,
480                strategy: request.key.strategy,
481                time_exchange: self.time_exchange(),
482                side: request.state.side,
483                price: fill_price,
484                quantity: request.state.quantity,
485                fees,
486            },
487        };
488
489        (order_response, Some(notifications))
490    }
491
492    pub fn validate_order_kind_supported(
493        &self,
494        order_kind: OrderKind,
495    ) -> Result<(), UnindexedOrderError> {
496        if order_kind == OrderKind::Market {
497            Ok(())
498        } else {
499            Err(UnindexedOrderError::Rejected(ApiError::OrderRejected(
500                format!("MockExchange does not support OrderKind::{order_kind:?}"),
501            )))
502        }
503    }
504
505    pub fn find_instrument_data(
506        &self,
507        instrument: &InstrumentNameExchange,
508    ) -> Result<&Instrument<ExchangeId, AssetNameExchange>, UnindexedApiError> {
509        self.instruments.get(instrument).ok_or_else(|| {
510            ApiError::InstrumentInvalid(
511                instrument.clone(),
512                format!("MockExchange is not set-up for managing: {instrument}"),
513            )
514        })
515    }
516
517    fn order_id_sequence_fetch_add(&mut self) -> OrderId {
518        let sequence = self.order_sequence;
519        self.order_sequence += 1;
520        OrderId::new(sequence.to_smolstr())
521    }
522
523    fn build_account_event<Kind>(&self, kind: Kind) -> UnindexedAccountEvent
524    where
525        Kind: Into<AccountEventKind<ExchangeId, AssetNameExchange, InstrumentNameExchange>>,
526    {
527        UnindexedAccountEvent {
528            exchange: self.exchange,
529            kind: kind.into(),
530        }
531    }
532}
533
534fn build_open_order_err_response<E>(
535    request: OrderRequestOpen<ExchangeId, InstrumentNameExchange>,
536    error: E,
537) -> Order<ExchangeId, InstrumentNameExchange, UnindexedOrderState>
538where
539    E: Into<UnindexedOrderError>,
540{
541    Order {
542        key: request.key,
543        side: request.state.side,
544        price: request.state.price,
545        quantity: request.state.quantity,
546        kind: request.state.kind,
547        time_in_force: request.state.time_in_force,
548        state: OrderState::inactive(error.into()),
549    }
550}
551
552#[derive(Debug)]
553pub struct OpenOrderNotifications {
554    pub balance: Snapshot<AssetBalance<AssetNameExchange>>,
555    pub trade: Trade<AssetNameExchange, InstrumentNameExchange>,
556}
557
558#[cfg(test)]
559#[allow(clippy::unwrap_used, clippy::expect_used)] // Test code: panics on bad input are acceptable
560mod tests {
561    use super::*;
562    use crate::{
563        UnindexedAccountSnapshot,
564        balance::{AssetBalance, Balance},
565        error::ApiError,
566        exchange::mock::request::MarketPrices,
567        fee::{FeeModelConfig, PercentageFeeModel},
568        fill::{BidAskFillModel, SimFillConfig},
569        order::{
570            OrderEvent, OrderKey, OrderKind, TimeInForce,
571            id::{ClientOrderId, StrategyId},
572            request::RequestOpen,
573            state::InactiveOrderState,
574        },
575    };
576    use chrono::Utc;
577    use rust_decimal::Decimal;
578    use rustrade_instrument::{
579        Side, Underlying,
580        asset::name::AssetNameExchange,
581        exchange::ExchangeId,
582        instrument::{
583            Instrument,
584            kind::InstrumentKind,
585            name::{InstrumentNameExchange, InstrumentNameInternal},
586            quote::InstrumentQuoteAsset,
587        },
588    };
589    use tokio::sync::{broadcast, mpsc};
590
591    fn d(s: &str) -> Decimal {
592        s.parse().unwrap()
593    }
594
595    const EXCHANGE: ExchangeId = ExchangeId::BinanceSpot;
596
597    fn base() -> AssetNameExchange {
598        AssetNameExchange::new("BTC")
599    }
600
601    fn quote() -> AssetNameExchange {
602        AssetNameExchange::new("USDT")
603    }
604
605    fn instrument_name() -> InstrumentNameExchange {
606        InstrumentNameExchange::new("BTCUSDT")
607    }
608
609    fn make_exchange(btc: &str, usdt: &str) -> MockExchange {
610        make_exchange_with_fee(btc, usdt, FeeModelConfig::default())
611    }
612
613    fn make_exchange_with_fee(btc: &str, usdt: &str, fee_model: FeeModelConfig) -> MockExchange {
614        let btc = d(btc);
615        let usdt = d(usdt);
616        let initial_state = UnindexedAccountSnapshot {
617            exchange: EXCHANGE,
618            balances: vec![
619                AssetBalance {
620                    asset: base(),
621                    balance: Balance {
622                        total: btc,
623                        free: btc,
624                    },
625                    time_exchange: Utc::now(),
626                },
627                AssetBalance {
628                    asset: quote(),
629                    balance: Balance {
630                        total: usdt,
631                        free: usdt,
632                    },
633                    time_exchange: Utc::now(),
634                },
635            ],
636            instruments: vec![],
637        };
638
639        let config = MockExecutionConfig::new(
640            EXCHANGE,
641            initial_state,
642            0, // latency_ms
643            fee_model,
644            SimFillConfig::default(),
645        );
646
647        let (_tx, request_rx) = mpsc::unbounded_channel();
648        let (event_tx, _) = broadcast::channel(1);
649
650        let mut instruments = FnvHashMap::default();
651        instruments.insert(
652            instrument_name(),
653            Instrument {
654                exchange: EXCHANGE,
655                name_internal: InstrumentNameInternal::new("btcusdt"),
656                name_exchange: instrument_name(),
657                underlying: Underlying {
658                    base: base(),
659                    quote: quote(),
660                },
661                quote: InstrumentQuoteAsset::UnderlyingQuote,
662                kind: InstrumentKind::Spot,
663                spec: None,
664            },
665        );
666
667        MockExchange::new(config, request_rx, event_tx, instruments)
668    }
669
670    fn buy_request(quantity: &str) -> OrderRequestOpen<ExchangeId, InstrumentNameExchange> {
671        let quantity = d(quantity);
672        OrderEvent {
673            key: OrderKey {
674                exchange: EXCHANGE,
675                instrument: instrument_name(),
676                strategy: StrategyId::new("test"),
677                cid: ClientOrderId::new("test-cid"),
678            },
679            state: RequestOpen {
680                side: Side::Buy,
681                price: None, // Market orders don't have a limit price
682                quantity,
683                kind: OrderKind::Market,
684                time_in_force: TimeInForce::ImmediateOrCancel,
685                position_id: None,
686                reduce_only: false,
687            },
688        }
689    }
690
691    fn sell_request(quantity: &str) -> OrderRequestOpen<ExchangeId, InstrumentNameExchange> {
692        let quantity = d(quantity);
693        OrderEvent {
694            key: OrderKey {
695                exchange: EXCHANGE,
696                instrument: instrument_name(),
697                strategy: StrategyId::new("test"),
698                cid: ClientOrderId::new("test-cid"),
699            },
700            state: RequestOpen {
701                side: Side::Sell,
702                price: None, // Market orders don't have a limit price
703                quantity,
704                kind: OrderKind::Market,
705                time_in_force: TimeInForce::ImmediateOrCancel,
706                position_id: None,
707                reduce_only: false,
708            },
709        }
710    }
711
712    fn market_prices(price: &str) -> MarketPrices {
713        let p = Some(d(price));
714        MarketPrices {
715            best_bid: p,
716            best_ask: p,
717            last_price: p,
718        }
719    }
720
721    #[test]
722    fn sell_order_decrements_base_balance_not_quote() {
723        let mut exchange = make_exchange("1.0", "10000");
724        let initial_usdt = d("10000");
725
726        let (response, notifications) =
727            exchange.open_order(sell_request("0.5"), market_prices("50000"));
728
729        assert!(
730            response.state.is_accepted(),
731            "sell should succeed: {:?}",
732            response.state
733        );
734        assert!(
735            notifications.is_some(),
736            "successful sell must produce notifications"
737        );
738
739        // Base (BTC) must be decremented by the quantity sold.
740        let btc = exchange.account.balance_mut(&base()).unwrap();
741        assert_eq!(
742            btc.balance.free,
743            d("0.5"),
744            "base balance should decrease by quantity sold"
745        );
746
747        // Quote (USDT) must be unchanged (fees = 0 in this test).
748        let usdt = exchange.account.balance_mut(&quote()).unwrap();
749        assert_eq!(
750            usdt.balance.free, initial_usdt,
751            "quote balance should be unchanged on sell"
752        );
753    }
754
755    #[test]
756    fn sell_order_insufficient_balance_names_base_asset() {
757        // Regression guard for the sell-side balance bug fixed in this branch:
758        // previously `balance_mut(&underlying.quote)` was called for sells, so
759        // BalanceInsufficient would name the quote asset (USDT) instead of the base (BTC).
760        let mut exchange = make_exchange("0.1", "10000");
761
762        let (response, notifications) = exchange.open_order(
763            sell_request("1.0"), // selling 1 BTC but only 0.1 available
764            market_prices("50000"),
765        );
766
767        assert!(
768            notifications.is_none(),
769            "failed order must produce no notifications"
770        );
771        match response.state {
772            OrderState::Inactive(InactiveOrderState::OpenFailed(
773                crate::error::OrderError::Rejected(ApiError::BalanceInsufficient(ref asset, _)),
774            )) => {
775                assert_eq!(
776                    *asset,
777                    base(),
778                    "BalanceInsufficient must name the base asset (BTC), not the quote (USDT)"
779                );
780            }
781            other => panic!("expected BalanceInsufficient, got: {other:?}"),
782        }
783    }
784
785    #[test]
786    fn bid_ask_fill_model_fills_at_ask_price_and_deducts_correct_balance() {
787        let mut exchange = make_exchange("0", "10000"); // 0 BTC, 10 000 USDT
788        exchange.fill_model = SimFillConfig::BidAsk(BidAskFillModel);
789
790        let market_prices = MarketPrices {
791            best_bid: Some(d("99.5")),
792            best_ask: Some(d("100.5")),
793            last_price: Some(d("100.0")),
794        };
795
796        // Market buy of 1 BTC; reference price 100 is only used as a fallback
797        // when fill_model returns None — BidAsk returns best_ask so it is not used.
798        let (response, notifications) = exchange.open_order(buy_request("1"), market_prices);
799
800        assert!(
801            response.state.is_accepted(),
802            "buy should succeed: {:?}",
803            response.state
804        );
805        let notifs = notifications.expect("successful buy must produce notifications");
806
807        // BidAskFillModel: market buy fills at best_ask = 100.5, not last_price 100.0.
808        assert_eq!(
809            notifs.trade.price,
810            d("100.5"),
811            "fill price must be best_ask"
812        );
813
814        // Balance deduction: 1 * 100.5 = 100.5 USDT; fee_model = Zero.
815        let usdt = exchange.account.balance_mut(&quote()).unwrap();
816        assert_eq!(
817            usdt.balance.free,
818            d("9899.5"),
819            "quote balance must decrease by fill_price * qty"
820        );
821    }
822
823    #[test]
824    fn percentage_fee_model_deducts_correct_fee_on_buy() {
825        // 0.1% fee rate
826        let fee_model = FeeModelConfig::Percentage(PercentageFeeModel { rate: d("0.001") });
827        let mut exchange = make_exchange_with_fee("0", "10000", fee_model);
828
829        // Buy 10 BTC at price 100 USDT each
830        // Notional = 10 * 100 = 1000 USDT
831        // Fee = 1000 * 0.001 = 1 USDT
832        // Total deducted = 1000 + 1 = 1001 USDT
833        let (response, notifications) =
834            exchange.open_order(buy_request("10"), market_prices("100"));
835
836        assert!(
837            response.state.is_accepted(),
838            "buy should succeed: {:?}",
839            response.state
840        );
841        let notifs = notifications.expect("successful buy must produce notifications");
842
843        // Trade must report fee in quote denomination
844        assert_eq!(notifs.trade.fees.fees, d("1"), "trade fee must be 1 USDT");
845
846        // Quote balance: 10000 - 1001 = 8999
847        let usdt = exchange.account.balance_mut(&quote()).unwrap();
848        assert_eq!(
849            usdt.balance.free,
850            d("8999"),
851            "quote balance must decrease by notional + fee"
852        );
853    }
854
855    #[test]
856    fn percentage_fee_model_deducts_correct_fee_on_sell() {
857        // 0.1% fee rate
858        let fee_model = FeeModelConfig::Percentage(PercentageFeeModel { rate: d("0.001") });
859        let mut exchange = make_exchange_with_fee("10", "0", fee_model);
860
861        // Sell 1 BTC at price 100 USDT
862        // Notional = 1 * 100 = 100 USDT
863        // Fee (quote) = 100 * 0.001 = 0.1 USDT
864        // Fee (base) = 0.1 / 100 = 0.001 BTC
865        // Total base deducted = 1 + 0.001 = 1.001 BTC
866        let (response, notifications) =
867            exchange.open_order(sell_request("1"), market_prices("100"));
868
869        assert!(
870            response.state.is_accepted(),
871            "sell should succeed: {:?}",
872            response.state
873        );
874        let notifs = notifications.expect("successful sell must produce notifications");
875
876        // Trade must report fee in quote denomination
877        assert_eq!(
878            notifs.trade.fees.fees,
879            d("0.1"),
880            "trade fee must be 0.1 USDT"
881        );
882
883        // Base balance: 10 - 1.001 = 8.999
884        let btc = exchange.account.balance_mut(&base()).unwrap();
885        assert_eq!(
886            btc.balance.free,
887            d("8.999"),
888            "base balance must decrease by quantity + fee_in_base"
889        );
890    }
891
892    #[test]
893    fn percentage_fee_with_zero_price_returns_zero_fee() {
894        // Edge case: if fill_price is zero, fee computation must not divide by zero
895        let fee_model = FeeModelConfig::Percentage(PercentageFeeModel { rate: d("0.001") });
896        let mut exchange = make_exchange_with_fee("10", "0", fee_model);
897
898        // Sell 1 BTC at price 0 (degenerate case)
899        // Fee (quote) = 0 * 0.001 * 1 = 0
900        // Fee (base) = guarded by is_zero() check, returns 0
901        let (response, notifications) = exchange.open_order(sell_request("1"), market_prices("0"));
902
903        assert!(
904            response.state.is_accepted(),
905            "sell at zero price should succeed: {:?}",
906            response.state
907        );
908        let notifs = notifications.expect("successful sell must produce notifications");
909
910        // Fee must be zero (not NaN or panic from division by zero)
911        assert_eq!(
912            notifs.trade.fees.fees,
913            Decimal::ZERO,
914            "fee must be zero when price is zero"
915        );
916
917        // Base balance: 10 - 1 = 9 (no fee deducted)
918        let btc = exchange.account.balance_mut(&base()).unwrap();
919        assert_eq!(
920            btc.balance.free,
921            d("9"),
922            "base balance must decrease by quantity only"
923        );
924    }
925}