o2-tools 0.1.17

Reusable tooling for trade account and order book contract interactions on Fuel
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
use crate::{
    helpers::get_total_amount,
    market_data::OrderData,
    order_book::Side,
};
use blart::{
    AsBytes,
    NoPrefixesBytes,
    TreeMap,
};
use fuels::{
    tx::TxId,
    types::{
        AssetId,
        Identity,
    },
};
use indexmap::IndexMap;
use slotmap::{
    DefaultKey,
    SlotMap,
};
use std::fmt::Debug;

pub type Price = u64;
pub type Quantity = u64;
pub type OrderId = DefaultKey;
pub type Balances = IndexMap<Identity, (u64, u64)>;

/// big endian representation of price for use as radix key
#[derive(Debug, Clone)]
struct PriceKey([u8; 8]);

impl From<Price> for PriceKey {
    fn from(value: Price) -> Self {
        Self(value.to_be_bytes())
    }
}

impl From<&Price> for PriceKey {
    fn from(value: &Price) -> Self {
        Self(value.to_be_bytes())
    }
}

impl From<&mut Price> for PriceKey {
    fn from(value: &mut Price) -> Self {
        Self(value.to_be_bytes())
    }
}

impl From<PriceKey> for Price {
    fn from(value: PriceKey) -> Self {
        u64::from_be_bytes(value.0)
    }
}

impl From<&PriceKey> for Price {
    fn from(value: &PriceKey) -> Self {
        u64::from_be_bytes(value.0)
    }
}

impl From<&mut PriceKey> for Price {
    fn from(value: &mut PriceKey) -> Self {
        u64::from_be_bytes(value.0)
    }
}

impl AsBytes for PriceKey {
    fn as_bytes(&self) -> &[u8] {
        &self.0
    }
}

unsafe impl NoPrefixesBytes for PriceKey {}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Fill {
    pub order_id: u64,
    pub quantity: u64,
    pub price: u64,
}

#[derive(Debug)]
pub struct OrderParams {
    pub side: Side,
    pub price: u64,
    pub quantity: u64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Order {
    pub trader_id: Identity,
    pub order_id: u64,
    pub side: Side,
    pub price: u64,
    pub quantity: Quantity,
    pub fill: Vec<Fill>,
    pub tx_id: Option<TxId>,
}

impl Order {
    pub fn new(
        order_id: u64,
        OrderData {
            trader_id,
            side,
            price,
            quantity,
        }: OrderData,
    ) -> Self {
        Self {
            order_id,
            trader_id,
            price,
            quantity,
            side,
            fill: vec![],
            tx_id: None,
        }
    }

    pub fn fill(&mut self, order_id: u64, fill_quantity: Quantity, price: Price) -> bool {
        let filled_order = Fill {
            quantity: fill_quantity,
            price,
            order_id,
        };
        self.fill.push(filled_order);
        self.quantity -= fill_quantity;
        self.quantity == 0
    }

    pub fn total(&self, decimals: u64) -> u64 {
        get_total_amount(self.quantity, self.price, decimals)
    }

    pub fn filled_total(&self, decimals: u64) -> u64 {
        self.fill
            .iter()
            .map(|f| get_total_amount(f.quantity, f.price, decimals))
            .sum()
    }

    pub fn remaining_total(&self, decimals: u64) -> Quantity {
        match self.side {
            Side::Buy => {
                get_total_amount(self.total_quantity(), self.price, decimals)
                    - self.filled_total(decimals)
            }
            Side::Sell => 0,
        }
    }

    pub fn total_quantity(&self) -> Quantity {
        self.quantity + self.filled_quantity()
    }

    pub fn filled_quantity(&self) -> Quantity {
        self.fill.iter().map(|q| q.quantity).sum::<Quantity>()
    }

    pub fn filled_price(&self) -> Price {
        let total_price: Price = self.fill.iter().map(|q| q.price).sum();
        let count = self.fill.len();

        if count == 0 {
            0
        } else {
            total_price / count as Price
        }
    }

    pub fn is_closed(&self) -> bool {
        self.quantity == 0
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MatchedEvent {
    pub direction: Side,
    pub taker_id: u64,
    pub maker_id: u64,
    pub quantity: u64,
    pub price: u64,
    pub total: u64,
}

#[derive(Clone, Default)]
pub struct BookConfig {
    pub base_asset: AssetId,
    pub base_decimals: u64,
    pub quote_asset: AssetId,
    pub quote_decimals: u64,
    pub taker_fee: u64,
    pub maker_fee: u64,
}

#[derive(Clone)]
pub struct Book {
    pub order_id: u64,
    pub base_asset: AssetId,
    pub base_decimals: u64,
    pub quote_asset: AssetId,
    pub quote_decimals: u64,
    pub taker_fee: u64,
    pub maker_fee: u64,
    pub all_orders: Vec<Order>,
    pub trades: Vec<MatchedEvent>,
    pub cancels: Vec<Order>,
    pub balances: Balances,
    buys: TreeMap<PriceKey, Vec<OrderId>>,
    sells: TreeMap<PriceKey, Vec<OrderId>>,
    orders: SlotMap<OrderId, Order>,
}

impl Book {
    pub fn new(config: BookConfig) -> Self {
        Self {
            order_id: 0,
            base_asset: config.base_asset,
            base_decimals: config.base_decimals,
            quote_asset: config.quote_asset,
            quote_decimals: config.quote_decimals,
            taker_fee: config.taker_fee,
            maker_fee: config.taker_fee,
            buys: TreeMap::new(),
            sells: TreeMap::new(),
            orders: SlotMap::new(),
            trades: Vec::new(),
            cancels: Vec::new(),
            all_orders: Vec::new(),
            balances: Balances::new(),
        }
    }

    pub fn subtract_fee(&self, is_maker: bool, amount: u64) -> u64 {
        let fee: u64 = if is_maker {
            self.maker_fee
        } else {
            self.taker_fee
        };
        if fee == 0 {
            return amount;
        }
        amount - ((amount * fee) / 1_000_000)
    }

    pub fn subtract_taker_fee(&self, amount: u64) -> u64 {
        self.subtract_fee(false, amount)
    }

    pub fn subtract_maker_fee(&self, amount: u64) -> u64 {
        self.subtract_fee(true, amount)
    }

    pub fn set_address_balance(
        &mut self,
        trader_id: &Identity,
        base_balance: u64,
        quote_balance: u64,
    ) {
        self.balances
            .insert(*trader_id, (base_balance, quote_balance));
    }

    pub fn set_balances(&mut self, balances: Balances) {
        self.balances = balances.clone();
    }

    pub fn change_balance_insert(&mut self, order: &Order) {
        if let Some(balance) = self.balances.get_mut(&order.trader_id) {
            if order.side == Side::Buy {
                balance.1 -= get_total_amount(
                    order.total_quantity(),
                    order.price,
                    self.base_decimals,
                );
            } else {
                balance.0 -= order.total_quantity();
            };
        }
    }

    pub fn change_balance_cancel(&mut self, order: &Order) {
        if let Some(balance) = self.balances.get_mut(&order.trader_id) {
            if order.side == Side::Buy {
                balance.1 +=
                    get_total_amount(order.quantity, order.price, self.base_decimals);
            } else {
                balance.0 += order.quantity;
            };
        }
    }

    pub fn change_balance_match(
        &mut self,
        maker: &Order,
        taker: &Order,
        fill_quantity: u64,
    ) {
        let maker_balances = self.balances.get(&maker.trader_id);
        let taker_balances = self.balances.get(&taker.trader_id);

        if maker_balances.is_none() || taker_balances.is_none() {
            return;
        }
        let mut maker_balances = maker_balances.cloned().unwrap();
        let mut taker_balances = taker_balances.cloned().unwrap();

        if maker.side == Side::Buy {
            maker_balances.0 += self.subtract_maker_fee(fill_quantity);
            taker_balances.1 += self.subtract_taker_fee(get_total_amount(
                fill_quantity,
                maker.price,
                self.base_decimals,
            ));
        } else {
            taker_balances.0 += self.subtract_taker_fee(fill_quantity);
            maker_balances.1 += self.subtract_maker_fee(get_total_amount(
                fill_quantity,
                maker.price,
                self.base_decimals,
            ));
        }

        if taker.is_closed() && taker.remaining_total(self.base_decimals) > 0 {
            taker_balances.1 += taker.remaining_total(self.base_decimals);
        }
        if maker.is_closed() && maker.remaining_total(self.base_decimals) > 0 {
            taker_balances.1 += taker.remaining_total(self.base_decimals);
        }

        self.balances.insert(maker.trader_id, maker_balances);
        self.balances.insert(taker.trader_id, taker_balances);
    }

    pub fn load(&mut self, orders: Vec<Order>) {
        for order in orders {
            if order.quantity == 0 {
                continue;
            }
            self.insert_order(&order);
        }
    }

    pub fn get_buys_count(&mut self) -> usize {
        self.buys.len()
    }

    pub fn get_sells_count(&mut self) -> usize {
        self.sells.len()
    }

    fn insert_order(&mut self, order: &Order) -> OrderId {
        let price = order.price;
        let id = self.orders.insert(order.clone());
        self.change_balance_insert(order);

        match order.side {
            Side::Sell => {
                self.sells.entry(price.into()).or_default().push(id);
            }
            Side::Buy => {
                self.buys.entry(price.into()).or_default().push(id);
            }
        }
        self.execute();
        id
    }

    pub fn get_orders(&self) -> Vec<Order> {
        self.orders.values().cloned().collect()
    }

    pub fn cancel(&mut self, order_id: u64) {
        if let Some((order_key, order)) =
            self.orders.iter().find(|o| o.1.order_id == order_id)
        {
            self.cancels.push(order.clone());
            self.change_balance_cancel(&order.clone());
            self.remove_order(order_key);
        }
    }

    fn remove_order(&mut self, order_id: OrderId) {
        let order = &self.orders[order_id];
        match order.side {
            Side::Sell => {
                if let Some(ref_vec) = self.sells.get_mut(&order.price.into()) {
                    ref_vec.retain(|id| *id != order_id);
                    if ref_vec.is_empty() {
                        self.sells.remove(&order.price.into());
                    }
                }
            }
            Side::Buy => {
                if let Some(ref_vec) = self.buys.get_mut(&order.price.into()) {
                    ref_vec.retain(|id| *id != order_id);
                    if ref_vec.is_empty() {
                        self.buys.remove(&order.price.into());
                    }
                }
            }
        }
        self.orders.remove(order_id);
    }

    fn lowest_sell(&self) -> Option<(&PriceKey, &OrderId)> {
        match self.sells.first_key_value().map(|(k, v)| (k, v.first())) {
            Some((k, Some(v))) => Some((k, v)),
            _ => None,
        }
    }

    fn highest_buy(&self) -> Option<(&PriceKey, &OrderId)> {
        match self.buys.last_key_value().map(|(k, v)| (k, v.first())) {
            Some((k, Some(v))) => Some((k, v)),
            _ => None,
        }
    }

    pub fn create_order_matched_event(&self, maker_order: &Order) -> MatchedEvent {
        let last_filled_order = maker_order.fill.last().unwrap();
        let total = get_total_amount(
            last_filled_order.quantity,
            last_filled_order.price,
            self.base_decimals,
        );
        MatchedEvent {
            direction: maker_order.side,
            taker_id: last_filled_order.order_id,
            maker_id: maker_order.order_id,
            quantity: last_filled_order.quantity,
            price: last_filled_order.price,
            total,
        }
    }

    pub fn execute(&mut self) {
        while self
            .lowest_sell()
            .zip(self.highest_buy())
            .map(|((sell, _), (buy, _))| Price::from(buy) >= Price::from(sell))
            .unwrap_or(false)
        {
            let sell_id = *self.lowest_sell().unwrap().1;
            let sell_quantity = self.orders[sell_id].quantity;
            let sell_order_id = self.orders[sell_id].order_id;
            let buy_id = *self.highest_buy().unwrap().1;
            let buy_quantity = self.orders[buy_id].quantity;
            let buy_order_id = self.orders[buy_id].order_id;
            let fill_quantity = sell_quantity.min(buy_quantity);

            let maker_id = if sell_order_id < buy_order_id {
                sell_id
            } else {
                buy_id
            };
            let taker_id = if sell_order_id > buy_order_id {
                sell_id
            } else {
                buy_id
            };
            let maker_price = self.orders[maker_id].price;

            let sell_is_full =
                self.orders[sell_id].fill(buy_order_id, fill_quantity, maker_price);
            let buy_is_full =
                self.orders[buy_id].fill(sell_order_id, fill_quantity, maker_price);

            self.change_balance_match(
                &self.orders[maker_id].clone(),
                &self.orders[taker_id].clone(),
                fill_quantity,
            );
            self.trades
                .push(self.create_order_matched_event(&self.orders[maker_id]));

            if sell_is_full {
                self.remove_order(sell_id);
            }
            if buy_is_full {
                self.remove_order(buy_id);
            }
        }
    }

    pub fn create_order(&mut self, order_data: OrderData) -> Order {
        self.order_id += 1;
        let order = Order::new(self.order_id, order_data);
        self.insert_order(&order);
        self.all_orders.push(order.clone());
        order
    }

    pub fn create_orders(&mut self, orders_data: Vec<OrderData>) -> Vec<Order> {
        let mut orders = Vec::new();
        for order_data in orders_data {
            orders.push(self.create_order(order_data));
        }
        orders
    }
}

#[derive(Clone, Debug)]
pub struct TestOrderBookState {
    pub orders: Vec<OrderData>,
    pub matches: Vec<MatchedEvent>,
    pub cancels: Vec<Order>,
}

pub fn get_default_orders_test(
    base_asset: AssetId,
    quote_asset: AssetId,
    initial_balances: Balances,
) -> (Book, TestOrderBookState) {
    let mut book: Book = Book::new(BookConfig {
        base_asset,
        base_decimals: 9,
        quote_asset,
        quote_decimals: 6,
        taker_fee: 0,
        maker_fee: 0,
    });
    let traders = initial_balances.keys().cloned().collect::<Vec<_>>();

    // Set initial balances
    book.set_balances(initial_balances);

    let mut orders = vec![
        OrderData {
            side: Side::Buy,
            price: 9_500_000,
            quantity: 20_000_000,
            trader_id: Identity::default(),
        },
        OrderData {
            side: Side::Sell,
            price: 20_000_000,
            quantity: 20_000_000,
            trader_id: Identity::default(),
        },
        OrderData {
            side: Side::Sell,
            price: 10_000_000,
            quantity: 20_000_000,
            trader_id: Identity::default(),
        },
        OrderData {
            side: Side::Buy,
            price: 10_000_000,
            quantity: 40_000_000,
            trader_id: Identity::default(),
        },
        OrderData {
            side: Side::Sell,
            price: 9_900_000,
            quantity: 40_000_000,
            trader_id: Identity::default(),
        },
        OrderData {
            side: Side::Buy,
            price: 10_000_000,
            quantity: 20_000_000,
            trader_id: Identity::default(),
        },
    ];

    for (i, order) in orders.iter_mut().enumerate() {
        let trader_id = traders.get(i % traders.len()).unwrap();
        order.trader_id = *trader_id;
    }

    let matches = vec![
        MatchedEvent {
            direction: Side::Sell,
            maker_id: 3,
            taker_id: 4,
            quantity: 20_000_000,
            price: 10_000_000,
            total: 200_000,
        },
        MatchedEvent {
            direction: Side::Buy,
            maker_id: 4,
            taker_id: 5,
            quantity: 20_000_000,
            price: 10_000_000,
            total: 200_000,
        },
        MatchedEvent {
            direction: Side::Sell,
            maker_id: 5,
            taker_id: 6,
            quantity: 20_000_000,
            price: 9_900_000,
            total: 198_000,
        },
    ];
    let cancels = vec![Order::new(
        1,
        OrderData {
            side: Side::Buy,
            price: 9_500_000,
            quantity: 20_000_000,
            trader_id: *traders.first().unwrap(),
        },
    )];

    // Create book with states
    book.create_orders(orders.clone());

    let test_order_book_state = TestOrderBookState {
        orders,
        matches,
        cancels,
    };

    (book, test_order_book_state)
}

pub fn get_default_orders_with_fees_test(
    base_asset: AssetId,
    quote_asset: AssetId,
    initial_balances: Balances,
) -> (Book, TestOrderBookState) {
    let mut book: Book = Book::new(BookConfig {
        base_asset,
        base_decimals: 9,
        quote_asset,
        quote_decimals: 6,
        taker_fee: 2_000,
        maker_fee: 1_000,
    });
    let traders = initial_balances.keys().cloned().collect::<Vec<_>>();

    // Set initial balances
    book.set_balances(initial_balances);

    let mut orders = vec![
        OrderData {
            side: Side::Buy,
            price: 9_500_000,
            quantity: 20_000_000,
            trader_id: Identity::default(),
        },
        OrderData {
            side: Side::Sell,
            price: 20_000_000,
            quantity: 20_000_000,
            trader_id: Identity::default(),
        },
        OrderData {
            side: Side::Sell,
            price: 10_000_000,
            quantity: 20_000_000,
            trader_id: Identity::default(),
        },
        OrderData {
            side: Side::Buy,
            price: 10_000_000,
            quantity: 40_000_000,
            trader_id: Identity::default(),
        },
        OrderData {
            side: Side::Sell,
            price: 9_900_000,
            quantity: 40_000_000,
            trader_id: Identity::default(),
        },
        OrderData {
            side: Side::Buy,
            price: 10_000_000,
            quantity: 20_000_000,
            trader_id: Identity::default(),
        },
    ];

    for (i, order) in orders.iter_mut().enumerate() {
        let trader_id = traders.get(i % traders.len()).unwrap();
        order.trader_id = *trader_id;
    }

    let matches = vec![
        MatchedEvent {
            direction: Side::Sell,
            maker_id: 3,
            taker_id: 4,
            quantity: 20_000_000,
            price: 10_000_000,
            total: 200_000,
        },
        MatchedEvent {
            direction: Side::Buy,
            maker_id: 4,
            taker_id: 5,
            quantity: 20_000_000,
            price: 10_000_000,
            total: 200_000,
        },
        MatchedEvent {
            direction: Side::Sell,
            maker_id: 5,
            taker_id: 6,
            quantity: 20_000_000,
            price: 9_900_000,
            total: 198_000,
        },
    ];
    let cancels = vec![Order::new(
        1,
        OrderData {
            side: Side::Buy,
            price: 9_500_000,
            quantity: 20_000_000,
            trader_id: *traders.first().unwrap(),
        },
    )];

    // Create book with states
    book.create_orders(orders.clone());

    let test_order_book_state = TestOrderBookState {
        orders,
        matches,
        cancels,
    };

    (book, test_order_book_state)
}

#[tokio::test]
async fn test_order_book_data() {
    use fuels::types::{
        Address,
        Identity,
    };
    use std::str::FromStr;
    const USDC: &str =
        "0x8288f40f7c9ab3b5bc36783b692f4ae96975c5a04d8f0da258886fdada0b26af";
    const BTC: &str =
        "0xd0cef4b9cb0706cfa4aa174e75fb006b8ddd2a0ecd6296a27c0a05dcb856f32f";
    let address_1 = Identity::Address(Address::from([5u8; 32]));
    let address_2 = Identity::Address(Address::from([6u8; 32]));

    let balances = Balances::from_iter(vec![
        (address_1, (100_000_000_000_000, 100_000_000_000_000)),
        (address_2, (100_000_000_000_000, 100_000_000_000_000)),
    ]);

    let (mut book, test_order_book_state) = get_default_orders_test(
        AssetId::from_str(BTC).unwrap(),
        AssetId::from_str(USDC).unwrap(),
        balances,
    );
    book.cancel(1);
    let balances = Balances::from_iter(vec![
        (address_1, (99999940000000, 100000000598000)),
        (address_2, (100000040000000, 99999999402000)),
    ]);

    assert_eq!(test_order_book_state.matches, book.trades);
    assert_eq!(test_order_book_state.cancels, book.cancels);
    assert_eq!(balances, book.balances);
}

#[tokio::test]
async fn test_order_book_data_fees() {
    use fuels::types::{
        Address,
        Identity,
    };
    use std::str::FromStr;
    const USDC: &str =
        "0x8288f40f7c9ab3b5bc36783b692f4ae96975c5a04d8f0da258886fdada0b26af";
    const BTC: &str =
        "0xd0cef4b9cb0706cfa4aa174e75fb006b8ddd2a0ecd6296a27c0a05dcb856f32f";
    let address_1 = Identity::Address(Address::from([5u8; 32]));
    let address_2 = Identity::Address(Address::from([6u8; 32]));

    let balances = Balances::from_iter(vec![
        (address_1, (100_000_000_000_000, 100_000_000_000_000)),
        (address_2, (100_000_000_000_000, 100_000_000_000_000)),
    ]);

    let (mut book, test_order_book_state) = get_default_orders_with_fees_test(
        AssetId::from_str(BTC).unwrap(),
        AssetId::from_str(USDC).unwrap(),
        balances,
    );
    book.cancel(1);
    let balances = Balances::from_iter(vec![
        (address_1, (99999940000000, 100000000596804)),
        (address_2, (100000039880000, 99999999402000)),
    ]);

    assert_eq!(test_order_book_state.matches, book.trades);
    assert_eq!(test_order_book_state.cancels, book.cancels);
    assert_eq!(balances, book.balances);
}