polyoxide-data 0.19.0

Rust client library for Polymarket Data API
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
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
use serde::{Deserialize, Serialize};

/// User's total position value
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserValue {
    /// User address
    pub user: String,
    /// Total value of positions
    pub value: f64,
}

/// Open interest for a market
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenInterest {
    /// Market condition ID
    pub market: String,
    /// Open interest value
    pub value: f64,
}

/// Sort field options for position queries
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum PositionSortBy {
    /// Sort by current value
    Current,
    /// Sort by initial value
    Initial,
    /// Sort by token count
    Tokens,
    /// Sort by cash P&L
    CashPnl,
    /// Sort by percentage P&L
    PercentPnl,
    /// Sort by market title
    Title,
    /// Sort by resolving status
    Resolving,
    /// Sort by price
    Price,
    /// Sort by average price
    AvgPrice,
}

impl std::fmt::Display for PositionSortBy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Current => write!(f, "CURRENT"),
            Self::Initial => write!(f, "INITIAL"),
            Self::Tokens => write!(f, "TOKENS"),
            Self::CashPnl => write!(f, "CASH_PNL"),
            Self::PercentPnl => write!(f, "PERCENT_PNL"),
            Self::Title => write!(f, "TITLE"),
            Self::Resolving => write!(f, "RESOLVING"),
            Self::Price => write!(f, "PRICE"),
            Self::AvgPrice => write!(f, "AVG_PRICE"),
        }
    }
}

/// Sort direction for queries
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "UPPERCASE")]
pub enum SortDirection {
    /// Ascending order
    Asc,
    /// Descending order (default)
    #[default]
    Desc,
}

impl std::fmt::Display for SortDirection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Asc => write!(f, "ASC"),
            Self::Desc => write!(f, "DESC"),
        }
    }
}

/// Sort field options for closed position queries
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ClosedPositionSortBy {
    /// Sort by realized P&L (default)
    #[default]
    RealizedPnl,
    /// Sort by market title
    Title,
    /// Sort by price
    Price,
    /// Sort by average price
    AvgPrice,
    /// Sort by timestamp
    Timestamp,
}

impl std::fmt::Display for ClosedPositionSortBy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::RealizedPnl => write!(f, "REALIZED_PNL"),
            Self::Title => write!(f, "TITLE"),
            Self::Price => write!(f, "PRICE"),
            Self::AvgPrice => write!(f, "AVG_PRICE"),
            Self::Timestamp => write!(f, "TIMESTAMP"),
        }
    }
}

/// Closed position record
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClosedPosition {
    /// Proxy wallet address
    pub proxy_wallet: String,
    /// Asset identifier (token ID)
    pub asset: String,
    /// Condition ID of the market
    pub condition_id: String,
    /// Average entry price
    pub avg_price: f64,
    /// Total amount bought
    pub total_bought: f64,
    /// Realized profit and loss
    pub realized_pnl: f64,
    /// Current market price
    pub cur_price: f64,
    /// Timestamp when position was closed
    #[cfg_attr(feature = "specta", specta(type = f64))]
    pub timestamp: i64,
    /// Market title
    pub title: String,
    /// Market slug
    pub slug: String,
    /// Market icon URL
    pub icon: Option<String>,
    /// Event slug
    pub event_slug: Option<String>,
    /// Outcome name (e.g., "Yes", "No")
    pub outcome: String,
    /// Outcome index (0 or 1 for binary markets)
    pub outcome_index: u32,
    /// Opposite outcome name
    pub opposite_outcome: String,
    /// Opposite outcome asset ID
    pub opposite_asset: String,
    /// Market end date
    pub end_date: Option<String>,
}

/// Trade side (buy or sell)
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum TradeSide {
    /// Buy order
    Buy,
    /// Sell order
    Sell,
    /// Unrecognized trade side (forward-compat). `Trade::side` is deserialized
    /// from live API responses, so an unexpected value falls back here instead
    /// of failing the whole page. Never construct this to send in a request filter.
    #[serde(other)]
    Unknown,
}

impl std::fmt::Display for TradeSide {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Buy => write!(f, "BUY"),
            Self::Sell => write!(f, "SELL"),
            Self::Unknown => write!(f, "UNKNOWN"),
        }
    }
}

/// Filter type for trade queries
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum TradeFilterType {
    /// Filter by cash amount
    Cash,
    /// Filter by token amount
    Tokens,
}

impl std::fmt::Display for TradeFilterType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Cash => write!(f, "CASH"),
            Self::Tokens => write!(f, "TOKENS"),
        }
    }
}

/// Trade record
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Trade {
    /// Proxy wallet address
    pub proxy_wallet: String,
    /// Trade side (BUY or SELL)
    pub side: TradeSide,
    /// Asset identifier (token ID)
    pub asset: String,
    /// Condition ID of the market
    pub condition_id: String,
    /// Trade size (number of shares)
    pub size: f64,
    /// Trade price
    pub price: f64,
    /// Trade timestamp
    #[cfg_attr(feature = "specta", specta(type = f64))]
    pub timestamp: i64,
    /// Market title
    pub title: String,
    /// Market slug
    pub slug: String,
    /// Market icon URL
    pub icon: Option<String>,
    /// Event slug
    pub event_slug: Option<String>,
    /// Outcome name (e.g., "Yes", "No")
    pub outcome: String,
    /// Outcome index (0 or 1 for binary markets)
    pub outcome_index: u32,
    /// User display name
    pub name: Option<String>,
    /// User pseudonym
    pub pseudonym: Option<String>,
    /// User bio
    pub bio: Option<String>,
    /// User profile image URL
    pub profile_image: Option<String>,
    /// Optimized profile image URL
    pub profile_image_optimized: Option<String>,
    /// Transaction hash
    pub transaction_hash: Option<String>,
}

/// Activity type
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum ActivityType {
    /// Trade activity
    Trade,
    /// Split activity
    Split,
    /// Merge activity
    Merge,
    /// Redeem activity
    Redeem,
    /// Reward activity
    Reward,
    /// Conversion activity
    Conversion,
    /// Collateral deposit
    Deposit,
    /// Collateral withdrawal
    Withdrawal,
    /// Yield accrual on collateral
    Yield,
    /// Maker rebate activity
    #[serde(rename = "MAKER_REBATE")]
    MakerRebate,
    /// Referral reward activity
    #[serde(rename = "REFERRAL_REWARD")]
    ReferralReward,
    /// Taker rebate activity
    #[serde(rename = "TAKER_REBATE")]
    TakerRebate,
    /// Unrecognized activity type (forward-compat). Never construct this to
    /// send in a request filter; [`super::api::users::ListActivity::activity_type`]
    /// silently drops it since the upstream API has no matching value to filter on.
    #[serde(other)]
    Unknown,
}

impl std::fmt::Display for ActivityType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Trade => write!(f, "TRADE"),
            Self::Split => write!(f, "SPLIT"),
            Self::Merge => write!(f, "MERGE"),
            Self::Redeem => write!(f, "REDEEM"),
            Self::Reward => write!(f, "REWARD"),
            Self::Conversion => write!(f, "CONVERSION"),
            Self::Deposit => write!(f, "DEPOSIT"),
            Self::Withdrawal => write!(f, "WITHDRAWAL"),
            Self::Yield => write!(f, "YIELD"),
            Self::MakerRebate => write!(f, "MAKER_REBATE"),
            Self::ReferralReward => write!(f, "REFERRAL_REWARD"),
            Self::TakerRebate => write!(f, "TAKER_REBATE"),
            Self::Unknown => write!(f, "UNKNOWN"),
        }
    }
}

/// Sort field options for activity queries
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ActivitySortBy {
    /// Sort by timestamp (default)
    #[default]
    Timestamp,
    /// Sort by token amount
    Tokens,
    /// Sort by cash amount
    Cash,
}

impl std::fmt::Display for ActivitySortBy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Timestamp => write!(f, "TIMESTAMP"),
            Self::Tokens => write!(f, "TOKENS"),
            Self::Cash => write!(f, "CASH"),
        }
    }
}

/// User activity record
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Activity {
    /// Proxy wallet address
    pub proxy_wallet: String,
    /// Activity timestamp
    #[cfg_attr(feature = "specta", specta(type = f64))]
    pub timestamp: i64,
    /// Condition ID of the market
    pub condition_id: String,
    /// Activity type
    #[serde(rename = "type")]
    pub activity_type: ActivityType,
    /// Token quantity
    pub size: f64,
    /// USD value
    pub usdc_size: f64,
    /// On-chain transaction hash
    pub transaction_hash: Option<String>,
    /// Execution price
    pub price: Option<f64>,
    /// Asset identifier (token ID)
    pub asset: Option<String>,
    // Deserialize into String because the API can return an empty string
    /// Trade side (BUY or SELL)
    pub side: Option<String>,
    /// Outcome index (0 or 1 for binary markets)
    pub outcome_index: Option<u32>,
    /// Market title
    pub title: Option<String>,
    /// Market slug
    pub slug: Option<String>,
    /// Market icon URL
    pub icon: Option<String>,
    /// Outcome name (e.g., "Yes", "No")
    pub outcome: Option<String>,
    /// User display name
    pub name: Option<String>,
    /// User pseudonym
    pub pseudonym: Option<String>,
    /// User bio
    pub bio: Option<String>,
    /// User profile image URL
    pub profile_image: Option<String>,
    /// Optimized profile image URL
    pub profile_image_optimized: Option<String>,
}

/// User position in a market
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Position {
    /// Proxy wallet address
    pub proxy_wallet: String,
    /// Asset identifier (token ID)
    pub asset: String,
    /// Condition ID of the market
    pub condition_id: String,
    /// Position size (number of shares)
    pub size: f64,
    /// Average entry price
    pub avg_price: f64,
    /// Initial value of position
    pub initial_value: f64,
    /// Current value of position
    pub current_value: f64,
    /// Cash profit and loss
    pub cash_pnl: f64,
    /// Percentage profit and loss
    pub percent_pnl: f64,
    /// Total amount bought
    pub total_bought: f64,
    /// Realized profit and loss
    pub realized_pnl: f64,
    /// Percentage realized P&L
    pub percent_realized_pnl: f64,
    /// Current market price
    pub cur_price: f64,
    /// Whether position is redeemable
    pub redeemable: bool,
    /// Whether position is mergeable
    pub mergeable: bool,
    /// Market title
    pub title: String,
    /// Market slug
    pub slug: String,
    /// Market icon URL
    pub icon: Option<String>,
    /// Event slug
    pub event_slug: Option<String>,
    /// Outcome name (e.g., "Yes", "No")
    pub outcome: String,
    /// Outcome index (0 or 1 for binary markets)
    pub outcome_index: u32,
    /// Opposite outcome name
    pub opposite_outcome: String,
    /// Opposite outcome asset ID
    pub opposite_asset: String,
    /// Market end date
    pub end_date: Option<String>,
    /// Whether this is a negative risk market
    pub negative_risk: bool,
}

/// A per-user position in a single market, as returned by `/v1/market-positions`.
///
/// Field names and types follow the upstream `MarketPositionV1` schema in
/// `docs/specs/data/openapi.yaml`.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MarketPositionV1 {
    /// Proxy wallet address of the position holder
    pub proxy_wallet: String,
    /// Display name of the position holder
    pub name: String,
    /// Profile image URL of the position holder
    pub profile_image: Option<String>,
    /// Whether the holder has a verified badge
    pub verified: bool,
    /// Outcome token asset ID
    pub asset: String,
    /// Condition ID of the market
    pub condition_id: String,
    /// Average entry price
    pub avg_price: f64,
    /// Position size (number of shares)
    pub size: f64,
    /// Current market price (OpenAPI field: `currPrice`)
    #[serde(rename = "currPrice")]
    pub curr_price: f64,
    /// Current value of the position
    pub current_value: f64,
    /// Unrealized cash P&L
    pub cash_pnl: f64,
    /// Total amount bought
    pub total_bought: f64,
    /// Realized P&L
    pub realized_pnl: f64,
    /// Total P&L (cash + realized)
    pub total_pnl: f64,
    /// Outcome name (e.g., "Yes", "No")
    pub outcome: String,
    /// Outcome index (0 or 1 for binary markets)
    pub outcome_index: u32,
}

/// Market positions grouped by outcome token.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetaMarketPositionV1 {
    /// Outcome token asset ID
    pub token: String,
    /// Positions for this token
    pub positions: Vec<MarketPositionV1>,
}

/// Status filter for `/v1/market-positions`.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "UPPERCASE")]
pub enum MarketPositionStatus {
    /// Only positions with size > 0.01
    Open,
    /// Only positions with size <= 0.01
    Closed,
    /// All positions regardless of size (default)
    #[default]
    All,
}

impl std::fmt::Display for MarketPositionStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Open => write!(f, "OPEN"),
            Self::Closed => write!(f, "CLOSED"),
            Self::All => write!(f, "ALL"),
        }
    }
}

/// Sort field options for `/v1/market-positions`.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum MarketPositionSortBy {
    /// Sort by token count
    Tokens,
    /// Sort by unrealized cash P&L
    CashPnl,
    /// Sort by realized P&L
    RealizedPnl,
    /// Sort by total P&L (cash + realized). Default.
    #[default]
    TotalPnl,
}

impl std::fmt::Display for MarketPositionSortBy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Tokens => write!(f, "TOKENS"),
            Self::CashPnl => write!(f, "CASH_PNL"),
            Self::RealizedPnl => write!(f, "REALIZED_PNL"),
            Self::TotalPnl => write!(f, "TOTAL_PNL"),
        }
    }
}

/// Time period for aggregation
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "UPPERCASE")]
pub enum TimePeriod {
    /// Daily aggregation (default)
    #[default]
    Day,
    /// Weekly aggregation
    Week,
    /// Monthly aggregation
    Month,
    /// All time aggregation
    All,
}

impl std::fmt::Display for TimePeriod {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Day => write!(f, "DAY"),
            Self::Week => write!(f, "WEEK"),
            Self::Month => write!(f, "MONTH"),
            Self::All => write!(f, "ALL"),
        }
    }
}

// ---------------------------------------------------------------------------
// Combinatorial (multi-market) positions and activity
//
// Monetary and share fields on these types are kept as `String` rather than
// `f64` deliberately: upstream documents them as "six-decimal
// precision-preserving" values and instructs clients to parse them as decimals,
// never through a float. Round-tripping them through `f64` would silently lose
// precision on large balances.
// ---------------------------------------------------------------------------

/// Resolution state of a combinatorial position.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ComboStatus {
    /// No leg has resolved yet.
    Open,
    /// Some legs have resolved; the combo is still live.
    Partial,
    /// Resolved at a fractional payout (e.g. a leg voided 50/50) — redemption
    /// pays the fractional value per share.
    ResolvedPartial,
    /// Resolved in the holder's favour; shares redeem 1:1 at $1.
    ResolvedWin,
    /// Resolved against the holder; shares are worthless.
    ResolvedLoss,
    /// Unrecognized status (forward-compat). Never construct this to send in a
    /// request filter; [`crate::api::combos::ListComboPositions::status`] drops
    /// it since the upstream API has no matching value to filter on.
    #[serde(other)]
    Unknown,
}

impl std::fmt::Display for ComboStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Open => write!(f, "OPEN"),
            Self::Partial => write!(f, "PARTIAL"),
            Self::ResolvedPartial => write!(f, "RESOLVED_PARTIAL"),
            Self::ResolvedWin => write!(f, "RESOLVED_WIN"),
            Self::ResolvedLoss => write!(f, "RESOLVED_LOSS"),
            Self::Unknown => write!(f, "UNKNOWN"),
        }
    }
}

/// Resolution state of a single leg within a combinatorial position.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ComboLegStatus {
    /// The leg's market has not resolved.
    Open,
    /// The leg's market resolved with a fractional payout (e.g. a 50/50 void).
    ResolvedPartial,
    /// The leg resolved in the holder's favour.
    ResolvedWin,
    /// The leg resolved against the holder.
    ResolvedLoss,
    /// Unrecognized leg status (forward-compat).
    #[serde(other)]
    Unknown,
}

impl std::fmt::Display for ComboLegStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Open => write!(f, "OPEN"),
            Self::ResolvedPartial => write!(f, "RESOLVED_PARTIAL"),
            Self::ResolvedWin => write!(f, "RESOLVED_WIN"),
            Self::ResolvedLoss => write!(f, "RESOLVED_LOSS"),
            Self::Unknown => write!(f, "UNKNOWN"),
        }
    }
}

/// Sort order for [`crate::api::combos::ListComboPositions`].
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ComboSort {
    /// Highest current value first (default).
    #[default]
    CurrentValueDesc,
    /// Highest entry cost first.
    EntryCostDesc,
    /// Most recently entered first.
    FirstEntryDesc,
    /// Most recently resolved first.
    ResolvedAtDesc,
    /// Oldest `updated_at` first — the sort to use for incremental sync
    /// alongside [`crate::api::combos::ListComboPositions::updated_after`].
    UpdatedAsc,
}

impl std::fmt::Display for ComboSort {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::CurrentValueDesc => write!(f, "current_value_desc"),
            Self::EntryCostDesc => write!(f, "entry_cost_desc"),
            Self::FirstEntryDesc => write!(f, "first_entry_desc"),
            Self::ResolvedAtDesc => write!(f, "resolved_at_desc"),
            Self::UpdatedAsc => write!(f, "updated_asc"),
        }
    }
}

/// Standard pagination metadata for combo endpoints.
///
/// There is no total count; `has_more` is derived from page fullness.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pagination {
    /// Page size used for this response.
    pub limit: i64,
    /// Offset used for this response.
    pub offset: i64,
    /// Whether another page is available.
    pub has_more: bool,
    /// Opaque signed cursor for the next page; `None` when `has_more` is false.
    ///
    /// Pass it back verbatim via `cursor(..)`, keeping the same sort. Never
    /// parse or construct it. Using the cursor makes deep pagination O(page)
    /// and stable against concurrent inserts.
    pub next_cursor: Option<String>,
}

/// Event metadata attached to a combo leg's market.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComboEvent {
    /// Event identifier.
    pub event_id: Option<String>,
    /// URL slug for the event.
    pub event_slug: Option<String>,
    /// Human-readable event title.
    pub event_title: Option<String>,
    /// Event image URL.
    pub event_image: Option<String>,
}

/// Market metadata attached to a combo leg.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComboMarket {
    /// Market identifier.
    pub market_id: Option<String>,
    /// URL slug for the market.
    pub slug: Option<String>,
    /// Market title.
    pub title: Option<String>,
    /// Outcome label this leg refers to.
    pub outcome: Option<String>,
    /// Market image URL.
    pub image_url: Option<String>,
    /// Market icon URL.
    pub icon_url: Option<String>,
    /// Market category.
    pub category: Option<String>,
    /// Market subcategory.
    pub subcategory: Option<String>,
    /// Tags applied to the market.
    #[serde(default)]
    pub tags: Vec<String>,
    /// Market end date (RFC3339 UTC).
    pub end_date: Option<String>,
    /// Parent event metadata.
    pub event: Option<ComboEvent>,
}

/// A single leg of a combinatorial position.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComboLeg {
    /// Zero-based index of this leg within the combo.
    pub leg_index: i64,
    /// Position identifier for the leg.
    pub leg_position_id: Option<String>,
    /// The leg market's condition ID (distinct from the combo's).
    pub leg_condition_id: Option<String>,
    /// Index of the selected outcome within the leg market.
    pub leg_outcome_index: Option<i64>,
    /// Label of the selected outcome.
    pub leg_outcome_label: Option<String>,
    /// Live per-leg resolution state, derived from the leg market's on-chain
    /// payout vector.
    pub leg_status: Option<ComboLegStatus>,
    /// RFC3339 UTC. Set once the leg's market resolves on-chain, including
    /// fractional resolutions that still report `leg_status` `Open`.
    pub leg_resolved_at: Option<String>,
    /// Live price for the leg outcome (decimal string, 0–1). `"0"` when no
    /// price is available.
    pub leg_current_price: Option<String>,
    /// Market metadata for the leg.
    pub market: Option<ComboMarket>,
}

/// A combinatorial (multi-market) position held by a user.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComboPosition {
    /// Combo condition ID (`0x` + 62 hex). Equals the `conditionId` of
    /// `isCombo` rows on `/activity`.
    pub combo_condition_id: String,
    /// Position identifier for the combo.
    pub combo_position_id: Option<String>,
    /// Module identifier; `3` is the Combinatorial module.
    pub module_id: Option<i64>,
    /// Holder's wallet address.
    pub user_address: Option<String>,
    /// Share balance as a precision-preserving decimal string.
    pub shares_balance: Option<String>,
    /// Average entry price in USDC, as a decimal string.
    pub entry_avg_price_usdc: Option<String>,
    /// *Remaining* cost basis (`entry_avg_price × shares_balance`).
    ///
    /// Reads ~0 after a winning combo is redeemed — use
    /// [`total_cost_usdc`](Self::total_cost_usdc) to display what was paid on
    /// closed positions.
    pub entry_cost_usdc: Option<String>,
    /// Gross redemption proceeds (winning shares redeem 1:1 at $1).
    ///
    /// `"0.00"` while open, unredeemed, or resolved-loss; accumulates under
    /// `Partial`. This is gross payout, not net PnL — net =
    /// `realized_payout_usdc − total_cost_usdc`.
    pub realized_payout_usdc: Option<String>,
    /// Original cost basis, surviving redemption burning the shares. Equals
    /// [`entry_cost_usdc`](Self::entry_cost_usdc) while open.
    pub total_cost_usdc: Option<String>,
    /// Exact gross entry basis including attributed BUY fees, as a six-decimal
    /// precision-preserving string (e.g. `"8999.997488"`).
    ///
    /// Tracks the remaining basis while the position is live and freezes once
    /// it is terminal. Exact net basis = `gross_entry_cost_usdc −
    /// entry_fees_usdc`. Parse as a decimal, never through a float.
    pub gross_entry_cost_usdc: Option<String>,
    /// BUY-fee portion of the same basis, as a six-decimal precision-preserving
    /// string. SELL fees are excluded; always ≤ `gross_entry_cost_usdc`.
    pub entry_fees_usdc: Option<String>,
    /// Resolution state of the combo.
    pub status: Option<ComboStatus>,
    /// First entry time (RFC3339 UTC).
    pub first_entry_at: Option<String>,
    /// Resolution time (RFC3339 UTC), or `None` while unresolved.
    pub resolved_at: Option<String>,
    /// Last-modified time (UTC, ISO 8601) — the incremental-sync watermark.
    ///
    /// Bumps on any recompute of the row (trade, redemption, resolution
    /// classification). Omitted on responses served by the legacy backend.
    pub updated_at: Option<String>,
    /// Total number of legs.
    pub legs_total: Option<i64>,
    /// Number of legs that have resolved.
    pub legs_resolved: Option<i64>,
    /// Number of legs still pending.
    pub legs_pending: Option<i64>,
    /// Per-leg breakdown.
    #[serde(default)]
    pub legs: Vec<ComboLeg>,
}

/// Response envelope for `GET /v1/positions/combos`.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CombosResponse {
    /// The combo positions in this page.
    #[serde(default)]
    pub combos: Vec<ComboPosition>,
    /// Pagination metadata.
    pub pagination: Option<Pagination>,
}

/// A combo lifecycle or redeem event.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComboActivity {
    /// Event identifier.
    pub id: Option<String>,
    /// Event type (split, merge, convert, compress, wrap, unwrap, redeem).
    ///
    /// Upstream documents `type` as the replacement for the deprecated
    /// [`event_kind`](Self::event_kind) and [`side`](Self::side) fields, but
    /// does not yet list it in the published schema — treat it as optional
    /// until it appears there.
    #[serde(rename = "type")]
    pub activity_type: Option<String>,
    /// Raw on-chain event name (e.g. `PositionsSplit`).
    #[deprecated(note = "upstream deprecated this field; use `activity_type` instead")]
    pub event_kind: Option<String>,
    /// Normalized rendering label (e.g. `Split`).
    #[deprecated(note = "upstream deprecated this field; use `activity_type` instead")]
    pub side: Option<String>,
    /// Module kind; always `Combinatorial`.
    pub module_kind: Option<String>,
    /// Holder's wallet address.
    pub user_address: Option<String>,
    /// Combo condition ID (`0x` + 62 hex).
    pub combo_condition_id: Option<String>,
    /// Position identifier for the combo.
    pub combo_position_id: Option<String>,
    /// Module identifier.
    pub module_id: Option<i64>,
    /// Lifecycle amount in USDC; `None` on redeems.
    pub amount_usdc: Option<f64>,
    /// Redeem payout in USDC; `None` on lifecycle events.
    pub payout_usdc: Option<f64>,
    /// Event time as a Unix timestamp (seconds).
    pub timestamp: Option<i64>,
    /// Transaction time (RFC3339 UTC).
    pub tx_dttm: Option<String>,
    /// Transaction hash.
    pub tx_hash: Option<String>,
    /// Log index within the transaction.
    pub log_index: Option<i64>,
    /// Block number.
    pub block_number: Option<i64>,
    /// Per-leg breakdown.
    #[serde(default)]
    pub legs: Vec<ComboLeg>,
}

/// Response envelope for `GET /v1/activity/combos`.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CombosActivityResponse {
    /// The combo activity rows in this page.
    #[serde(default)]
    pub activity: Vec<ComboActivity>,
    /// Pagination metadata.
    pub pagination: Option<Pagination>,
}

/// "Other" outcome size held by a user in an augmented neg-risk event.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OtherSize {
    /// Gamma event ID of the augmented neg-risk event.
    pub id: Option<i64>,
    /// User wallet address.
    pub user: Option<String>,
    /// Size of the "Other" position.
    pub size: Option<f64>,
}

/// A single moderated revision of a question.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RevisionEntry {
    /// Revised question text.
    pub revision: Option<String>,
    /// Revision time as a Unix timestamp (seconds).
    pub timestamp: Option<i64>,
}

/// Moderated revisions for a question.
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RevisionPayload {
    /// Question ID (`0x` + 64 hex).
    #[serde(rename = "questionID")]
    pub question_id: Option<String>,
    /// Revisions recorded for the question, oldest first.
    #[serde(default)]
    pub revisions: Vec<RevisionEntry>,
}

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

    /// Verify Display matches serde serialization for all PositionSortBy variants.
    #[test]
    fn position_sort_by_display_matches_serde() {
        let variants = [
            PositionSortBy::Current,
            PositionSortBy::Initial,
            PositionSortBy::Tokens,
            PositionSortBy::CashPnl,
            PositionSortBy::PercentPnl,
            PositionSortBy::Title,
            PositionSortBy::Resolving,
            PositionSortBy::Price,
            PositionSortBy::AvgPrice,
        ];
        for variant in variants {
            let serialized = serde_json::to_value(variant).unwrap();
            let display = variant.to_string();
            assert_eq!(
                format!("\"{}\"", display),
                serialized.to_string(),
                "Display mismatch for {:?}",
                variant
            );
        }
    }

    /// Verify Display matches serde serialization for all ClosedPositionSortBy variants.
    #[test]
    fn closed_position_sort_by_display_matches_serde() {
        let variants = [
            ClosedPositionSortBy::RealizedPnl,
            ClosedPositionSortBy::Title,
            ClosedPositionSortBy::Price,
            ClosedPositionSortBy::AvgPrice,
            ClosedPositionSortBy::Timestamp,
        ];
        for variant in variants {
            let serialized = serde_json::to_value(variant).unwrap();
            let display = variant.to_string();
            assert_eq!(
                format!("\"{}\"", display),
                serialized.to_string(),
                "Display mismatch for {:?}",
                variant
            );
        }
    }

    #[test]
    fn activity_sort_by_display_matches_serde() {
        let variants = [
            ActivitySortBy::Timestamp,
            ActivitySortBy::Tokens,
            ActivitySortBy::Cash,
        ];
        for variant in variants {
            let serialized = serde_json::to_value(variant).unwrap();
            let display = variant.to_string();
            assert_eq!(
                format!("\"{}\"", display),
                serialized.to_string(),
                "Display mismatch for {:?}",
                variant
            );
        }
    }

    #[test]
    fn sort_direction_display_matches_serde() {
        let variants = [SortDirection::Asc, SortDirection::Desc];
        for variant in variants {
            let serialized = serde_json::to_value(variant).unwrap();
            let display = variant.to_string();
            assert_eq!(
                format!("\"{}\"", display),
                serialized.to_string(),
                "Display mismatch for {:?}",
                variant
            );
        }
    }

    #[test]
    fn trade_side_display_matches_serde() {
        let variants = [TradeSide::Buy, TradeSide::Sell, TradeSide::Unknown];
        for variant in variants {
            let serialized = serde_json::to_value(variant).unwrap();
            let display = variant.to_string();
            assert_eq!(
                format!("\"{}\"", display),
                serialized.to_string(),
                "Display mismatch for {:?}",
                variant
            );
        }
    }

    #[test]
    fn trade_side_falls_back_to_unknown_for_future_values() {
        let result: TradeSide = serde_json::from_str("\"SOME_FUTURE_SIDE\"").unwrap();
        assert_eq!(result, TradeSide::Unknown);
    }

    #[test]
    fn trade_filter_type_display_matches_serde() {
        let variants = [TradeFilterType::Cash, TradeFilterType::Tokens];
        for variant in variants {
            let serialized = serde_json::to_value(variant).unwrap();
            let display = variant.to_string();
            assert_eq!(
                format!("\"{}\"", display),
                serialized.to_string(),
                "Display mismatch for {:?}",
                variant
            );
        }
    }

    #[test]
    fn activity_type_display_matches_serde() {
        let variants = [
            ActivityType::Trade,
            ActivityType::Split,
            ActivityType::Merge,
            ActivityType::Redeem,
            ActivityType::Reward,
            ActivityType::Conversion,
            ActivityType::MakerRebate,
            ActivityType::ReferralReward,
            ActivityType::TakerRebate,
            ActivityType::Unknown,
        ];
        for variant in variants {
            let serialized = serde_json::to_value(variant).unwrap();
            let display = variant.to_string();
            assert_eq!(
                format!("\"{}\"", display),
                serialized.to_string(),
                "Display mismatch for {:?}",
                variant
            );
        }
    }

    #[test]
    fn activity_type_roundtrip_serde() {
        for variant in [
            ActivityType::Trade,
            ActivityType::Split,
            ActivityType::Merge,
            ActivityType::Redeem,
            ActivityType::Reward,
            ActivityType::Conversion,
            ActivityType::MakerRebate,
            ActivityType::ReferralReward,
            ActivityType::TakerRebate,
        ] {
            let json = serde_json::to_string(&variant).unwrap();
            let deserialized: ActivityType = serde_json::from_str(&json).unwrap();
            assert_eq!(variant, deserialized);
        }
    }

    #[test]
    fn activity_type_falls_back_to_unknown_for_future_types() {
        // A hypothetical future activity type Polymarket hasn't documented yet.
        let result: ActivityType = serde_json::from_str("\"SOME_FUTURE_TYPE\"").unwrap();
        assert_eq!(result, ActivityType::Unknown);

        // Case mismatches also fall back rather than poisoning the whole page.
        let result: ActivityType = serde_json::from_str("\"trade\"").unwrap();
        assert_eq!(result, ActivityType::Unknown);
    }

    #[test]
    fn sort_direction_default_is_desc() {
        assert_eq!(SortDirection::default(), SortDirection::Desc);
    }

    #[test]
    fn closed_position_sort_by_default_is_realized_pnl() {
        assert_eq!(
            ClosedPositionSortBy::default(),
            ClosedPositionSortBy::RealizedPnl
        );
    }

    #[test]
    fn activity_sort_by_default_is_timestamp() {
        assert_eq!(ActivitySortBy::default(), ActivitySortBy::Timestamp);
    }

    #[test]
    fn position_sort_by_serde_roundtrip() {
        for variant in [
            PositionSortBy::Current,
            PositionSortBy::Initial,
            PositionSortBy::Tokens,
            PositionSortBy::CashPnl,
            PositionSortBy::PercentPnl,
            PositionSortBy::Title,
            PositionSortBy::Resolving,
            PositionSortBy::Price,
            PositionSortBy::AvgPrice,
        ] {
            let json = serde_json::to_string(&variant).unwrap();
            let deserialized: PositionSortBy = serde_json::from_str(&json).unwrap();
            assert_eq!(variant, deserialized);
        }
    }

    #[test]
    fn deserialize_position_from_json() {
        let json = r#"{
            "proxyWallet": "0xabc123",
            "asset": "token123",
            "conditionId": "cond456",
            "size": 100.5,
            "avgPrice": 0.65,
            "initialValue": 65.0,
            "currentValue": 70.0,
            "cashPnl": 5.0,
            "percentPnl": 7.69,
            "totalBought": 100.5,
            "realizedPnl": 2.0,
            "percentRealizedPnl": 3.08,
            "curPrice": 0.70,
            "redeemable": false,
            "mergeable": true,
            "title": "Will X happen?",
            "slug": "will-x-happen",
            "icon": "https://example.com/icon.png",
            "eventSlug": "x-event",
            "outcome": "Yes",
            "outcomeIndex": 0,
            "oppositeOutcome": "No",
            "oppositeAsset": "token789",
            "endDate": "2025-12-31",
            "negativeRisk": false
        }"#;

        let pos: Position = serde_json::from_str(json).unwrap();
        assert_eq!(pos.proxy_wallet, "0xabc123");
        assert_eq!(pos.asset, "token123");
        assert_eq!(pos.condition_id, "cond456");
        assert!((pos.size - 100.5).abs() < f64::EPSILON);
        assert!((pos.avg_price - 0.65).abs() < f64::EPSILON);
        assert!((pos.initial_value - 65.0).abs() < f64::EPSILON);
        assert!((pos.current_value - 70.0).abs() < f64::EPSILON);
        assert!((pos.cash_pnl - 5.0).abs() < f64::EPSILON);
        assert!(!pos.redeemable);
        assert!(pos.mergeable);
        assert_eq!(pos.title, "Will X happen?");
        assert_eq!(pos.outcome, "Yes");
        assert_eq!(pos.outcome_index, 0);
        assert_eq!(pos.opposite_outcome, "No");
        assert!(!pos.negative_risk);
        assert_eq!(pos.icon, Some("https://example.com/icon.png".to_string()));
        assert_eq!(pos.event_slug, Some("x-event".to_string()));
    }

    #[test]
    fn deserialize_position_with_null_optionals() {
        let json = r#"{
            "proxyWallet": "0xabc123",
            "asset": "token123",
            "conditionId": "cond456",
            "size": 0.0,
            "avgPrice": 0.0,
            "initialValue": 0.0,
            "currentValue": 0.0,
            "cashPnl": 0.0,
            "percentPnl": 0.0,
            "totalBought": 0.0,
            "realizedPnl": 0.0,
            "percentRealizedPnl": 0.0,
            "curPrice": 0.0,
            "redeemable": false,
            "mergeable": false,
            "title": "Test",
            "slug": "test",
            "icon": null,
            "eventSlug": null,
            "outcome": "No",
            "outcomeIndex": 1,
            "oppositeOutcome": "Yes",
            "oppositeAsset": "token000",
            "endDate": null,
            "negativeRisk": true
        }"#;

        let pos: Position = serde_json::from_str(json).unwrap();
        assert!(pos.icon.is_none());
        assert!(pos.event_slug.is_none());
        assert!(pos.end_date.is_none());
        assert!(pos.negative_risk);
    }

    #[test]
    fn deserialize_closed_position_from_json() {
        let json = r#"{
            "proxyWallet": "0xdef456",
            "asset": "token_closed",
            "conditionId": "cond_closed",
            "avgPrice": 0.45,
            "totalBought": 200.0,
            "realizedPnl": -10.0,
            "curPrice": 0.35,
            "timestamp": 1700000000,
            "title": "Closed market?",
            "slug": "closed-market",
            "icon": null,
            "eventSlug": "closed-event",
            "outcome": "No",
            "outcomeIndex": 1,
            "oppositeOutcome": "Yes",
            "oppositeAsset": "token_opp",
            "endDate": "2024-06-30"
        }"#;

        let closed: ClosedPosition = serde_json::from_str(json).unwrap();
        assert_eq!(closed.proxy_wallet, "0xdef456");
        assert!((closed.avg_price - 0.45).abs() < f64::EPSILON);
        assert!((closed.realized_pnl - (-10.0)).abs() < f64::EPSILON);
        assert_eq!(closed.timestamp, 1700000000);
        assert_eq!(closed.outcome, "No");
        assert_eq!(closed.outcome_index, 1);
        assert!(closed.icon.is_none());
        assert_eq!(closed.event_slug, Some("closed-event".to_string()));
    }

    #[test]
    fn deserialize_trade_from_json() {
        let json = r#"{
            "proxyWallet": "0x1234",
            "side": "BUY",
            "asset": "token_buy",
            "conditionId": "cond_trade",
            "size": 50.0,
            "price": 0.72,
            "timestamp": 1700001000,
            "title": "Trade market?",
            "slug": "trade-market",
            "icon": "https://example.com/trade.png",
            "eventSlug": null,
            "outcome": "Yes",
            "outcomeIndex": 0,
            "name": "TraderOne",
            "pseudonym": "t1",
            "bio": "A trader",
            "profileImage": null,
            "profileImageOptimized": null,
            "transactionHash": "0xhash123"
        }"#;

        let trade: Trade = serde_json::from_str(json).unwrap();
        assert_eq!(trade.proxy_wallet, "0x1234");
        assert_eq!(trade.side, TradeSide::Buy);
        assert!((trade.size - 50.0).abs() < f64::EPSILON);
        assert!((trade.price - 0.72).abs() < f64::EPSILON);
        assert_eq!(trade.timestamp, 1700001000);
        assert_eq!(trade.name, Some("TraderOne".to_string()));
        assert_eq!(trade.transaction_hash, Some("0xhash123".to_string()));
        assert!(trade.profile_image.is_none());
    }

    #[test]
    fn deserialize_trade_sell_side() {
        let json = r#"{
            "proxyWallet": "0x5678",
            "side": "SELL",
            "asset": "token_sell",
            "conditionId": "cond_sell",
            "size": 25.0,
            "price": 0.30,
            "timestamp": 1700002000,
            "title": "Sell test",
            "slug": "sell-test",
            "icon": null,
            "eventSlug": null,
            "outcome": "No",
            "outcomeIndex": 1,
            "name": null,
            "pseudonym": null,
            "bio": null,
            "profileImage": null,
            "profileImageOptimized": null,
            "transactionHash": null
        }"#;

        let trade: Trade = serde_json::from_str(json).unwrap();
        assert_eq!(trade.side, TradeSide::Sell);
        assert!(trade.name.is_none());
        assert!(trade.transaction_hash.is_none());
    }

    #[test]
    fn deserialize_activity_from_json() {
        let json = r#"{
            "proxyWallet": "0xact123",
            "timestamp": 1700003000,
            "conditionId": "cond_act",
            "type": "TRADE",
            "size": 10.0,
            "usdcSize": 7.50,
            "transactionHash": "0xacthash",
            "price": 0.75,
            "asset": "token_act",
            "side": "BUY",
            "outcomeIndex": 0,
            "title": "Activity market",
            "slug": "activity-market",
            "icon": null,
            "outcome": "Yes",
            "name": null,
            "pseudonym": null,
            "bio": null,
            "profileImage": null,
            "profileImageOptimized": null
        }"#;

        let activity: Activity = serde_json::from_str(json).unwrap();
        assert_eq!(activity.proxy_wallet, "0xact123");
        assert_eq!(activity.activity_type, ActivityType::Trade);
        assert!((activity.size - 10.0).abs() < f64::EPSILON);
        assert!((activity.usdc_size - 7.50).abs() < f64::EPSILON);
        assert_eq!(activity.side, Some("BUY".to_string()));
        assert_eq!(activity.outcome_index, Some(0));
    }

    #[test]
    fn deserialize_activity_merge_type() {
        let json = r#"{
            "proxyWallet": "0xmerge",
            "timestamp": 1700004000,
            "conditionId": "cond_merge",
            "type": "MERGE",
            "size": 5.0,
            "usdcSize": 3.0,
            "transactionHash": null,
            "price": null,
            "asset": null,
            "side": "",
            "outcomeIndex": null,
            "title": null,
            "slug": null,
            "icon": null,
            "outcome": null,
            "name": null,
            "pseudonym": null,
            "bio": null,
            "profileImage": null,
            "profileImageOptimized": null
        }"#;

        let activity: Activity = serde_json::from_str(json).unwrap();
        assert_eq!(activity.activity_type, ActivityType::Merge);
        // Side is an empty string from the API, stored as Some("")
        assert_eq!(activity.side, Some("".to_string()));
        assert!(activity.price.is_none());
        assert!(activity.asset.is_none());
        assert!(activity.title.is_none());
    }

    #[test]
    fn deserialize_user_value() {
        let json = r#"{"user": "0xuser", "value": 1234.56}"#;
        let uv: UserValue = serde_json::from_str(json).unwrap();
        assert_eq!(uv.user, "0xuser");
        assert!((uv.value - 1234.56).abs() < f64::EPSILON);
    }

    #[test]
    fn deserialize_open_interest() {
        let json = r#"{"market": "0xcond", "value": 50000.0}"#;
        let oi: OpenInterest = serde_json::from_str(json).unwrap();
        assert_eq!(oi.market, "0xcond");
        assert!((oi.value - 50000.0).abs() < f64::EPSILON);
    }

    #[test]
    fn market_position_status_display_matches_serde() {
        for variant in [
            MarketPositionStatus::Open,
            MarketPositionStatus::Closed,
            MarketPositionStatus::All,
        ] {
            let serialized = serde_json::to_value(variant).unwrap();
            assert_eq!(format!("\"{}\"", variant), serialized.to_string());
        }
    }

    #[test]
    fn market_position_status_default_is_all() {
        assert_eq!(MarketPositionStatus::default(), MarketPositionStatus::All);
    }

    #[test]
    fn market_position_sort_by_display_matches_serde() {
        for variant in [
            MarketPositionSortBy::Tokens,
            MarketPositionSortBy::CashPnl,
            MarketPositionSortBy::RealizedPnl,
            MarketPositionSortBy::TotalPnl,
        ] {
            let serialized = serde_json::to_value(variant).unwrap();
            assert_eq!(format!("\"{}\"", variant), serialized.to_string());
        }
    }

    #[test]
    fn market_position_sort_by_default_is_total_pnl() {
        assert_eq!(
            MarketPositionSortBy::default(),
            MarketPositionSortBy::TotalPnl
        );
    }

    #[test]
    fn deserialize_market_position_v1() {
        // Field names lifted from `MarketPositionV1` in docs/specs/data/openapi.yaml.
        let json = r#"{
            "proxyWallet": "0xabc",
            "name": "Alice",
            "profileImage": "https://example.com/a.png",
            "verified": true,
            "asset": "token_a",
            "conditionId": "cond_mp",
            "avgPrice": 0.42,
            "size": 1234.5,
            "currPrice": 0.51,
            "currentValue": 629.60,
            "cashPnl": 110.0,
            "totalBought": 520.0,
            "realizedPnl": 15.5,
            "totalPnl": 125.5,
            "outcome": "Yes",
            "outcomeIndex": 0
        }"#;

        let pos: MarketPositionV1 = serde_json::from_str(json).unwrap();
        assert_eq!(pos.proxy_wallet, "0xabc");
        assert_eq!(pos.name, "Alice");
        assert_eq!(
            pos.profile_image.as_deref(),
            Some("https://example.com/a.png")
        );
        assert!(pos.verified);
        assert_eq!(pos.asset, "token_a");
        assert_eq!(pos.condition_id, "cond_mp");
        assert!((pos.avg_price - 0.42).abs() < f64::EPSILON);
        assert!((pos.size - 1234.5).abs() < f64::EPSILON);
        assert!((pos.curr_price - 0.51).abs() < f64::EPSILON);
        assert!((pos.current_value - 629.60).abs() < f64::EPSILON);
        assert!((pos.cash_pnl - 110.0).abs() < f64::EPSILON);
        assert!((pos.total_bought - 520.0).abs() < f64::EPSILON);
        assert!((pos.realized_pnl - 15.5).abs() < f64::EPSILON);
        assert!((pos.total_pnl - 125.5).abs() < f64::EPSILON);
        assert_eq!(pos.outcome, "Yes");
        assert_eq!(pos.outcome_index, 0);
    }

    #[test]
    fn market_position_v1_roundtrip() {
        let original = MarketPositionV1 {
            proxy_wallet: "0xabc".into(),
            name: "Alice".into(),
            profile_image: None,
            verified: false,
            asset: "token_a".into(),
            condition_id: "cond_mp".into(),
            avg_price: 0.5,
            size: 10.0,
            curr_price: 0.6,
            current_value: 6.0,
            cash_pnl: 1.0,
            total_bought: 5.0,
            realized_pnl: 0.0,
            total_pnl: 1.0,
            outcome: "No".into(),
            outcome_index: 1,
        };
        let json = serde_json::to_string(&original).unwrap();
        // Ensure currPrice is used over snake_case in the wire format.
        assert!(json.contains("\"currPrice\""));
        let back: MarketPositionV1 = serde_json::from_str(&json).unwrap();
        assert_eq!(back.proxy_wallet, original.proxy_wallet);
        assert_eq!(back.outcome_index, original.outcome_index);
        assert!((back.curr_price - original.curr_price).abs() < f64::EPSILON);
    }

    #[test]
    fn deserialize_meta_market_position_v1() {
        let json = r#"{
            "token": "token_a",
            "positions": [
                {
                    "proxyWallet": "0xabc",
                    "name": "Alice",
                    "profileImage": null,
                    "verified": false,
                    "asset": "token_a",
                    "conditionId": "cond_mp",
                    "avgPrice": 0.42,
                    "size": 100.0,
                    "currPrice": 0.51,
                    "currentValue": 51.0,
                    "cashPnl": 9.0,
                    "totalBought": 42.0,
                    "realizedPnl": 0.0,
                    "totalPnl": 9.0,
                    "outcome": "Yes",
                    "outcomeIndex": 0
                }
            ]
        }"#;

        let meta: MetaMarketPositionV1 = serde_json::from_str(json).unwrap();
        assert_eq!(meta.token, "token_a");
        assert_eq!(meta.positions.len(), 1);
        assert_eq!(meta.positions[0].name, "Alice");
        assert!(meta.positions[0].profile_image.is_none());
    }
}