kaccy-core 0.2.0

Core business logic for Kaccy Protocol - batching, fee optimization, and transaction management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//! Time-based order types for advanced trading
//!
//! This module provides orders that have time-based activation or execution:
//! - MOC (Market-on-Close): Execute at market price at session close
//! - LOC (Limit-on-Close): Execute at limit price at session close
//! - GAT (Good-After-Time): Order becomes active after specified time
//! - Day orders: Orders that expire at end of trading day
//! - GTT (Good-Till-Time): Orders that expire at specific time

use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;

use super::order_book::{LimitOrder, OrderSide};

/// Time-based order type
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum TimeBasedOrderType {
    /// Market-on-Close: execute at market price at session close
    MOC {
        /// Token to trade.
        token_id: Uuid,
        /// Buy or sell direction.
        side: OrderSide,
        /// Order quantity.
        quantity: Decimal,
        /// Trading session close time (Unix timestamp)
        close_time: i64,
    },
    /// Limit-on-Close: execute at limit price at session close
    LOC {
        /// Token to trade.
        token_id: Uuid,
        /// Buy or sell direction.
        side: OrderSide,
        /// Order quantity.
        quantity: Decimal,
        /// Limit price for execution.
        price: Decimal,
        /// Trading session close time (Unix timestamp)
        close_time: i64,
    },
    /// Good-After-Time: order becomes active after specified time
    GAT {
        /// Underlying limit order to activate.
        order: Box<LimitOrder>,
        /// Activation time (Unix timestamp)
        activation_time: i64,
    },
    /// Good-Till-Time: order expires at specified time
    GTT {
        /// Underlying limit order.
        order: Box<LimitOrder>,
        /// Expiration time (Unix timestamp)
        expiration_time: i64,
    },
    /// Day order: expires at end of trading day
    Day {
        /// Underlying limit order.
        order: Box<LimitOrder>,
        /// End of day time (Unix timestamp)
        eod_time: i64,
    },
    /// Time slice order: execute only during specific time window
    TimeSlice {
        /// Underlying limit order.
        order: Box<LimitOrder>,
        /// Window start time (Unix timestamp)
        start_time: i64,
        /// Window end time (Unix timestamp)
        end_time: i64,
    },
}

/// Status of a time-based order
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum TimeBasedOrderStatus {
    /// Order is scheduled but not yet active
    Scheduled,
    /// Order is active and can be executed
    Active,
    /// Order has been partially filled
    PartiallyFilled,
    /// Order has been fully filled
    Filled,
    /// Order expired before execution
    Expired,
    /// Order was cancelled
    Cancelled,
    /// Order was rejected
    Rejected,
}

/// Time-based order with metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeBasedOrder {
    /// Unique identifier for this order
    pub id: Uuid,
    /// User who submitted the order
    pub user_id: Uuid,
    /// Specific time-based order type and parameters
    pub order_type: TimeBasedOrderType,
    /// Current lifecycle status of the order
    pub status: TimeBasedOrderStatus,
    /// Unix timestamp when the order was created
    pub created_at: i64,
    /// Unix timestamp when the order was last updated
    pub updated_at: i64,
    /// Amount of the order that has been filled so far
    pub filled_quantity: Decimal,
    /// Arbitrary key-value metadata attached to the order
    pub metadata: HashMap<String, String>,
}

impl TimeBasedOrder {
    /// Create a new Market-on-Close order
    pub fn new_moc(
        user_id: Uuid,
        token_id: Uuid,
        side: OrderSide,
        quantity: Decimal,
        close_time: i64,
    ) -> Result<Self, &'static str> {
        if quantity <= Decimal::ZERO {
            return Err("Quantity must be positive");
        }

        let now = chrono::Utc::now().timestamp();
        if close_time <= now {
            return Err("Close time must be in the future");
        }

        Ok(Self {
            id: Uuid::new_v4(),
            user_id,
            order_type: TimeBasedOrderType::MOC {
                token_id,
                side,
                quantity,
                close_time,
            },
            status: TimeBasedOrderStatus::Scheduled,
            created_at: now,
            updated_at: now,
            filled_quantity: Decimal::ZERO,
            metadata: HashMap::new(),
        })
    }

    /// Create a new Limit-on-Close order
    pub fn new_loc(
        user_id: Uuid,
        token_id: Uuid,
        side: OrderSide,
        quantity: Decimal,
        price: Decimal,
        close_time: i64,
    ) -> Result<Self, &'static str> {
        if quantity <= Decimal::ZERO {
            return Err("Quantity must be positive");
        }

        if price <= Decimal::ZERO {
            return Err("Price must be positive");
        }

        let now = chrono::Utc::now().timestamp();
        if close_time <= now {
            return Err("Close time must be in the future");
        }

        Ok(Self {
            id: Uuid::new_v4(),
            user_id,
            order_type: TimeBasedOrderType::LOC {
                token_id,
                side,
                quantity,
                price,
                close_time,
            },
            status: TimeBasedOrderStatus::Scheduled,
            created_at: now,
            updated_at: now,
            filled_quantity: Decimal::ZERO,
            metadata: HashMap::new(),
        })
    }

    /// Create a new Good-After-Time order
    pub fn new_gat(
        user_id: Uuid,
        order: LimitOrder,
        activation_time: i64,
    ) -> Result<Self, &'static str> {
        let now = chrono::Utc::now().timestamp();
        if activation_time <= now {
            return Err("Activation time must be in the future");
        }

        Ok(Self {
            id: Uuid::new_v4(),
            user_id,
            order_type: TimeBasedOrderType::GAT {
                order: Box::new(order),
                activation_time,
            },
            status: TimeBasedOrderStatus::Scheduled,
            created_at: now,
            updated_at: now,
            filled_quantity: Decimal::ZERO,
            metadata: HashMap::new(),
        })
    }

    /// Create a new Good-Till-Time order
    pub fn new_gtt(
        user_id: Uuid,
        order: LimitOrder,
        expiration_time: i64,
    ) -> Result<Self, &'static str> {
        let now = chrono::Utc::now().timestamp();
        if expiration_time <= now {
            return Err("Expiration time must be in the future");
        }

        Ok(Self {
            id: Uuid::new_v4(),
            user_id,
            order_type: TimeBasedOrderType::GTT {
                order: Box::new(order),
                expiration_time,
            },
            status: TimeBasedOrderStatus::Active, // GTT is active immediately
            created_at: now,
            updated_at: now,
            filled_quantity: Decimal::ZERO,
            metadata: HashMap::new(),
        })
    }

    /// Create a new Day order
    pub fn new_day(user_id: Uuid, order: LimitOrder, eod_time: i64) -> Result<Self, &'static str> {
        let now = chrono::Utc::now().timestamp();
        if eod_time <= now {
            return Err("End of day time must be in the future");
        }

        Ok(Self {
            id: Uuid::new_v4(),
            user_id,
            order_type: TimeBasedOrderType::Day {
                order: Box::new(order),
                eod_time,
            },
            status: TimeBasedOrderStatus::Active, // Day orders are active immediately
            created_at: now,
            updated_at: now,
            filled_quantity: Decimal::ZERO,
            metadata: HashMap::new(),
        })
    }

    /// Create a new Time Slice order
    pub fn new_time_slice(
        user_id: Uuid,
        order: LimitOrder,
        start_time: i64,
        end_time: i64,
    ) -> Result<Self, &'static str> {
        let now = chrono::Utc::now().timestamp();

        if start_time >= end_time {
            return Err("Start time must be before end time");
        }

        if end_time <= now {
            return Err("End time must be in the future");
        }

        let status = if start_time > now {
            TimeBasedOrderStatus::Scheduled
        } else {
            TimeBasedOrderStatus::Active
        };

        Ok(Self {
            id: Uuid::new_v4(),
            user_id,
            order_type: TimeBasedOrderType::TimeSlice {
                order: Box::new(order),
                start_time,
                end_time,
            },
            status,
            created_at: now,
            updated_at: now,
            filled_quantity: Decimal::ZERO,
            metadata: HashMap::new(),
        })
    }

    /// Update order status
    pub fn update_status(&mut self, status: TimeBasedOrderStatus) {
        self.status = status;
        self.updated_at = chrono::Utc::now().timestamp();
    }

    /// Check if order should be activated at current time
    pub fn should_activate(&self, current_time: i64) -> bool {
        match &self.order_type {
            TimeBasedOrderType::GAT {
                activation_time, ..
            } => current_time >= *activation_time && self.status == TimeBasedOrderStatus::Scheduled,
            TimeBasedOrderType::TimeSlice { start_time, .. } => {
                current_time >= *start_time && self.status == TimeBasedOrderStatus::Scheduled
            }
            _ => false,
        }
    }

    /// Check if order has expired at current time
    pub fn is_expired(&self, current_time: i64) -> bool {
        match &self.order_type {
            TimeBasedOrderType::MOC { close_time, .. } => current_time > *close_time,
            TimeBasedOrderType::LOC { close_time, .. } => current_time > *close_time,
            TimeBasedOrderType::GTT {
                expiration_time, ..
            } => current_time > *expiration_time,
            TimeBasedOrderType::Day { eod_time, .. } => current_time > *eod_time,
            TimeBasedOrderType::TimeSlice { end_time, .. } => current_time > *end_time,
            _ => false,
        }
    }

    /// Check if order should execute on close
    pub fn should_execute_on_close(&self, current_time: i64) -> bool {
        match &self.order_type {
            TimeBasedOrderType::MOC { close_time, .. } => {
                current_time >= *close_time && self.status != TimeBasedOrderStatus::Filled
            }
            TimeBasedOrderType::LOC { close_time, .. } => {
                current_time >= *close_time && self.status != TimeBasedOrderStatus::Filled
            }
            _ => false,
        }
    }
}

/// Manager for time-based orders
pub struct TimeBasedOrderManager {
    /// Active time-based orders
    orders: Arc<RwLock<HashMap<Uuid, TimeBasedOrder>>>,
    /// Scheduled activations (activation_time -> Vec<order_id>)
    scheduled_activations: Arc<RwLock<HashMap<i64, Vec<Uuid>>>>,
    /// Scheduled expirations (expiration_time -> Vec<order_id>)
    scheduled_expirations: Arc<RwLock<HashMap<i64, Vec<Uuid>>>>,
}

impl TimeBasedOrderManager {
    /// Create a new time-based order manager
    pub fn new() -> Self {
        Self {
            orders: Arc::new(RwLock::new(HashMap::new())),
            scheduled_activations: Arc::new(RwLock::new(HashMap::new())),
            scheduled_expirations: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Submit a time-based order
    pub async fn submit(&self, order: TimeBasedOrder) -> Result<Uuid, &'static str> {
        let order_id = order.id;

        // Schedule activation if needed
        match &order.order_type {
            TimeBasedOrderType::GAT {
                activation_time, ..
            } => {
                self.scheduled_activations
                    .write()
                    .await
                    .entry(*activation_time)
                    .or_insert_with(Vec::new)
                    .push(order_id);
            }
            TimeBasedOrderType::TimeSlice { start_time, .. } => {
                if order.status == TimeBasedOrderStatus::Scheduled {
                    self.scheduled_activations
                        .write()
                        .await
                        .entry(*start_time)
                        .or_insert_with(Vec::new)
                        .push(order_id);
                }
            }
            _ => {}
        }

        // Schedule expiration
        let expiration_time = match &order.order_type {
            TimeBasedOrderType::MOC { close_time, .. } => Some(*close_time),
            TimeBasedOrderType::LOC { close_time, .. } => Some(*close_time),
            TimeBasedOrderType::GTT {
                expiration_time, ..
            } => Some(*expiration_time),
            TimeBasedOrderType::Day { eod_time, .. } => Some(*eod_time),
            TimeBasedOrderType::TimeSlice { end_time, .. } => Some(*end_time),
            _ => None,
        };

        if let Some(exp_time) = expiration_time {
            self.scheduled_expirations
                .write()
                .await
                .entry(exp_time)
                .or_insert_with(Vec::new)
                .push(order_id);
        }

        // Store the order
        self.orders.write().await.insert(order_id, order);

        Ok(order_id)
    }

    /// Process time events (activations and expirations)
    pub async fn process_time_events(&self, current_time: i64) -> TimeEventResult {
        let mut activated = Vec::new();
        let mut expired = Vec::new();
        let mut to_execute_on_close = Vec::new();

        // Check for activations
        let activation_times: Vec<i64> = self
            .scheduled_activations
            .read()
            .await
            .keys()
            .filter(|&&t| t <= current_time)
            .copied()
            .collect();

        for time in activation_times {
            if let Some(order_ids) = self.scheduled_activations.write().await.remove(&time) {
                for order_id in order_ids {
                    if let Some(order) = self.orders.write().await.get_mut(&order_id) {
                        if order.should_activate(current_time) {
                            order.update_status(TimeBasedOrderStatus::Active);
                            activated.push(order_id);
                        }
                    }
                }
            }
        }

        // Check for expirations
        let expiration_times: Vec<i64> = self
            .scheduled_expirations
            .read()
            .await
            .keys()
            .filter(|&&t| t <= current_time)
            .copied()
            .collect();

        for time in expiration_times {
            if let Some(order_ids) = self.scheduled_expirations.write().await.remove(&time) {
                for order_id in order_ids {
                    if let Some(order) = self.orders.write().await.get_mut(&order_id) {
                        if order.should_execute_on_close(current_time) {
                            to_execute_on_close.push(order_id);
                        } else if order.is_expired(current_time)
                            && !matches!(order.status, TimeBasedOrderStatus::Filled)
                        {
                            order.update_status(TimeBasedOrderStatus::Expired);
                            expired.push(order_id);
                        }
                    }
                }
            }
        }

        TimeEventResult {
            activated,
            expired,
            to_execute_on_close,
        }
    }

    /// Cancel a time-based order
    pub async fn cancel(&self, order_id: Uuid) -> Option<TimeBasedOrder> {
        if let Some(mut order) = self.orders.write().await.remove(&order_id) {
            order.update_status(TimeBasedOrderStatus::Cancelled);
            Some(order)
        } else {
            None
        }
    }

    /// Get a time-based order by ID
    pub async fn get(&self, id: Uuid) -> Option<TimeBasedOrder> {
        self.orders.read().await.get(&id).cloned()
    }

    /// Get all time-based orders for a user
    pub async fn get_by_user(&self, user_id: Uuid) -> Vec<TimeBasedOrder> {
        self.orders
            .read()
            .await
            .values()
            .filter(|o| o.user_id == user_id)
            .cloned()
            .collect()
    }

    /// Get active orders for a user
    pub async fn get_active_by_user(&self, user_id: Uuid) -> Vec<TimeBasedOrder> {
        self.orders
            .read()
            .await
            .values()
            .filter(|o| {
                o.user_id == user_id
                    && matches!(
                        o.status,
                        TimeBasedOrderStatus::Active
                            | TimeBasedOrderStatus::Scheduled
                            | TimeBasedOrderStatus::PartiallyFilled
                    )
            })
            .cloned()
            .collect()
    }

    /// Update fill status for an order
    pub async fn update_fill(&self, order_id: Uuid, filled_quantity: Decimal, is_complete: bool) {
        if let Some(order) = self.orders.write().await.get_mut(&order_id) {
            order.filled_quantity = filled_quantity;
            if is_complete {
                order.update_status(TimeBasedOrderStatus::Filled);
            } else {
                order.update_status(TimeBasedOrderStatus::PartiallyFilled);
            }
        }
    }
}

impl Default for TimeBasedOrderManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of processing time events
#[derive(Debug, Clone)]
pub struct TimeEventResult {
    /// Orders that were activated
    pub activated: Vec<Uuid>,
    /// Orders that expired
    pub expired: Vec<Uuid>,
    /// Orders to execute at close
    pub to_execute_on_close: Vec<Uuid>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use rust_decimal_macros::dec;

    fn create_test_order(token_id: Uuid, side: OrderSide, price: Decimal) -> LimitOrder {
        LimitOrder {
            order_id: Uuid::new_v4(),
            user_id: Uuid::new_v4(),
            token_id,
            side,
            price,
            amount: dec!(100),
            filled_amount: Decimal::ZERO,
            timestamp: chrono::Utc::now().timestamp(),
        }
    }

    #[test]
    fn test_moc_order_creation() {
        let user_id = Uuid::new_v4();
        let token_id = Uuid::new_v4();
        let close_time = chrono::Utc::now().timestamp() + 3600; // 1 hour from now

        let moc = TimeBasedOrder::new_moc(user_id, token_id, OrderSide::Buy, dec!(100), close_time);
        assert!(moc.is_ok());
        assert_eq!(moc.unwrap().status, TimeBasedOrderStatus::Scheduled);
    }

    #[test]
    fn test_moc_order_past_close_time() {
        let user_id = Uuid::new_v4();
        let token_id = Uuid::new_v4();
        let close_time = chrono::Utc::now().timestamp() - 3600; // 1 hour ago

        let moc = TimeBasedOrder::new_moc(user_id, token_id, OrderSide::Buy, dec!(100), close_time);
        assert!(moc.is_err());
    }

    #[test]
    fn test_loc_order_creation() {
        let user_id = Uuid::new_v4();
        let token_id = Uuid::new_v4();
        let close_time = chrono::Utc::now().timestamp() + 3600;

        let loc = TimeBasedOrder::new_loc(
            user_id,
            token_id,
            OrderSide::Buy,
            dec!(100),
            dec!(50),
            close_time,
        );
        assert!(loc.is_ok());
    }

    #[test]
    fn test_gat_order_creation() {
        let user_id = Uuid::new_v4();
        let token_id = Uuid::new_v4();
        let activation_time = chrono::Utc::now().timestamp() + 3600;

        let order = create_test_order(token_id, OrderSide::Buy, dec!(100));
        let gat = TimeBasedOrder::new_gat(user_id, order, activation_time);
        assert!(gat.is_ok());
        assert_eq!(gat.unwrap().status, TimeBasedOrderStatus::Scheduled);
    }

    #[test]
    fn test_gtt_order_creation() {
        let user_id = Uuid::new_v4();
        let token_id = Uuid::new_v4();
        let expiration_time = chrono::Utc::now().timestamp() + 3600;

        let order = create_test_order(token_id, OrderSide::Buy, dec!(100));
        let gtt = TimeBasedOrder::new_gtt(user_id, order, expiration_time);
        assert!(gtt.is_ok());
        assert_eq!(gtt.unwrap().status, TimeBasedOrderStatus::Active);
    }

    #[test]
    fn test_time_slice_order() {
        let user_id = Uuid::new_v4();
        let token_id = Uuid::new_v4();
        let now = chrono::Utc::now().timestamp();
        let start_time = now + 1800; // 30 minutes from now
        let end_time = now + 3600; // 1 hour from now

        let order = create_test_order(token_id, OrderSide::Buy, dec!(100));
        let slice = TimeBasedOrder::new_time_slice(user_id, order, start_time, end_time);
        assert!(slice.is_ok());
        assert_eq!(slice.unwrap().status, TimeBasedOrderStatus::Scheduled);
    }

    #[test]
    fn test_should_activate() {
        let user_id = Uuid::new_v4();
        let token_id = Uuid::new_v4();
        let activation_time = chrono::Utc::now().timestamp() + 100;

        let order = create_test_order(token_id, OrderSide::Buy, dec!(100));
        let gat = TimeBasedOrder::new_gat(user_id, order, activation_time).unwrap();

        assert!(!gat.should_activate(activation_time - 50));
        assert!(gat.should_activate(activation_time + 50));
    }

    #[test]
    fn test_is_expired() {
        let user_id = Uuid::new_v4();
        let token_id = Uuid::new_v4();
        let expiration_time = chrono::Utc::now().timestamp() + 100;

        let order = create_test_order(token_id, OrderSide::Buy, dec!(100));
        let gtt = TimeBasedOrder::new_gtt(user_id, order, expiration_time).unwrap();

        assert!(!gtt.is_expired(expiration_time - 50));
        assert!(gtt.is_expired(expiration_time + 50));
    }

    #[tokio::test]
    async fn test_time_based_order_manager() {
        let manager = TimeBasedOrderManager::new();
        let user_id = Uuid::new_v4();
        let token_id = Uuid::new_v4();
        let activation_time = chrono::Utc::now().timestamp() + 100;

        let order = create_test_order(token_id, OrderSide::Buy, dec!(100));
        let gat = TimeBasedOrder::new_gat(user_id, order, activation_time).unwrap();
        let order_id = manager.submit(gat).await.unwrap();

        // Process before activation time
        let result = manager.process_time_events(activation_time - 50).await;
        assert_eq!(result.activated.len(), 0);

        // Process after activation time
        let result = manager.process_time_events(activation_time + 50).await;
        assert_eq!(result.activated.len(), 1);
        assert_eq!(result.activated[0], order_id);
    }

    #[tokio::test]
    async fn test_cancel_order() {
        let manager = TimeBasedOrderManager::new();
        let user_id = Uuid::new_v4();
        let token_id = Uuid::new_v4();
        let close_time = chrono::Utc::now().timestamp() + 3600;

        let moc = TimeBasedOrder::new_moc(user_id, token_id, OrderSide::Buy, dec!(100), close_time)
            .unwrap();
        let order_id = manager.submit(moc).await.unwrap();

        let cancelled = manager.cancel(order_id).await;
        assert!(cancelled.is_some());
        assert_eq!(cancelled.unwrap().status, TimeBasedOrderStatus::Cancelled);
    }
}