cow-rs 0.1.1

Rust SDK for the CoW Protocol: quoting, signing, posting and tracking orders, plus composable orders, on-chain reads and subgraph queries.
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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
//! Response types for the `CoW` Protocol subgraph.
//!
//! All types in this module derive `Serialize` and `Deserialize` with
//! `camelCase` field names to match the `GraphQL` response format. String
//! fields are used for large integers (volume, amounts) to avoid overflow.
//!
//! # Key types
//!
//! | Type | Represents |
//! |---|---|
//! | [`Totals`] | Protocol-wide aggregate statistics |
//! | [`DailyVolume`] / [`HourlyVolume`] | Volume snapshots |
//! | [`DailyTotal`] / [`HourlyTotal`] | Full per-period statistics |
//! | [`Token`] | An ERC-20 token indexed by the subgraph |
//! | [`Trade`] | A single trade within a settlement |
//! | [`Order`] | An order indexed by the subgraph |
//! | [`Settlement`] | An on-chain batch settlement |
//! | [`Pair`] | A trading pair (token0/token1) |
//! | [`Bundle`] | Current ETH/USD price |
//! | [`User`] | A trader address with aggregate stats |

use std::fmt;

use serde::{Deserialize, Serialize};

// ── Aggregate stats ───────────────────────────────────────────────────────────

/// Protocol-wide aggregate statistics.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Totals {
    /// Number of distinct ERC-20 tokens seen.
    pub tokens: String,
    /// Total number of orders.
    pub orders: String,
    /// Total number of unique traders.
    pub traders: String,
    /// Total number of on-chain batch settlements.
    pub settlements: String,
    /// Cumulative volume in USD.
    pub volume_usd: String,
    /// Cumulative volume in ETH.
    pub volume_eth: String,
    /// Cumulative fees collected in USD.
    pub fees_usd: String,
    /// Cumulative fees collected in ETH.
    pub fees_eth: String,
}

impl fmt::Display for Totals {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "totals(orders={}, traders={})", self.orders, self.traders)
    }
}

/// Per-day volume snapshot.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DailyVolume {
    /// Unix timestamp (start of day, UTC).
    pub timestamp: String,
    /// USD volume for this day.
    pub volume_usd: String,
}

impl fmt::Display for DailyVolume {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "daily-vol(ts={}, ${})", self.timestamp, self.volume_usd)
    }
}

/// Per-hour volume snapshot.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HourlyVolume {
    /// Unix timestamp (start of hour, UTC).
    pub timestamp: String,
    /// USD volume for this hour.
    pub volume_usd: String,
}

impl fmt::Display for HourlyVolume {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "hourly-vol(ts={}, ${})", self.timestamp, self.volume_usd)
    }
}

// ── DailyTotal / HourlyTotal (full schema entities) ───────────────────────────

/// Full per-day protocol statistics entity from the subgraph.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DailyTotal {
    /// Start-of-day Unix timestamp.
    pub timestamp: String,
    /// Total orders settled this day.
    pub orders: String,
    /// Unique traders active this day.
    pub traders: String,
    /// Unique tokens traded this day.
    pub tokens: String,
    /// Number of batch settlements this day.
    pub settlements: String,
    /// Total volume in ETH.
    pub volume_eth: String,
    /// Total volume in USD.
    pub volume_usd: String,
    /// Fees collected in ETH.
    pub fees_eth: String,
    /// Fees collected in USD.
    pub fees_usd: String,
}

impl fmt::Display for DailyTotal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "daily-total(ts={}, orders={})", self.timestamp, self.orders)
    }
}

/// Full per-hour protocol statistics entity from the subgraph.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HourlyTotal {
    /// Start-of-hour Unix timestamp.
    pub timestamp: String,
    /// Total orders settled this hour.
    pub orders: String,
    /// Unique traders active this hour.
    pub traders: String,
    /// Unique tokens traded this hour.
    pub tokens: String,
    /// Number of batch settlements this hour.
    pub settlements: String,
    /// Total volume in ETH.
    pub volume_eth: String,
    /// Total volume in USD.
    pub volume_usd: String,
    /// Fees collected in ETH.
    pub fees_eth: String,
    /// Fees collected in USD.
    pub fees_usd: String,
}

impl fmt::Display for HourlyTotal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "hourly-total(ts={}, orders={})", self.timestamp, self.orders)
    }
}

// ── Token ─────────────────────────────────────────────────────────────────────

/// An ERC-20 token indexed by the subgraph.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Token {
    /// Subgraph entity ID (token address in lowercase hex).
    pub id: String,
    /// Checksummed ERC-20 contract address.
    pub address: String,
    /// Unix timestamp of the first trade involving this token.
    pub first_trade_timestamp: String,
    /// Token name from ERC-20 metadata.
    pub name: String,
    /// Token symbol from ERC-20 metadata.
    pub symbol: String,
    /// Decimal places (ERC-20 `decimals()`).
    pub decimals: String,
    /// Cumulative volume traded (in token units).
    pub total_volume: String,
    /// Current price in ETH.
    pub price_eth: String,
    /// Current price in USD.
    pub price_usd: String,
    /// Total number of trades involving this token.
    pub number_of_trades: String,
}

impl Token {
    /// Returns the token symbol as a string slice.
    ///
    /// # Returns
    ///
    /// A `&str` referencing the token's symbol field.
    #[must_use]
    pub fn symbol_ref(&self) -> &str {
        &self.symbol
    }

    /// Returns the token address as a string slice.
    ///
    /// # Returns
    ///
    /// A `&str` referencing the token's address field.
    #[must_use]
    pub fn address_ref(&self) -> &str {
        &self.address
    }
}

impl fmt::Display for Token {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} ({})", self.symbol, self.address)
    }
}

/// Per-day volume and price statistics for a specific token.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TokenDailyTotal {
    /// Entity ID: `{tokenAddress}-{dayTimestamp}`.
    pub id: String,
    /// Token this snapshot belongs to.
    pub token: Token,
    /// Start-of-day Unix timestamp.
    pub timestamp: String,
    /// Volume traded in token units.
    pub total_volume: String,
    /// Volume in USD.
    pub total_volume_usd: String,
    /// Total number of trades.
    pub total_trades: String,
    /// Opening price in USD.
    pub open_price: String,
    /// Closing price in USD.
    pub close_price: String,
    /// Highest price in USD.
    pub higher_price: String,
    /// Lowest price in USD.
    pub lower_price: String,
    /// Volume-weighted average price in USD.
    pub average_price: String,
}

impl fmt::Display for TokenDailyTotal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "token-daily({}, ts={})", self.token, self.timestamp)
    }
}

/// Per-hour volume and price statistics for a specific token.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TokenHourlyTotal {
    /// Entity ID: `{tokenAddress}-{hourTimestamp}`.
    pub id: String,
    /// Token this snapshot belongs to.
    pub token: Token,
    /// Start-of-hour Unix timestamp.
    pub timestamp: String,
    /// Volume traded in token units.
    pub total_volume: String,
    /// Volume in USD.
    pub total_volume_usd: String,
    /// Total number of trades.
    pub total_trades: String,
    /// Opening price in USD.
    pub open_price: String,
    /// Closing price in USD.
    pub close_price: String,
    /// Highest price in USD.
    pub higher_price: String,
    /// Lowest price in USD.
    pub lower_price: String,
    /// Volume-weighted average price in USD.
    pub average_price: String,
}

impl fmt::Display for TokenHourlyTotal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "token-hourly({}, ts={})", self.token, self.timestamp)
    }
}

/// A price-changing event for a token (used to reconstruct OHLC data).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TokenTradingEvent {
    /// Entity ID.
    pub id: String,
    /// Token this event belongs to.
    pub token: Token,
    /// USD price at this event.
    pub price_usd: String,
    /// Event timestamp.
    pub timestamp: String,
}

impl fmt::Display for TokenTradingEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "trade-event({}, ts={}, price=${})", self.token, self.timestamp, self.price_usd)
    }
}

// ── User ──────────────────────────────────────────────────────────────────────

/// A trader address indexed by the subgraph.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct User {
    /// Subgraph entity ID (address in lowercase hex).
    pub id: String,
    /// Trader address.
    pub address: String,
    /// Unix timestamp of the first trade.
    pub first_trade_timestamp: String,
    /// Total number of trades executed.
    pub number_of_trades: String,
    /// Cumulative volume of tokens sold, measured in ETH.
    pub solved_amount_eth: String,
    /// Cumulative volume of tokens sold, measured in USD.
    pub solved_amount_usd: String,
}

impl fmt::Display for User {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.address)
    }
}

// ── Settlement ────────────────────────────────────────────────────────────────

/// An on-chain batch settlement transaction.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Settlement {
    /// Entity ID (transaction hash).
    pub id: String,
    /// Transaction hash.
    pub tx_hash: String,
    /// Timestamp of the first trade in this settlement.
    pub first_trade_timestamp: String,
    /// Solver address that submitted the settlement.
    pub solver: String,
    /// Gas cost of the settlement transaction.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tx_cost: Option<String>,
    /// Transaction fee paid in ETH.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tx_fee_in_eth: Option<String>,
}

impl Settlement {
    /// Returns `true` if a gas-cost estimate is available for this settlement.
    ///
    /// # Returns
    ///
    /// `true` if `tx_cost` is `Some`.
    #[must_use]
    pub const fn has_gas_cost(&self) -> bool {
        self.tx_cost.is_some()
    }

    /// Returns `true` if a transaction fee (in ETH) is available for this settlement.
    ///
    /// # Returns
    ///
    /// `true` if `tx_fee_in_eth` is `Some`.
    #[must_use]
    pub const fn has_tx_fee(&self) -> bool {
        self.tx_fee_in_eth.is_some()
    }
}

impl fmt::Display for Settlement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "settlement({})", self.tx_hash)
    }
}

// ── Trade ─────────────────────────────────────────────────────────────────────

/// A single trade executed within a batch settlement.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Trade {
    /// Entity ID: `{txHash}-{tradeIndex}`.
    pub id: String,
    /// Trade execution timestamp.
    pub timestamp: String,
    /// Gas price of the settlement transaction.
    pub gas_price: String,
    /// Protocol fee collected for this trade (in sell token).
    pub fee_amount: String,
    /// Settlement transaction hash.
    pub tx_hash: String,
    /// ID of the [`Settlement`] containing this trade.
    pub settlement: String,
    /// Amount of buy token received.
    pub buy_amount: String,
    /// Amount of sell token used (after fee).
    pub sell_amount: String,
    /// Amount of sell token before the fee was deducted.
    pub sell_amount_before_fees: String,
    /// Buy token.
    pub buy_token: Token,
    /// Sell token.
    pub sell_token: Token,
    /// Trader that placed the order.
    pub owner: User,
    /// ID of the [`Order`] this trade fills.
    pub order: String,
}

impl Trade {
    /// Returns `true` if a settlement transaction hash is available for this trade.
    ///
    /// # Returns
    ///
    /// `true` if the `tx_hash` field is non-empty.
    #[must_use]
    pub const fn has_tx_hash(&self) -> bool {
        !self.tx_hash.is_empty()
    }
}

impl fmt::Display for Trade {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "trade({} {}{})", self.tx_hash, self.sell_token, self.buy_token)
    }
}

// ── Order ─────────────────────────────────────────────────────────────────────

/// An order indexed by the subgraph.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Order {
    /// Entity ID (order UID hex).
    pub id: String,
    /// Order owner / trader.
    pub owner: User,
    /// Token to sell.
    pub sell_token: Token,
    /// Token to buy.
    pub buy_token: Token,
    /// Optional receiver address (if different from owner).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub receiver: Option<String>,
    /// Total sell amount.
    pub sell_amount: String,
    /// Total buy amount (minimum for sell orders).
    pub buy_amount: String,
    /// Order validity deadline (Unix timestamp).
    pub valid_to: String,
    /// App-data hash (hex).
    pub app_data: String,
    /// Protocol fee amount.
    pub fee_amount: String,
    /// Order kind: `"sell"` or `"buy"`.
    pub kind: String,
    /// Whether the order can be partially filled.
    pub partially_fillable: bool,
    /// Order status: `"open"`, `"filled"`, `"cancelled"`, or `"expired"`.
    pub status: String,
    /// Cumulative sell amount executed so far.
    pub executed_sell_amount: String,
    /// Executed sell amount before fees.
    pub executed_sell_amount_before_fees: String,
    /// Cumulative buy amount executed so far.
    pub executed_buy_amount: String,
    /// Cumulative fee amount executed so far.
    pub executed_fee_amount: String,
    /// Timestamp of order cancellation (if any).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub invalidate_timestamp: Option<String>,
    /// Order creation timestamp.
    pub timestamp: String,
    /// Transaction hash of the first fill.
    pub tx_hash: String,
    /// Whether the signer is a smart contract (`EIP-1271`).
    pub is_signer_safe: bool,
    /// Signing scheme used (e.g. `"eip712"`, `"ethsign"`, `"eip1271"`).
    pub signing_scheme: String,
    /// The full order UID (bytes).
    pub uid: String,
    /// Surplus generated by this order.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub surplus: Option<String>,
}

impl Order {
    /// Returns `true` if this is a sell order (`kind == "sell"`).
    ///
    /// # Returns
    ///
    /// `true` if the order kind is `"sell"`.
    #[must_use]
    pub fn is_sell(&self) -> bool {
        self.kind == "sell"
    }

    /// Returns `true` if this is a buy order (`kind == "buy"`).
    ///
    /// # Returns
    ///
    /// `true` if the order kind is `"buy"`.
    #[must_use]
    pub fn is_buy(&self) -> bool {
        self.kind == "buy"
    }

    /// Returns `true` if the order status is `"open"`.
    ///
    /// # Returns
    ///
    /// `true` if the order status is `"open"`.
    #[must_use]
    pub fn is_open(&self) -> bool {
        self.status == "open"
    }

    /// Returns `true` if the order status is `"filled"`.
    ///
    /// # Returns
    ///
    /// `true` if the order status is `"filled"`.
    #[must_use]
    pub fn is_filled(&self) -> bool {
        self.status == "filled"
    }

    /// Returns `true` if the order status is `"cancelled"`.
    ///
    /// # Returns
    ///
    /// `true` if the order status is `"cancelled"`.
    #[must_use]
    pub fn is_cancelled(&self) -> bool {
        self.status == "cancelled"
    }

    /// Returns `true` if the order status is `"expired"`.
    ///
    /// # Returns
    ///
    /// `true` if the order status is `"expired"`.
    #[must_use]
    pub fn is_expired(&self) -> bool {
        self.status == "expired"
    }

    /// Returns `true` if the order is in a terminal state (filled, cancelled, or expired).
    ///
    /// # Returns
    ///
    /// `true` if the order is filled, cancelled, or expired.
    #[must_use]
    pub fn is_terminal(&self) -> bool {
        self.is_filled() || self.is_cancelled() || self.is_expired()
    }

    /// Returns `true` if a custom receiver address is set (differs from owner).
    ///
    /// # Returns
    ///
    /// `true` if `receiver` is `Some`.
    #[must_use]
    pub const fn has_receiver(&self) -> bool {
        self.receiver.is_some()
    }

    /// Returns `true` if a cancellation/invalidation timestamp is recorded.
    ///
    /// # Returns
    ///
    /// `true` if `invalidate_timestamp` is `Some`.
    #[must_use]
    pub const fn has_invalidate_timestamp(&self) -> bool {
        self.invalidate_timestamp.is_some()
    }

    /// Returns `true` if a surplus value is available for this order.
    ///
    /// # Returns
    ///
    /// `true` if `surplus` is `Some`.
    #[must_use]
    pub const fn has_surplus(&self) -> bool {
        self.surplus.is_some()
    }

    /// Returns `true` if the order may be partially filled.
    ///
    /// # Returns
    ///
    /// `true` if the `partially_fillable` flag is set.
    #[must_use]
    pub const fn is_partially_fillable(&self) -> bool {
        self.partially_fillable
    }

    /// Returns `true` if the order signer is a smart contract (`EIP-1271`).
    ///
    /// # Returns
    ///
    /// `true` if the `is_signer_safe` flag is set.
    #[must_use]
    pub const fn is_signer_safe(&self) -> bool {
        self.is_signer_safe
    }
}

impl fmt::Display for Order {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let short_uid = if self.uid.len() > 10 { &self.uid[..10] } else { &self.uid };
        write!(f, "order({short_uid}{} {})", self.kind, self.status)
    }
}

// ── Pair ──────────────────────────────────────────────────────────────────────

/// A trading pair (two tokens) indexed by the subgraph.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Pair {
    /// Entity ID: `{token0Address}-{token1Address}`.
    pub id: String,
    /// First token (lower address).
    pub token0: Token,
    /// Second token (higher address).
    pub token1: Token,
    /// Total volume in token0 units.
    pub volume_token0: String,
    /// Total volume in token1 units.
    pub volume_token1: String,
    /// Total number of trades for this pair.
    pub number_of_trades: String,
}

impl fmt::Display for Pair {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "pair({}/{})", self.token0, self.token1)
    }
}

/// Per-day statistics for a token pair.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PairDaily {
    /// Entity ID: `{token0Address}-{token1Address}-{dayTimestamp}`.
    pub id: String,
    /// First token in the pair (lower address).
    pub token0: Token,
    /// Second token in the pair (higher address).
    pub token1: Token,
    /// Start-of-day Unix timestamp.
    pub timestamp: String,
    /// Volume in token0 units for this day.
    pub volume_token0: String,
    /// Volume in token1 units for this day.
    pub volume_token1: String,
    /// Number of trades this day.
    pub number_of_trades: String,
}

impl fmt::Display for PairDaily {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "pair-daily({}/{}, ts={})", self.token0, self.token1, self.timestamp)
    }
}

/// Per-hour statistics for a token pair.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PairHourly {
    /// Entity ID: `{token0Address}-{token1Address}-{hourTimestamp}`.
    pub id: String,
    /// First token in the pair.
    pub token0: Token,
    /// Second token in the pair.
    pub token1: Token,
    /// Start-of-hour Unix timestamp.
    pub timestamp: String,
    /// Volume in token0 units.
    pub volume_token0: String,
    /// Volume in token1 units.
    pub volume_token1: String,
    /// Number of trades this hour.
    pub number_of_trades: String,
}

impl fmt::Display for PairHourly {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "pair-hourly({}/{}, ts={})", self.token0, self.token1, self.timestamp)
    }
}

// ── Bundle ────────────────────────────────────────────────────────────────────

/// Aggregate price bundle — contains the current ETH/USD price.
///
/// The subgraph maintains a single `Bundle` entity with `id = "1"`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Bundle {
    /// Always `"1"` (singleton entity).
    pub id: String,
    /// Current ETH price in USD.
    pub eth_price_usd: String,
}

impl Bundle {
    /// Returns the current ETH/USD price as a string slice.
    ///
    /// # Returns
    ///
    /// A `&str` referencing the `eth_price_usd` field.
    #[must_use]
    pub fn eth_price_usd_ref(&self) -> &str {
        &self.eth_price_usd
    }
}

impl fmt::Display for Bundle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "eth-price=${}", self.eth_price_usd)
    }
}

// ── Total (singleton accumulator) ────────────────────────────────────────────

/// Protocol-wide singleton accumulator entity from the subgraph.
///
/// Mirrors the `Total` `GraphQL` type. Unlike [`Totals`] (which is the
/// flattened query-response shape), this type matches the full subgraph
/// entity including its `id` field.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Total {
    /// Singleton entity ID (always `"1"`).
    pub id: String,
    /// Total number of orders placed.
    pub orders: String,
    /// Total number of batch settlements.
    pub settlements: String,
    /// Total number of distinct tokens traded.
    pub tokens: String,
    /// Total number of unique traders.
    pub traders: String,
    /// Total number of trades executed.
    pub number_of_trades: String,
    /// Cumulative volume in ETH.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub volume_eth: Option<String>,
    /// Cumulative volume in USD.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub volume_usd: Option<String>,
    /// Cumulative fees in ETH.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fees_eth: Option<String>,
    /// Cumulative fees in USD.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fees_usd: Option<String>,
}

impl fmt::Display for Total {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "total(orders={}, traders={})", self.orders, self.traders)
    }
}

// ── UniswapToken ─────────────────────────────────────────────────────────────

/// A Uniswap token entity indexed by the `CoW` Protocol subgraph.
///
/// Mirrors the `UniswapToken` `GraphQL` type.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UniswapToken {
    /// Entity ID (token address hex).
    pub id: String,
    /// Token contract address (bytes).
    pub address: String,
    /// Token name from ERC-20 metadata.
    pub name: String,
    /// Token symbol from ERC-20 metadata.
    pub symbol: String,
    /// Decimal places (ERC-20 `decimals()`).
    pub decimals: i32,
    /// Derived price in ETH.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub price_eth: Option<String>,
    /// Derived price in USD.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub price_usd: Option<String>,
}

impl fmt::Display for UniswapToken {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} ({})", self.symbol, self.address)
    }
}

// ── UniswapPool ──────────────────────────────────────────────────────────────

/// A Uniswap pool entity indexed by the `CoW` Protocol subgraph.
///
/// Mirrors the `UniswapPool` `GraphQL` type.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UniswapPool {
    /// Pool contract address.
    pub id: String,
    /// In-range liquidity.
    pub liquidity: String,
    /// Current tick (may be absent for inactive pools).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tick: Option<String>,
    /// First token in the pool.
    pub token0: UniswapToken,
    /// Price of token0 in terms of token1.
    pub token0_price: String,
    /// Second token in the pool.
    pub token1: UniswapToken,
    /// Price of token1 in terms of token0.
    pub token1_price: String,
    /// Total token0 locked across all ticks.
    pub total_value_locked_token0: String,
    /// Total token1 locked across all ticks.
    pub total_value_locked_token1: String,
}

impl fmt::Display for UniswapPool {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "pool({}, {}/{})", self.id, self.token0, self.token1)
    }
}

// ── Subgraph block / meta ────────────────────────────────────────────────────

/// Block information returned by the subgraph `_meta` field.
///
/// Mirrors the `_Block_` `GraphQL` type.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SubgraphBlock {
    /// Block hash (may be `None` if a block-number constraint was used).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hash: Option<String>,
    /// Block number.
    pub number: i64,
    /// Parent block hash.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_hash: Option<String>,
    /// Block timestamp (integer representation).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<i64>,
}

impl fmt::Display for SubgraphBlock {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "block(#{})", self.number)
    }
}

/// Top-level subgraph indexing metadata returned by the `_meta` field.
///
/// Mirrors the `_Meta_` `GraphQL` type.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SubgraphMeta {
    /// Block the subgraph has indexed up to.
    pub block: SubgraphBlock,
    /// Subgraph deployment ID.
    pub deployment: String,
    /// Whether the subgraph encountered indexing errors at some past block.
    pub has_indexing_errors: bool,
}

impl fmt::Display for SubgraphMeta {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "meta(deploy={}, block={})", self.deployment, self.block)
    }
}

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

    // ── Helper constructors ──────────────────────────────────────────────────

    fn sample_token() -> Token {
        Token {
            id: "0xabc".into(),
            address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48".into(),
            first_trade_timestamp: "1700000000".into(),
            name: "USD Coin".into(),
            symbol: "USDC".into(),
            decimals: "6".into(),
            total_volume: "1000000".into(),
            price_eth: "0.0003".into(),
            price_usd: "1.0".into(),
            number_of_trades: "42".into(),
        }
    }

    fn sample_user() -> User {
        User {
            id: "0xuser".into(),
            address: "0xUserAddress".into(),
            first_trade_timestamp: "1700000000".into(),
            number_of_trades: "10".into(),
            solved_amount_eth: "5.0".into(),
            solved_amount_usd: "10000".into(),
        }
    }

    // ── Display impls ────────────────────────────────────────────────────────

    #[test]
    fn totals_display() {
        let t = Totals {
            tokens: "100".into(),
            orders: "500".into(),
            traders: "50".into(),
            settlements: "20".into(),
            volume_usd: "1000000".into(),
            volume_eth: "500".into(),
            fees_usd: "1000".into(),
            fees_eth: "0.5".into(),
        };
        assert_eq!(t.to_string(), "totals(orders=500, traders=50)");
    }

    #[test]
    fn daily_volume_display() {
        let d = DailyVolume { timestamp: "1700000000".into(), volume_usd: "500000".into() };
        assert_eq!(d.to_string(), "daily-vol(ts=1700000000, $500000)");
    }

    #[test]
    fn hourly_volume_display() {
        let h = HourlyVolume { timestamp: "1700000000".into(), volume_usd: "10000".into() };
        assert_eq!(h.to_string(), "hourly-vol(ts=1700000000, $10000)");
    }

    #[test]
    fn daily_total_display() {
        let d = DailyTotal {
            timestamp: "1700000000".into(),
            orders: "100".into(),
            traders: "10".into(),
            tokens: "5".into(),
            settlements: "3".into(),
            volume_eth: "50".into(),
            volume_usd: "100000".into(),
            fees_eth: "0.1".into(),
            fees_usd: "200".into(),
        };
        assert_eq!(d.to_string(), "daily-total(ts=1700000000, orders=100)");
    }

    #[test]
    fn hourly_total_display() {
        let h = HourlyTotal {
            timestamp: "1700000000".into(),
            orders: "10".into(),
            traders: "5".into(),
            tokens: "3".into(),
            settlements: "1".into(),
            volume_eth: "10".into(),
            volume_usd: "20000".into(),
            fees_eth: "0.01".into(),
            fees_usd: "20".into(),
        };
        assert_eq!(h.to_string(), "hourly-total(ts=1700000000, orders=10)");
    }

    #[test]
    fn token_display() {
        let t = sample_token();
        assert_eq!(t.to_string(), "USDC (0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48)");
    }

    #[test]
    fn token_accessors() {
        let t = sample_token();
        assert_eq!(t.symbol_ref(), "USDC");
        assert_eq!(t.address_ref(), "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
    }

    #[test]
    fn user_display() {
        let u = sample_user();
        assert_eq!(u.to_string(), "0xUserAddress");
    }

    #[test]
    fn settlement_display_and_methods() {
        let s = Settlement {
            id: "0xtx".into(),
            tx_hash: "0xdeadbeef".into(),
            first_trade_timestamp: "1700000000".into(),
            solver: "0xsolver".into(),
            tx_cost: Some("1000".into()),
            tx_fee_in_eth: None,
        };
        assert_eq!(s.to_string(), "settlement(0xdeadbeef)");
        assert!(s.has_gas_cost());
        assert!(!s.has_tx_fee());

        let s2 = Settlement {
            id: "0xtx".into(),
            tx_hash: "0xdeadbeef".into(),
            first_trade_timestamp: "1700000000".into(),
            solver: "0xsolver".into(),
            tx_cost: None,
            tx_fee_in_eth: Some("0.01".into()),
        };
        assert!(!s2.has_gas_cost());
        assert!(s2.has_tx_fee());
    }

    #[test]
    fn trade_display_and_has_tx_hash() {
        let t = Trade {
            id: "0x-0".into(),
            timestamp: "1700000000".into(),
            gas_price: "20".into(),
            fee_amount: "100".into(),
            tx_hash: "0xabc".into(),
            settlement: "0xsettle".into(),
            buy_amount: "500".into(),
            sell_amount: "1000".into(),
            sell_amount_before_fees: "1100".into(),
            buy_token: sample_token(),
            sell_token: sample_token(),
            owner: sample_user(),
            order: "0xorder".into(),
        };
        assert!(t.has_tx_hash());
        assert!(t.to_string().contains("0xabc"));
    }

    #[test]
    fn order_methods() {
        let o = Order {
            id: "0xorderuid1234567890".into(),
            owner: sample_user(),
            sell_token: sample_token(),
            buy_token: sample_token(),
            receiver: Some("0xreceiver".into()),
            sell_amount: "1000".into(),
            buy_amount: "500".into(),
            valid_to: "1700000000".into(),
            app_data: "0xappdata".into(),
            fee_amount: "10".into(),
            kind: "sell".into(),
            partially_fillable: true,
            status: "open".into(),
            executed_sell_amount: "0".into(),
            executed_sell_amount_before_fees: "0".into(),
            executed_buy_amount: "0".into(),
            executed_fee_amount: "0".into(),
            invalidate_timestamp: None,
            timestamp: "1700000000".into(),
            tx_hash: "0xtx".into(),
            is_signer_safe: false,
            signing_scheme: "eip712".into(),
            uid: "0xorderuid1234567890abcdef".into(),
            surplus: Some("50".into()),
        };
        assert!(o.is_sell());
        assert!(!o.is_buy());
        assert!(o.is_open());
        assert!(!o.is_filled());
        assert!(!o.is_cancelled());
        assert!(!o.is_expired());
        assert!(!o.is_terminal());
        assert!(o.has_receiver());
        assert!(!o.has_invalidate_timestamp());
        assert!(o.has_surplus());
        assert!(o.is_partially_fillable());
        assert!(!o.is_signer_safe());
        assert!(o.to_string().contains("sell"));
        assert!(o.to_string().contains("open"));
    }

    #[test]
    fn order_terminal_states() {
        let make = |status: &str| Order {
            id: "x".into(),
            owner: sample_user(),
            sell_token: sample_token(),
            buy_token: sample_token(),
            receiver: None,
            sell_amount: "0".into(),
            buy_amount: "0".into(),
            valid_to: "0".into(),
            app_data: "0x".into(),
            fee_amount: "0".into(),
            kind: "buy".into(),
            partially_fillable: false,
            status: status.into(),
            executed_sell_amount: "0".into(),
            executed_sell_amount_before_fees: "0".into(),
            executed_buy_amount: "0".into(),
            executed_fee_amount: "0".into(),
            invalidate_timestamp: Some("100".into()),
            timestamp: "0".into(),
            tx_hash: String::new(),
            is_signer_safe: true,
            signing_scheme: "eip1271".into(),
            uid: "short".into(),
            surplus: None,
        };
        assert!(make("filled").is_filled());
        assert!(make("filled").is_terminal());
        assert!(make("cancelled").is_cancelled());
        assert!(make("cancelled").is_terminal());
        assert!(make("expired").is_expired());
        assert!(make("expired").is_terminal());

        let buy_order = make("open");
        assert!(buy_order.is_buy());
        assert!(buy_order.has_invalidate_timestamp());
        assert!(!buy_order.has_surplus());
        assert!(!buy_order.is_partially_fillable());
        assert!(buy_order.is_signer_safe());
    }

    #[test]
    fn bundle_display_and_accessor() {
        let b = Bundle { id: "1".into(), eth_price_usd: "3500.00".into() };
        assert_eq!(b.to_string(), "eth-price=$3500.00");
        assert_eq!(b.eth_price_usd_ref(), "3500.00");
    }

    #[test]
    fn total_display() {
        let t = Total {
            id: "1".into(),
            orders: "100".into(),
            settlements: "10".into(),
            tokens: "20".into(),
            traders: "30".into(),
            number_of_trades: "200".into(),
            volume_eth: None,
            volume_usd: None,
            fees_eth: None,
            fees_usd: None,
        };
        assert_eq!(t.to_string(), "total(orders=100, traders=30)");
    }

    #[test]
    fn subgraph_block_display() {
        let b = SubgraphBlock {
            hash: Some("0xabc".into()),
            number: 12345,
            parent_hash: None,
            timestamp: Some(1700000000),
        };
        assert_eq!(b.to_string(), "block(#12345)");
    }

    #[test]
    fn subgraph_meta_display() {
        let m = SubgraphMeta {
            block: SubgraphBlock { hash: None, number: 999, parent_hash: None, timestamp: None },
            deployment: "deploy-123".into(),
            has_indexing_errors: false,
        };
        assert_eq!(m.to_string(), "meta(deploy=deploy-123, block=block(#999))");
    }

    #[test]
    fn uniswap_token_display() {
        let t = UniswapToken {
            id: "0xabc".into(),
            address: "0xABC".into(),
            name: "Token".into(),
            symbol: "TKN".into(),
            decimals: 18,
            price_eth: None,
            price_usd: None,
        };
        assert_eq!(t.to_string(), "TKN (0xABC)");
    }

    #[test]
    fn uniswap_pool_display() {
        let t0 = UniswapToken {
            id: "0xa".into(),
            address: "0xA".into(),
            name: "A".into(),
            symbol: "A".into(),
            decimals: 18,
            price_eth: None,
            price_usd: None,
        };
        let t1 = UniswapToken {
            id: "0xb".into(),
            address: "0xB".into(),
            name: "B".into(),
            symbol: "B".into(),
            decimals: 18,
            price_eth: None,
            price_usd: None,
        };
        let p = UniswapPool {
            id: "0xpool".into(),
            liquidity: "1000".into(),
            tick: Some("100".into()),
            token0: t0,
            token0_price: "1.0".into(),
            token1: t1,
            token1_price: "1.0".into(),
            total_value_locked_token0: "500".into(),
            total_value_locked_token1: "500".into(),
        };
        assert_eq!(p.to_string(), "pool(0xpool, A (0xA)/B (0xB))");
    }

    #[test]
    fn pair_display() {
        let p = Pair {
            id: "0xa-0xb".into(),
            token0: sample_token(),
            token1: sample_token(),
            volume_token0: "100".into(),
            volume_token1: "200".into(),
            number_of_trades: "5".into(),
        };
        assert!(p.to_string().starts_with("pair("));
    }

    #[test]
    fn pair_daily_display() {
        let pd = PairDaily {
            id: "0xa-0xb-123".into(),
            token0: sample_token(),
            token1: sample_token(),
            timestamp: "1700000000".into(),
            volume_token0: "100".into(),
            volume_token1: "200".into(),
            number_of_trades: "5".into(),
        };
        assert!(pd.to_string().contains("pair-daily("));
    }

    #[test]
    fn pair_hourly_display() {
        let ph = PairHourly {
            id: "0xa-0xb-123".into(),
            token0: sample_token(),
            token1: sample_token(),
            timestamp: "1700000000".into(),
            volume_token0: "100".into(),
            volume_token1: "200".into(),
            number_of_trades: "5".into(),
        };
        assert!(ph.to_string().contains("pair-hourly("));
    }

    #[test]
    fn token_daily_total_display() {
        let tdt = TokenDailyTotal {
            id: "0x-123".into(),
            token: sample_token(),
            timestamp: "1700000000".into(),
            total_volume: "1000".into(),
            total_volume_usd: "1000".into(),
            total_trades: "10".into(),
            open_price: "1.0".into(),
            close_price: "1.01".into(),
            higher_price: "1.02".into(),
            lower_price: "0.99".into(),
            average_price: "1.005".into(),
        };
        assert!(tdt.to_string().contains("token-daily("));
    }

    #[test]
    fn token_hourly_total_display() {
        let tht = TokenHourlyTotal {
            id: "0x-123".into(),
            token: sample_token(),
            timestamp: "1700000000".into(),
            total_volume: "500".into(),
            total_volume_usd: "500".into(),
            total_trades: "5".into(),
            open_price: "1.0".into(),
            close_price: "1.01".into(),
            higher_price: "1.02".into(),
            lower_price: "0.99".into(),
            average_price: "1.005".into(),
        };
        assert!(tht.to_string().contains("token-hourly("));
    }

    #[test]
    fn token_trading_event_display() {
        let e = TokenTradingEvent {
            id: "evt1".into(),
            token: sample_token(),
            price_usd: "1.01".into(),
            timestamp: "1700000000".into(),
        };
        assert!(e.to_string().contains("trade-event("));
        assert!(e.to_string().contains("price=$1.01"));
    }

    // ── Serde roundtrips ─────────────────────────────────────────────────────

    #[test]
    fn totals_serde_roundtrip() {
        let t = Totals {
            tokens: "100".into(),
            orders: "500".into(),
            traders: "50".into(),
            settlements: "20".into(),
            volume_usd: "1000000".into(),
            volume_eth: "500".into(),
            fees_usd: "1000".into(),
            fees_eth: "0.5".into(),
        };
        let json = serde_json::to_string(&t).unwrap();
        let t2: Totals = serde_json::from_str(&json).unwrap();
        assert_eq!(t2.orders, "500");
    }

    #[test]
    fn settlement_serde_skips_none() {
        let s = Settlement {
            id: "x".into(),
            tx_hash: "0x".into(),
            first_trade_timestamp: "0".into(),
            solver: "0x".into(),
            tx_cost: None,
            tx_fee_in_eth: None,
        };
        let json = serde_json::to_string(&s).unwrap();
        assert!(!json.contains("txCost"));
        assert!(!json.contains("txFeeInEth"));
    }

    #[test]
    fn total_serde_roundtrip_with_optional_fields() {
        let t = Total {
            id: "1".into(),
            orders: "10".into(),
            settlements: "5".into(),
            tokens: "3".into(),
            traders: "2".into(),
            number_of_trades: "20".into(),
            volume_eth: Some("100".into()),
            volume_usd: Some("200000".into()),
            fees_eth: None,
            fees_usd: None,
        };
        let json = serde_json::to_string(&t).unwrap();
        assert!(json.contains("volumeEth"));
        assert!(!json.contains("feesEth"));
        let t2: Total = serde_json::from_str(&json).unwrap();
        assert_eq!(t2.volume_eth, Some("100".into()));
        assert_eq!(t2.fees_eth, None);
    }

    #[test]
    fn bundle_serde_roundtrip() {
        let b = Bundle { id: "1".into(), eth_price_usd: "3500".into() };
        let json = serde_json::to_string(&b).unwrap();
        assert!(json.contains("ethPriceUsd"));
        let b2: Bundle = serde_json::from_str(&json).unwrap();
        assert_eq!(b2.eth_price_usd, "3500");
    }
}