qx-core 0.4.2

牵星内核:时间戳口径、事件溯源、重放校验与定点数值
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
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
//! 订单与成交。
//!
//! 状态机区分"提交意图"与"venue 事实":
//! `Submitted` 只是离开本地的意图,`Accepted` 才代表获得 venue 确认。

use crate::clock::Ts;
use crate::identity::InstrumentId;
use crate::numeric::{ExactDecimal, Money, Price, Quantity};
use crate::trading::OrderPolicy;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;

#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub enum Side {
    Buy,
    Sell,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub enum OrderStatus {
    PendingSubmit,
    Submitted,
    Accepted,
    Working,
    PartiallyFilled,
    Filled,
    CancelPending,
    Cancelled,
    Rejected,
    Expired,
    /// 连接中断或超时后的未知状态,必须进入对账,不能自动补单。
    Unknown,
}

impl OrderStatus {
    /// 是否为终态(不可再迁移)。
    pub fn is_terminal(self) -> bool {
        matches!(
            self,
            OrderStatus::Filled
                | OrderStatus::Cancelled
                | OrderStatus::Rejected
                | OrderStatus::Expired
        )
    }

    pub fn can_transition_to(self, next: Self) -> bool {
        use OrderStatus::*;
        if self == next {
            return !self.is_terminal();
        }
        matches!(
            (self, next),
            (PendingSubmit, Submitted | Rejected | Unknown)
                | (Submitted, Accepted | CancelPending | Rejected | Unknown)
                | (Accepted, Working | CancelPending | Rejected | Unknown)
                | (
                    Working,
                    PartiallyFilled | Filled | CancelPending | Rejected | Expired | Unknown
                )
                | (
                    PartiallyFilled,
                    PartiallyFilled | Filled | CancelPending | Expired | Unknown
                )
                | (CancelPending, Cancelled | Filled | Unknown)
                | (
                    Unknown,
                    Accepted | Working | PartiallyFilled | Filled | Cancelled | Rejected | Expired
                )
        )
    }

    /// Return the shortest legal lifecycle path to an authoritative reconciliation snapshot.
    /// Intermediate states are replayed from the snapshot fact; no venue side effect is implied.
    pub fn reconciliation_path(self, target: Self) -> Option<Vec<Self>> {
        const STATUSES: [OrderStatus; 11] = [
            OrderStatus::PendingSubmit,
            OrderStatus::Submitted,
            OrderStatus::Accepted,
            OrderStatus::Working,
            OrderStatus::PartiallyFilled,
            OrderStatus::Filled,
            OrderStatus::CancelPending,
            OrderStatus::Cancelled,
            OrderStatus::Rejected,
            OrderStatus::Expired,
            OrderStatus::Unknown,
        ];
        if self == target {
            return Some(Vec::new());
        }
        let mut queue = VecDeque::from([(self, Vec::new())]);
        let mut visited = vec![self];
        while let Some((status, path)) = queue.pop_front() {
            for next in STATUSES {
                if !status.can_transition_to(next)
                    || (next == OrderStatus::Unknown && self != OrderStatus::Unknown)
                    || visited.contains(&next)
                {
                    continue;
                }
                let mut next_path = path.clone();
                next_path.push(next);
                if next == target {
                    return Some(next_path);
                }
                visited.push(next);
                queue.push_back((next, next_path));
            }
        }
        None
    }

    pub fn transition(&mut self, next: Self) -> Result<(), String> {
        if self.can_transition_to(next) {
            *self = next;
            Ok(())
        } else {
            Err(format!("非法订单状态迁移: {:?} -> {:?}", self, next))
        }
    }
}

#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OrderTrace {
    /// Stable run identity that created the order. Older orders omit it; readers must not infer it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub run_id: Option<String>,
    pub strategy_id: Option<String>,
    pub signal_id: Option<u64>,
    pub intent_id: Option<u64>,
    pub rule_version: Option<String>,
}

/// Precise order amounts for versioned live/crypto paths.
///
/// Legacy `qty`/`limit`/`filled` stay at scale 1e9. When this payload is present those
/// legacy amount fields must be zero/None; consumers must use this scale-bearing payload.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ExactOrderAmounts {
    /// Base amount for ordinary orders. A quote-budget market buy uses zero here and
    /// carries `quote_budget` instead; fills remain base quantities in `filled`.
    pub quantity: ExactDecimal,
    pub limit_price: Option<ExactDecimal>,
    pub filled: ExactDecimal,
    /// Maximum quote-currency spend for a market buy. The order's quote currency is taken
    /// from its frozen TradingInstrumentSpec. This is a distinct sizing unit, never a
    /// price-derived estimate of base quantity.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub quote_budget: Option<ExactDecimal>,
    /// Cumulative quote-currency amount consumed by fills for a quote-budget order.
    #[serde(default)]
    pub quote_filled: ExactDecimal,
    /// Fresh exact ticker price used only for risk/reservation on market orders.
    /// It is never forwarded as the venue order price.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub market_reference_price: Option<ExactDecimal>,
    /// Exchange-side conditional execution intent. Legacy orders omit this field.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub conditional: Option<ConditionalOrderIntent>,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConditionalOrderKind {
    Trigger,
    StopLoss,
    TakeProfit,
    Trailing,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TriggerPriceType {
    Last,
    Mark,
    Index,
}

/// Conditional terms are part of the immutable exact order request. For a trigger order,
/// `limit_price` selects trigger-limit; omission selects trigger-market.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ConditionalOrderIntent {
    pub kind: ConditionalOrderKind,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub trigger_price: Option<ExactDecimal>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub trigger_price_type: Option<TriggerPriceType>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub trailing_percent: Option<ExactDecimal>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub trailing_amount: Option<ExactDecimal>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub trailing_trigger_price: Option<ExactDecimal>,
}

/// Precise exchange fill values, kept separate from the legacy 1e9 amount fields.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct ExactFillAmounts {
    pub quantity: ExactDecimal,
    pub price: ExactDecimal,
    /// Actual quote-currency cost reported by the venue when available. Used by quote-budget
    /// market buys and to book exact spot cash without recomputing rounded products.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub quote_cost: Option<ExactDecimal>,
    /// The venue/simulator has completed this quote-budget order, even when lot-step rounding
    /// leaves a small unspent quote balance. This is an order lifecycle fact, not extra spend.
    #[serde(default, skip_serializing_if = "is_false")]
    pub quote_budget_complete: bool,
}

fn is_false(value: &bool) -> bool {
    !*value
}

#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Order {
    pub client_id: u64,
    pub instrument: InstrumentId,
    pub side: Side,
    pub qty: Quantity,
    /// None 表示市价单。
    pub limit: Option<Price>,
    pub status: OrderStatus,
    pub filled: Quantity,
    pub account_id: String,
    pub trace: Option<OrderTrace>,
    /// 现货/保证金/永续/期货的执行策略;None 保持现货 Cash/1x 兼容语义。
    #[serde(default)]
    pub policy: Option<OrderPolicy>,
    /// Versioned high-precision amount payload. Omitted on all legacy 1e9 orders.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub exact_amounts: Option<ExactOrderAmounts>,
}

impl Order {
    /// Compare immutable submission facts while ignoring the lifecycle state and cumulative
    /// fills, which may have advanced since the original submission was persisted.
    /// A reused client order ID is idempotent only when every economic and routing input
    /// matches the original request.
    pub fn same_submission_identity(&self, other: &Self) -> bool {
        let same_exact_request = match (self.exact_amounts, other.exact_amounts) {
            (Some(left), Some(right)) => {
                left.quantity == right.quantity
                    && left.limit_price == right.limit_price
                    && left.quote_budget == right.quote_budget
                    && left.market_reference_price == right.market_reference_price
                    && left.conditional == right.conditional
            }
            (None, None) => true,
            _ => false,
        };

        self.client_id == other.client_id
            && self.instrument == other.instrument
            && self.side == other.side
            && self.qty == other.qty
            && self.limit == other.limit
            && self.account_id == other.account_id
            && self.trace == other.trace
            && self.policy == other.policy
            && same_exact_request
    }

    /// Compare stable order identity while allowing quantity, price, or
    /// conditional terms to evolve through a confirmed amendment.
    pub fn same_amendment_lineage(&self, other: &Self) -> bool {
        let same_exact_contract = match (self.exact_amounts, other.exact_amounts) {
            (Some(left), Some(right)) => {
                left.quote_budget == right.quote_budget
                    && left.market_reference_price == right.market_reference_price
            }
            (None, None) => true,
            _ => false,
        };

        self.client_id == other.client_id
            && self.instrument == other.instrument
            && self.side == other.side
            && self.account_id == other.account_id
            && self.trace == other.trace
            && self.policy == other.policy
            && same_exact_contract
    }

    pub fn validate(&self) -> Result<(), String> {
        if self.client_id == 0 {
            return Err("client_order_id 必须为正".into());
        }
        if let Some(exact) = self.exact_amounts {
            if self.qty.raw() != 0 || self.filled.raw() != 0 || self.limit.is_some() {
                return Err("精确订单必须清空旧 1e9 数量/价格字段,不能同时携带两套金额".into());
            }
            if let Some(quote_budget) = exact.quote_budget {
                if self.side != Side::Buy
                    || exact.limit_price.is_some()
                    || !exact.quantity.is_zero()
                    || !quote_budget.is_positive()
                {
                    return Err(
                        "计价币预算订单必须是正预算的精确市价买单,且不能携带基础币数量或限价"
                            .into(),
                    );
                }
                if exact.filled.is_negative()
                    || exact.quote_filled.is_negative()
                    || exact
                        .quote_filled
                        .checked_cmp(quote_budget)
                        .is_none_or(|ordering| ordering.is_gt())
                {
                    return Err("计价币预算订单成交量或花费超出有效范围".into());
                }
            } else {
                if !exact.quantity.is_positive() {
                    return Err("订单数量必须为正".into());
                }
                if exact.filled.is_negative()
                    || exact
                        .filled
                        .checked_cmp(exact.quantity)
                        .is_none_or(|ordering| ordering.is_gt())
                {
                    return Err("订单已成交数量超出有效范围".into());
                }
                if !exact.quote_filled.is_zero() {
                    return Err("基础币数量订单不能携带计价币成交花费".into());
                }
            }
            if exact.limit_price.is_some_and(|price| !price.is_positive()) {
                return Err("限价单价格必须为正".into());
            }
            if exact
                .market_reference_price
                .is_some_and(|price| !price.is_positive())
            {
                return Err("市价单风控参考价必须为正".into());
            }
            if exact.limit_price.is_some() && exact.market_reference_price.is_some() {
                return Err("精确限价单不能同时携带市价风险参考价".into());
            }
            if let Some(conditional) = exact.conditional {
                if exact.quote_budget.is_some() {
                    return Err("条件单不能使用计价币预算数量".into());
                }
                let has_trailing = conditional.trailing_percent.is_some()
                    || conditional.trailing_amount.is_some()
                    || conditional.trailing_trigger_price.is_some();
                match conditional.kind {
                    ConditionalOrderKind::Trailing => {
                        if conditional.trigger_price.is_some()
                            || conditional.trigger_price_type.is_some()
                            || (conditional.trailing_percent.is_some()
                                == conditional.trailing_amount.is_some())
                        {
                            return Err(
                                "追踪单必须且只能设置 trailing_percent 或 trailing_amount,不能设置普通触发价/来源".into(),
                            );
                        }
                        if conditional
                            .trailing_percent
                            .is_some_and(|value| !value.is_positive())
                            || conditional
                                .trailing_amount
                                .is_some_and(|value| !value.is_positive())
                        {
                            return Err("追踪幅度必须为正".into());
                        }
                    }
                    _ => {
                        if conditional
                            .trigger_price
                            .is_none_or(|value| !value.is_positive())
                            || has_trailing
                        {
                            return Err(
                                "触发/止损/止盈单必须提供正触发价,且不能携带追踪参数".into()
                            );
                        }
                    }
                }
                if conditional
                    .trailing_trigger_price
                    .is_some_and(|value| !value.is_positive())
                {
                    return Err("追踪单启动价必须为正".into());
                }
            }
        } else {
            if self.qty.raw() <= 0 {
                return Err("订单数量必须为正".into());
            }
            if self.filled.raw() < 0 || self.filled.raw() > self.qty.raw() {
                return Err("订单已成交数量超出有效范围".into());
            }
            if self.limit.is_some_and(|price| price.raw() <= 0) {
                return Err("限价单价格必须为正".into());
            }
        }
        if self.account_id.trim().is_empty() {
            return Err("订单账户不能为空".into());
        }
        if self
            .trace
            .as_ref()
            .and_then(|trace| trace.run_id.as_deref())
            .is_some_and(|run_id| run_id.trim().is_empty())
        {
            return Err("订单 run_id 不能为空字符串;未知运行身份应省略该字段".into());
        }
        if self.policy.is_some_and(|policy| policy.leverage == 0) {
            return Err("订单杠杆必须大于 0".into());
        }
        Ok(())
    }

    /// Guard a legacy 1e9 consumer against silently reading an exact amount as zero.
    pub fn require_legacy_amounts(&self) -> Result<(), String> {
        if self.exact_amounts.is_some() {
            return Err("该路径尚未接入精确订单金额,拒绝读取 1e9 兼容字段".into());
        }
        Ok(())
    }

    pub fn remaining(&self) -> Quantity {
        Quantity::from_raw(self.qty.raw().saturating_sub(self.filled.raw()).max(0))
    }

    /// Exact quantity including a high-precision payload when present.
    pub fn quantity_decimal(&self) -> ExactDecimal {
        self.exact_amounts.map_or_else(
            || ExactDecimal::from_legacy_raw(self.qty.raw()),
            |amounts| amounts.quantity,
        )
    }

    pub fn quote_budget_decimal(&self) -> Option<ExactDecimal> {
        self.exact_amounts.and_then(|amounts| amounts.quote_budget)
    }

    pub fn quote_filled_decimal(&self) -> ExactDecimal {
        self.exact_amounts
            .map_or(ExactDecimal::ZERO, |amounts| amounts.quote_filled)
    }

    /// Remaining amount in the order's declared sizing unit. For quote-budget orders this
    /// is quote currency; consumers doing quantity/exposure math must use `remaining_decimal`.
    pub fn remaining_budget_decimal(&self) -> Option<ExactDecimal> {
        let amounts = self.exact_amounts?;
        amounts.quote_budget?.checked_sub(amounts.quote_filled)
    }

    pub fn is_quote_budget_order(&self) -> bool {
        self.quote_budget_decimal().is_some()
    }

    /// Whether any amount remains in the order's own sizing unit.
    pub fn has_remaining_amount(&self) -> bool {
        if self.is_quote_budget_order() {
            return self
                .remaining_budget_decimal()
                .is_some_and(ExactDecimal::is_positive);
        }
        self.remaining_decimal()
            .is_some_and(ExactDecimal::is_positive)
    }

    pub fn filled_decimal(&self) -> ExactDecimal {
        self.exact_amounts.map_or_else(
            || ExactDecimal::from_legacy_raw(self.filled.raw()),
            |amounts| amounts.filled,
        )
    }

    pub fn limit_price_decimal(&self) -> Option<ExactDecimal> {
        self.exact_amounts.map_or_else(
            || {
                self.limit
                    .map(|price| ExactDecimal::from_legacy_raw(price.raw()))
            },
            |amounts| amounts.limit_price,
        )
    }

    /// Price used to value an exact pending order. A market order carries the fresh
    /// reference captured at admission so later reservations do not fall back to 1e-9.
    pub fn market_reference_price_decimal(&self) -> Option<ExactDecimal> {
        self.exact_amounts
            .and_then(|amounts| amounts.market_reference_price)
    }

    pub fn remaining_decimal(&self) -> Option<ExactDecimal> {
        self.quantity_decimal().checked_sub(self.filled_decimal())
    }

    pub fn trace_fill(
        &self,
        fill: &mut Fill,
        venue_id: Option<&str>,
        venue_order_id: Option<&str>,
    ) {
        if fill.account_id.is_empty() {
            fill.account_id = self.account_id.clone();
        }
        fill.venue_id = venue_id.map(str::to_string);
        fill.venue_order_id = venue_order_id.map(str::to_string);
        if let Some(trace) = &self.trace {
            fill.strategy_id = trace.strategy_id.clone();
            fill.signal_id = trace.signal_id;
            fill.intent_id = trace.intent_id;
            fill.rule_version = trace.rule_version.clone();
        }
    }
}

/// 成交:不可变事实。
#[derive(Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
pub struct FillFee {
    pub amount: Money,
    /// Venue 币种;`None` 仅用于兼容旧 Fill,按账簿币种解释。
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub currency: Option<String>,
    /// Venue trade IDs which contributed this fee row. Persisted so a restored adapter can
    /// deduplicate trade-level fee queries after process restart.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub source_trade_ids: Vec<String>,
}

#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct ExactFillFee {
    pub amount: ExactDecimal,
    pub currency: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub source_trade_ids: Vec<String>,
}

#[derive(Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
pub struct Fill {
    pub order_id: u64,
    pub qty: Quantity,
    pub price: Price,
    pub fee: Money,
    pub ts: Ts,
    /// 成交归属账户。真实 Venue 回报必须填充;旧模拟路径允许为空并由 OMS 补齐。
    pub account_id: String,
    pub strategy_id: Option<String>,
    pub signal_id: Option<u64>,
    pub intent_id: Option<u64>,
    pub venue_id: Option<String>,
    pub venue_order_id: Option<String>,
    pub rule_version: Option<String>,
    pub fee_currency: Option<String>,
    /// 按币种拆分的成交手续费。为空时读取旧的 `fee` / `fee_currency` 两格。
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fees: Vec<FillFee>,
    /// Scale-aware per-currency fee rows for fills that exceed the legacy 1e9 amount grid.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub exact_fees: Vec<ExactFillFee>,
    /// Versioned high-precision quantity/price payload; absent on legacy 1e9 fills.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub exact_amounts: Option<ExactFillAmounts>,
}

impl Fill {
    /// Stable suffix for trade identities and scale-aware fill fields used by replay dedup.
    pub fn exact_identity_suffix(&self) -> String {
        let mut components = Vec::new();
        if let Some(amounts) = self.exact_amounts {
            components.push(format!(
                "amount={}:{};price={}:{}",
                amounts.quantity.coefficient(),
                amounts.quantity.scale(),
                amounts.price.coefficient(),
                amounts.price.scale(),
            ));
        }
        for fee in &self.exact_fees {
            let mut trade_ids = fee.source_trade_ids.clone();
            trade_ids.sort();
            components.push(format!(
                "fee={}:{};currency={};trades={}",
                fee.amount.coefficient(),
                fee.amount.scale(),
                fee.currency.as_deref().unwrap_or_default(),
                trade_ids
                    .iter()
                    .map(|trade_id| format!("{}:{trade_id}", trade_id.len()))
                    .collect::<Vec<_>>()
                    .join(","),
            ));
        }
        if self.exact_fees.is_empty() {
            for fee in &self.fees {
                if fee.source_trade_ids.is_empty() {
                    continue;
                }
                let mut trade_ids = fee.source_trade_ids.clone();
                trade_ids.sort();
                components.push(format!(
                    "trades={}",
                    trade_ids
                        .iter()
                        .map(|trade_id| format!("{}:{trade_id}", trade_id.len()))
                        .collect::<Vec<_>>()
                        .join(","),
                ));
            }
        }
        if components.is_empty() {
            return String::new();
        }
        components.sort();
        format!("|exact:{}", components.join("|"))
    }

    pub fn validate_exact_amounts(&self) -> Result<(), String> {
        if let Some(exact) = self.exact_amounts {
            if self.qty.raw() != 0 || self.price.raw() != 0 {
                return Err("精确成交必须清空旧 1e9 数量/价格字段,不能同时携带两套金额".into());
            }
            if !exact.quantity.is_positive() || !exact.price.is_positive() {
                return Err("精确成交数量和价格必须为正".into());
            }
            if exact.quote_cost.is_some_and(|cost| !cost.is_positive()) {
                return Err("精确成交计价币成本必须为正".into());
            }
            if exact.quote_budget_complete && exact.quote_cost.is_none() {
                return Err("quote_budget 完成标记必须包含精确计价币成本".into());
            }
        }
        Ok(())
    }

    /// Guard a legacy 1e9 consumer against silently reading an exact fill as zero.
    pub fn require_legacy_amounts(&self) -> Result<(), String> {
        if self.exact_amounts.is_some() {
            return Err("该路径尚未接入精确成交金额,拒绝读取 1e9 兼容字段".into());
        }
        Ok(())
    }

    pub fn quantity_decimal(&self) -> ExactDecimal {
        self.exact_amounts.map_or_else(
            || ExactDecimal::from_legacy_raw(self.qty.raw()),
            |amounts| amounts.quantity,
        )
    }

    pub fn price_decimal(&self) -> ExactDecimal {
        self.exact_amounts.map_or_else(
            || ExactDecimal::from_legacy_raw(self.price.raw()),
            |amounts| amounts.price,
        )
    }

    /// 读取新旧格式统一的费用事实,并拒绝同时写入互相矛盾的字段。
    pub fn fee_charges(&self) -> Result<Vec<FillFee>, String> {
        if !self.exact_fees.is_empty() {
            return Err("精确手续费必须由精确账簿路径读取,不能投影到 1e9 Money".into());
        }
        if self.fees.is_empty() {
            return Ok(if self.fee.raw() == 0 {
                Vec::new()
            } else {
                vec![FillFee {
                    amount: self.fee,
                    currency: self.fee_currency.clone(),
                    source_trade_ids: Vec::new(),
                }]
            });
        }
        if self.fee.raw() != 0
            && (self.fees.len() != 1
                || self.fees[0].amount != self.fee
                || self.fees[0].currency.as_deref() != self.fee_currency.as_deref())
        {
            return Err("Fill 的兼容手续费字段与 fees 明细冲突".into());
        }
        for charge in &self.fees {
            if charge.amount.raw() != 0
                && charge
                    .currency
                    .as_deref()
                    .is_none_or(|currency| currency.trim().is_empty())
            {
                return Err("非零 Fill 手续费必须声明币种".into());
            }
        }
        Ok(self.fees.clone())
    }
}

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

    #[test]
    fn order_trace_run_identity_is_optional_backward_compatible_and_nonempty() {
        let legacy: OrderTrace = serde_json::from_str(
            r#"{"strategy_id":"legacy","signal_id":null,"intent_id":null,"rule_version":null}"#,
        )
        .unwrap();
        assert_eq!(legacy.run_id, None);

        let mut order = Order {
            client_id: 1,
            instrument: InstrumentId::parse("BTC/USDT.OKX").unwrap(),
            side: Side::Buy,
            qty: Quantity::from_i64(1),
            limit: None,
            status: OrderStatus::PendingSubmit,
            filled: Quantity::ZERO,
            account_id: "main".into(),
            trace: Some(OrderTrace {
                run_id: Some("  ".into()),
                strategy_id: Some("strategy".into()),
                signal_id: None,
                intent_id: None,
                rule_version: None,
            }),
            policy: None,
            exact_amounts: None,
        };
        assert!(order.validate().unwrap_err().contains("run_id"));
        order.trace.as_mut().unwrap().run_id = Some("run-42".into());
        order.validate().unwrap();
    }

    #[test]
    fn conditional_order_terms_are_validated_and_part_of_idempotency_identity() {
        let mut order = Order {
            client_id: 41,
            instrument: InstrumentId::parse("BTC/USDT.OKX").unwrap(),
            side: Side::Sell,
            qty: Quantity::ZERO,
            limit: None,
            status: OrderStatus::PendingSubmit,
            filled: Quantity::ZERO,
            account_id: "main".into(),
            trace: None,
            policy: None,
            exact_amounts: Some(ExactOrderAmounts {
                quantity: ExactDecimal::parse("0.01").unwrap(),
                limit_price: None,
                filled: ExactDecimal::ZERO,
                quote_budget: None,
                quote_filled: ExactDecimal::ZERO,
                market_reference_price: Some(ExactDecimal::parse("65000").unwrap()),
                conditional: Some(ConditionalOrderIntent {
                    kind: ConditionalOrderKind::StopLoss,
                    trigger_price: Some(ExactDecimal::parse("62000").unwrap()),
                    trigger_price_type: Some(TriggerPriceType::Mark),
                    trailing_percent: None,
                    trailing_amount: None,
                    trailing_trigger_price: None,
                }),
            }),
        };
        order.validate().unwrap();
        let mut retry = order.clone();
        retry
            .exact_amounts
            .as_mut()
            .unwrap()
            .conditional
            .as_mut()
            .unwrap()
            .trigger_price = Some(ExactDecimal::parse("61000").unwrap());
        assert!(!order.same_submission_identity(&retry));

        let intent = order
            .exact_amounts
            .as_mut()
            .unwrap()
            .conditional
            .as_mut()
            .unwrap();
        intent.trigger_price = None;
        assert!(order.validate().unwrap_err().contains("正触发价"));
    }

    #[test]
    fn remaining_decreases() {
        let o = Order {
            client_id: 1,
            instrument: InstrumentId::parse("X.Y").unwrap(),
            side: Side::Buy,
            qty: Quantity::from_i64(10),
            limit: None,
            status: OrderStatus::PartiallyFilled,
            filled: Quantity::from_i64(4),
            account_id: "a".into(),
            trace: None,
            policy: None,

            exact_amounts: None,
        };
        assert_eq!(o.remaining().raw(), Quantity::from_i64(6).raw());
        assert!(!o.status.is_terminal());
    }

    #[test]
    fn unknown_requires_reconcile_before_recovery() {
        assert!(OrderStatus::PendingSubmit.can_transition_to(OrderStatus::Unknown));
        assert!(OrderStatus::Submitted.can_transition_to(OrderStatus::Unknown));
        assert!(OrderStatus::Unknown.can_transition_to(OrderStatus::Accepted));
        assert!(!OrderStatus::CancelPending.can_transition_to(OrderStatus::PartiallyFilled));
        assert!(OrderStatus::CancelPending.can_transition_to(OrderStatus::Filled));
        assert!(!OrderStatus::Filled.can_transition_to(OrderStatus::Working));
    }

    #[test]
    fn invalid_order_shape_is_rejected_before_submission() {
        let mut order = Order {
            client_id: 1,
            instrument: InstrumentId::parse("X.Y").unwrap(),
            side: Side::Buy,
            qty: Quantity::from_i64(1),
            limit: Some(Price::from_i64(10)),
            status: OrderStatus::Submitted,
            filled: Quantity::ZERO,
            account_id: "a".into(),
            trace: None,
            policy: None,

            exact_amounts: None,
        };
        assert!(order.validate().is_ok());
        order.qty = Quantity::ZERO;
        assert!(order.validate().is_err());
    }

    #[test]
    fn exact_order_amounts_keep_sub_nano_values_and_round_trip() {
        let exact = ExactOrderAmounts {
            quantity: ExactDecimal::parse("0.000000000123456789").unwrap(),
            limit_price: Some(ExactDecimal::parse("0.00000001").unwrap()),
            filled: ExactDecimal::parse("0.000000000023456789").unwrap(),
            market_reference_price: None,
            quote_budget: None,
            quote_filled: Default::default(),
            conditional: None,
        };
        let order = Order {
            client_id: 2,
            instrument: InstrumentId::parse("PEPE/USDT.OKX").unwrap(),
            side: Side::Buy,
            qty: Quantity::ZERO,
            limit: None,
            status: OrderStatus::PartiallyFilled,
            filled: Quantity::ZERO,
            account_id: "main".into(),
            trace: None,
            policy: None,
            exact_amounts: Some(exact),
        };

        order.validate().unwrap();
        assert_eq!(
            order.remaining_decimal().unwrap(),
            ExactDecimal::parse("0.0000000001").unwrap()
        );
        assert_eq!(order.quantity_decimal(), exact.quantity);
        assert_eq!(order.limit_price_decimal(), exact.limit_price);

        let encoded = serde_json::to_string(&order).unwrap();
        let decoded: Order = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded, order);
        assert!(encoded.contains("\"coefficient\":\"123456789\""));
    }

    #[test]
    fn exact_amounts_reject_ambiguous_legacy_fields_and_invalid_fills() {
        let mut order = Order {
            client_id: 3,
            instrument: InstrumentId::parse("SHIB/USDT.BINANCE").unwrap(),
            side: Side::Buy,
            qty: Quantity::from_i64(1),
            limit: None,
            status: OrderStatus::Submitted,
            filled: Quantity::ZERO,
            account_id: "main".into(),
            trace: None,
            policy: None,
            exact_amounts: Some(ExactOrderAmounts {
                quantity: ExactDecimal::parse("1000.0000000001").unwrap(),
                limit_price: None,
                filled: ExactDecimal::ZERO,
                market_reference_price: None,
                quote_budget: None,
                quote_filled: Default::default(),
                conditional: None,
            }),
        };
        assert!(order.validate().is_err());
        order.qty = Quantity::ZERO;
        order.validate().unwrap();

        let mut fill = Fill {
            order_id: 3,
            exact_amounts: Some(ExactFillAmounts {
                quantity: ExactDecimal::ZERO,
                price: ExactDecimal::parse("0.00000001").unwrap(),
                quote_cost: None,
                quote_budget_complete: false,
            }),
            ..Fill::default()
        };
        assert!(fill.validate_exact_amounts().is_err());
        fill.exact_amounts.as_mut().unwrap().quantity =
            ExactDecimal::parse("0.0000000001").unwrap();
        fill.validate_exact_amounts().unwrap();
    }

    #[test]
    fn order_wire_rejects_unknown_fields_and_keeps_legacy_optional_fields_readable() {
        let policy = OrderPolicy {
            reduce_only: false,
            position_side: crate::PositionSide::Net,
            margin_mode: crate::MarginMode::Cross,
            position_mode: crate::PositionMode::OneWay,
            leverage: 3,
            post_only: false,
        };
        let order = Order {
            client_id: 9,
            instrument: InstrumentId::parse("BTCUSDT.BINANCE").unwrap(),
            side: Side::Buy,
            qty: Quantity::from_i64(1),
            limit: None,
            status: OrderStatus::PendingSubmit,
            filled: Quantity::ZERO,
            account_id: "main".into(),
            trace: None,
            policy: Some(policy),
            exact_amounts: None,
        };

        let mut misspelled_policy = serde_json::to_value(&order).unwrap();
        let object = misspelled_policy.as_object_mut().unwrap();
        let policy = object.remove("policy").unwrap();
        object.insert("polciy".into(), policy);
        assert!(serde_json::from_value::<Order>(misspelled_policy).is_err());

        let mut misspelled_policy_field = serde_json::to_value(&order).unwrap();
        misspelled_policy_field["policy"]["post_onli"] = serde_json::Value::Bool(true);
        assert!(serde_json::from_value::<Order>(misspelled_policy_field).is_err());

        let mut legacy = serde_json::to_value(Order {
            policy: None,
            ..order
        })
        .unwrap();
        let object = legacy.as_object_mut().unwrap();
        object.remove("policy");
        object.remove("exact_amounts");
        let decoded: Order = serde_json::from_value(legacy).unwrap();
        assert!(decoded.policy.is_none());
        assert!(decoded.exact_amounts.is_none());
    }
}