cabalspy 0.3.0

KOL and smart money wallet tracking API for Solana, Base, BNB Chain, Ethereum and Robinhood Chain. Realtime KOL trades, whale wallets, copy-trading signals, bundle detection, token holders and PnL.
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
//! Chains, wallet types and response models.
//!
//! Every response field is optional and defaulted. The API returns `null` in many
//! places, and adds fields over time; deserialization must never fail because of
//! that. Anything not modelled here is kept in the `extra` map on the larger
//! structs, so nothing is silently dropped.
//!
//! Two conventions worth knowing:
//!
//! 1. Timestamps inside `data` are not RFC 3339. They arrive as
//!    `"YYYY-MM-DD HH:MM:SS"` with no offset, and are UTC. Only `meta.timestamp`
//!    carries a `Z`. They are kept as `String` here so no timezone assumption is
//!    baked in; see [`crate::parse_api_timestamp`].
//! 2. `realized_pnl` is `total_sell - total_buy`. A wallet that bought and has
//!    not sold reports its whole investment as a loss and `-100` percent. Check
//!    `still_holding` before showing it to a user.

use std::collections::HashMap;
use std::fmt;

use serde::{Deserialize, Serialize};

// ── enums ────────────────────────────────────────────────────────────────────

/// A supported blockchain.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Chain {
    Solana,
    Bnb,
    Base,
    Eth,
    /// Robinhood Chain, Robinhood's Ethereum L2 on the Arbitrum Orbit stack.
    Rh,
}

/// Every chain this crate knows about.
pub const CHAINS: [Chain; 5] = [Chain::Solana, Chain::Bnb, Chain::Base, Chain::Eth, Chain::Rh];

impl Chain {
    pub fn as_str(self) -> &'static str {
        match self {
            Chain::Solana => "solana",
            Chain::Bnb => "bnb",
            Chain::Base => "base",
            Chain::Eth => "eth",
            Chain::Rh => "rh",
        }
    }

    /// The wallet types this chain actually has.
    pub fn wallet_types(self) -> &'static [WalletType] {
        match self {
            Chain::Solana => &[WalletType::Kol, WalletType::Smart, WalletType::Whale],
            Chain::Bnb | Chain::Base | Chain::Rh => &[WalletType::Kol, WalletType::Smart],
            Chain::Eth => &[WalletType::Kol],
        }
    }

    /// True when this chain has that wallet type.
    pub fn supports(self, wallet_type: WalletType) -> bool {
        self.wallet_types().contains(&wallet_type)
    }

    /// The chain's native currency.
    pub fn currency(self) -> &'static str {
        match self {
            Chain::Solana => "SOL",
            Chain::Bnb => "BNB",
            Chain::Base | Chain::Eth | Chain::Rh => "ETH",
        }
    }

    /// Whether the REST API returns market cap, price and unrealized PNL here.
    ///
    /// On every other chain those REST fields are `null`. The websocket gateway
    /// is different: it populates them on all chains.
    pub fn has_rest_marketcap(self) -> bool {
        matches!(self, Chain::Solana)
    }
}

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

/// The kind of wallet a label describes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WalletType {
    /// Key Opinion Leader: a trader whose calls move markets.
    Kol,
    /// Smart money: identified by track record rather than reach.
    Smart,
    /// Whale. Solana only.
    Whale,
}

impl WalletType {
    pub fn as_str(self) -> &'static str {
        match self {
            WalletType::Kol => "kol",
            WalletType::Smart => "smart",
            WalletType::Whale => "whale",
        }
    }
}

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

/// A statistics window.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Period {
    #[serde(rename = "6h")]
    H6,
    #[serde(rename = "1d")]
    D1,
    #[serde(rename = "7d")]
    D7,
    #[serde(rename = "30d")]
    D30,
}

impl Period {
    pub fn as_str(self) -> &'static str {
        match self {
            Period::H6 => "6h",
            Period::D1 => "1d",
            Period::D7 => "7d",
            Period::D30 => "30d",
        }
    }
}

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

/// Which kind of signal to return.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SignalMode {
    Cluster,
    Entry,
    Exit,
}

impl SignalMode {
    pub fn as_str(self) -> &'static str {
        match self {
            SignalMode::Cluster => "cluster",
            SignalMode::Entry => "entry",
            SignalMode::Exit => "exit",
        }
    }
}

/// Which analytics view to return.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AnalyticsMode {
    VolumeTrend,
    MostTraded,
    WinRate,
    TopPerformers,
}

impl AnalyticsMode {
    pub fn as_str(self) -> &'static str {
        match self {
            AnalyticsMode::VolumeTrend => "volume_trend",
            AnalyticsMode::MostTraded => "most_traded",
            AnalyticsMode::WinRate => "win_rate",
            AnalyticsMode::TopPerformers => "top_performers",
        }
    }
}

/// A tier of the curated calls. `Big` and `Whale` exist on Solana only.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CallTier {
    /// "Gambles"
    Gamble,
    /// "Moderate Gamble"
    Moderate,
    /// "Good Token"
    Good,
    /// "Big Token"
    Big,
    /// "Whales Call"
    Whale,
}

impl CallTier {
    pub fn as_str(self) -> &'static str {
        match self {
            CallTier::Gamble => "gamble",
            CallTier::Moderate => "moderate",
            CallTier::Good => "good",
            CallTier::Big => "big",
            CallTier::Whale => "whale",
        }
    }
}

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

/// The window of [`crate::Calls::stats`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CallStatsPeriod {
    #[serde(rename = "1d")]
    D1,
    #[serde(rename = "7d")]
    D7,
    #[serde(rename = "30d")]
    D30,
    #[serde(rename = "90d")]
    D90,
}

impl CallStatsPeriod {
    pub fn as_str(self) -> &'static str {
        match self {
            CallStatsPeriod::D1 => "1d",
            CallStatsPeriod::D7 => "7d",
            CallStatsPeriod::D30 => "30d",
            CallStatsPeriod::D90 => "90d",
        }
    }
}

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

/// Reads `null` as the type's default instead of failing.
fn null_as_default<'de, D, T>(deserializer: D) -> std::result::Result<T, D::Error>
where
    D: serde::Deserializer<'de>,
    T: Default + Deserialize<'de>,
{
    Ok(Option::<T>::deserialize(deserializer)?.unwrap_or_default())
}

// ── envelope ─────────────────────────────────────────────────────────────────

/// Metadata attached to every successful response.
#[derive(Debug, Clone, Deserialize, Default)]
pub struct ResponseMeta {
    #[serde(default)]
    pub request_id: String,
    #[serde(default)]
    pub cached: bool,
    #[serde(default)]
    pub cache_age_seconds: i64,
    #[serde(default)]
    pub version: String,
    /// RFC 3339 with a `Z` suffix, unlike the timestamps inside `data`.
    #[serde(default)]
    pub timestamp: String,
}

/// Cursor pagination block.
#[derive(Debug, Clone, Deserialize, Default)]
pub struct Pagination {
    #[serde(default)]
    pub limit: i64,
    /// 0 when the server reports no total (`null`, for example `/v1/calls` with a tier filter).
    #[serde(default, deserialize_with = "null_as_default")]
    pub total: i64,
    #[serde(default)]
    pub has_more: bool,
    #[serde(default)]
    pub next_cursor: Option<String>,
}

/// A full response: the payload plus pagination, meta and rate limit headers.
#[derive(Debug, Clone)]
pub struct Envelope<T> {
    pub data: T,
    pub pagination: Option<Pagination>,
    pub meta: ResponseMeta,
    pub rate_limit: crate::error::RateLimit,
    pub status: u16,
    /// Set when the request ran on the public demo key. See [`DemoInfo`].
    pub demo: Option<DemoInfo>,
}

/// The top-level `demo` object the API adds to every response made with the
/// public demo key.
///
/// Demo data is delayed by 15 minutes and capped at 5 rows per list, and the
/// key allows 20 requests per IP per UTC day. `remaining_today` tells you how
/// many are left; the `X-Demo-Remaining` header fills it in if the body lacks it.
#[derive(Debug, Clone, Deserialize, Default)]
pub struct DemoInfo {
    #[serde(default)]
    pub notice: Option<String>,
    #[serde(default)]
    pub remaining_today: Option<u64>,
    #[serde(default)]
    pub upgrade: Option<DemoUpgrade>,
}

/// Where to go once the demo is no longer enough.
#[derive(Debug, Clone, Deserialize, Default)]
pub struct DemoUpgrade {
    /// Free test key with 1,000 realtime requests per month.
    #[serde(default)]
    pub test_key: Option<String>,
    /// Pay per call with x402, no account needed.
    #[serde(default)]
    pub pay_per_call: Option<String>,
    #[serde(default)]
    pub docs: Option<String>,
}

#[derive(Debug, Deserialize)]
pub(crate) struct RawEnvelope<T> {
    pub data: T,
    #[serde(default)]
    pub pagination: Option<Pagination>,
    #[serde(default)]
    pub meta: ResponseMeta,
    /// Kept loose: error bodies send `"demo": true`, success bodies an object.
    #[serde(default)]
    pub demo: Option<serde_json::Value>,
}

#[derive(Debug, Deserialize)]
pub(crate) struct RawError {
    #[serde(default)]
    pub error: crate::error::ApiErrorBody,
    /// Sent alongside `demo_limit_reached`.
    #[serde(default)]
    pub upgrade: Option<serde_json::Value>,
}

// ── shared models ────────────────────────────────────────────────────────────

/// A labeled wallet's public identity.
#[derive(Debug, Clone, Deserialize, Default)]
pub struct WalletProfile {
    #[serde(default)]
    pub wallet_address: Option<String>,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub image_url: Option<String>,
    #[serde(default)]
    pub twitter: Option<String>,
    #[serde(default)]
    pub telegram: Option<String>,
    #[serde(default)]
    pub copytrade_link: Option<String>,
    #[serde(default, rename = "type")]
    pub wallet_type: Option<WalletType>,
    #[serde(default)]
    pub blockchain: Option<Chain>,
    #[serde(default)]
    pub currency: Option<String>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Token block as returned by the token endpoints.
///
/// `market_cap_usd`, `price_usd` and `sol_price_usd` are only populated where the
/// endpoint passes the native price into its market cap builder. `tokens/holders`
/// does; `tokens/stats` and `tokens/transactions` do not, so those come back
/// `None` there even on Solana.
#[derive(Debug, Clone, Deserialize, Default)]
pub struct TokenBlock {
    #[serde(default)]
    pub mint: Option<String>,
    #[serde(default)]
    pub token_name: Option<String>,
    #[serde(default)]
    pub token_supply: Option<f64>,
    #[serde(default)]
    pub token_decimals: Option<i64>,
    #[serde(default)]
    pub blockchain: Option<Chain>,
    #[serde(default)]
    pub currency: Option<String>,
    #[serde(default)]
    pub market_cap: Option<f64>,
    #[serde(default)]
    pub market_cap_usd: Option<f64>,
    #[serde(default)]
    pub market_cap_currency: Option<String>,
    #[serde(default)]
    pub price: Option<f64>,
    #[serde(default)]
    pub price_usd: Option<f64>,
    #[serde(default)]
    pub sol_price_usd: Option<f64>,
    #[serde(default)]
    pub pool: Option<String>,
    #[serde(default)]
    pub on_curve: Option<bool>,
    #[serde(default)]
    pub bonding_curve_progress: Option<f64>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

// ── wallets/tracker ──────────────────────────────────────────────────────────

#[derive(Debug, Clone, Deserialize, Default)]
pub struct PeriodStats {
    #[serde(default)]
    pub period: Option<String>,
    #[serde(default)]
    pub buy_txn: Option<i64>,
    #[serde(default)]
    pub sell_txn: Option<i64>,
    #[serde(default)]
    pub total_buy: Option<f64>,
    #[serde(default)]
    pub total_buy_usd: Option<f64>,
    #[serde(default)]
    pub total_sell: Option<f64>,
    #[serde(default)]
    pub total_sell_usd: Option<f64>,
    #[serde(default)]
    pub volume: Option<f64>,
    #[serde(default)]
    pub volume_usd: Option<f64>,
    /// `total_sell - total_buy`. See the note at the top of this module.
    #[serde(default)]
    pub realized_pnl: Option<f64>,
    #[serde(default)]
    pub realized_pnl_usd: Option<f64>,
    #[serde(default)]
    pub realized_pnl_percentage: Option<f64>,
    #[serde(default)]
    pub win_count: Option<i64>,
    #[serde(default)]
    pub loss_count: Option<i64>,
    #[serde(default)]
    pub best_trade_pnl: Option<f64>,
    #[serde(default)]
    pub worst_trade_pnl: Option<f64>,
    #[serde(default)]
    pub largest_buy: Option<f64>,
    #[serde(default)]
    pub avg_hold_time_minutes: Option<f64>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct WinRateDistribution {
    #[serde(default)]
    pub below_zero: Option<i64>,
    #[serde(default)]
    pub above_zero_to_100: Option<i64>,
    #[serde(default)]
    pub above_100_to_500: Option<i64>,
    #[serde(default)]
    pub above_500: Option<i64>,
    #[serde(default)]
    pub closed_count: Option<i64>,
    #[serde(default)]
    pub win_count: Option<i64>,
    #[serde(default)]
    pub win_rate_percentage: Option<f64>,
}

/// A summary object, not a list, despite the plural name on the wire.
#[derive(Debug, Clone, Deserialize, Default)]
pub struct ActiveTokensSummary {
    #[serde(default)]
    pub active_tokens_count: Option<i64>,
    #[serde(default)]
    pub still_holding_count: Option<i64>,
    #[serde(default)]
    pub avg_position_size: Option<f64>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct WalletTracker {
    #[serde(default)]
    pub wallet: Option<String>,
    #[serde(default)]
    pub profile: WalletProfile,
    #[serde(default)]
    pub period_stats: PeriodStats,
    #[serde(default)]
    pub period_active_tokens: ActiveTokensSummary,
    #[serde(default)]
    pub period_win_rate_distribution: WinRateDistribution,
    #[serde(default)]
    pub period_history_tokens: Vec<serde_json::Value>,
    #[serde(default)]
    pub period_realized_pnl_chart: Vec<serde_json::Value>,
    #[serde(default)]
    pub period_trades: Vec<serde_json::Value>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

// ── tokens/stats ─────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Deserialize, Default)]
pub struct TotalHolders {
    #[serde(default)]
    pub kol_count: Option<i64>,
    #[serde(default)]
    pub smart_count: Option<i64>,
    #[serde(default)]
    pub whale_count: Option<i64>,
    #[serde(default)]
    pub still_holding_count: Option<i64>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct TotalHoldings {
    #[serde(default)]
    pub token_amount: Option<f64>,
    #[serde(default)]
    pub token_amount_peak: Option<f64>,
    #[serde(default)]
    pub supply_pct: Option<f64>,
    #[serde(default)]
    pub supply_pct_peak: Option<f64>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct TraderHoldings {
    #[serde(default)]
    pub token_amount: Option<f64>,
    #[serde(default)]
    pub token_amount_peak: Option<f64>,
    #[serde(default)]
    pub supply_pct: Option<f64>,
    #[serde(default)]
    pub supply_pct_peak: Option<f64>,
    #[serde(default)]
    pub bag_pct: Option<f64>,
    #[serde(default)]
    pub still_holding: Option<bool>,
    /// Frozen at the first buy and never recomputed.
    #[serde(default)]
    pub entry_market_cap: Option<f64>,
    #[serde(default)]
    pub entry_market_cap_usd: Option<f64>,
    #[serde(default)]
    pub unrealized_pnl_sol: Option<f64>,
    #[serde(default)]
    pub unrealized_pnl_usd: Option<f64>,
    #[serde(default)]
    pub unrealized_pnl_pct: Option<f64>,
    #[serde(default)]
    pub remaining_sol: Option<f64>,
    #[serde(default)]
    pub remaining_usd: Option<f64>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct TraderStats {
    #[serde(default)]
    pub buy: Option<f64>,
    #[serde(default)]
    pub buy_usd: Option<f64>,
    #[serde(default)]
    pub buy_count: Option<i64>,
    #[serde(default)]
    pub buy_tokens: Option<f64>,
    #[serde(default)]
    pub sell: Option<f64>,
    #[serde(default)]
    pub sell_usd: Option<f64>,
    #[serde(default)]
    pub sell_count: Option<i64>,
    #[serde(default)]
    pub sell_tokens: Option<f64>,
    #[serde(default)]
    pub avg_buy_price: Option<f64>,
    /// `total_sell - total_buy`. See the note at the top of this module.
    #[serde(default)]
    pub realized_pnl: Option<f64>,
    #[serde(default)]
    pub realized_pnl_usd: Option<f64>,
    #[serde(default)]
    pub realized_pnl_percentage: Option<f64>,
    /// `"YYYY-MM-DD HH:MM:SS"`, UTC without a marker.
    #[serde(default)]
    pub first_trade_at: Option<String>,
    #[serde(default)]
    pub last_trade_at: Option<String>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct TokenTrader {
    #[serde(default)]
    pub profile: WalletProfile,
    #[serde(default)]
    pub trader_holdings: TraderHoldings,
    #[serde(default)]
    pub trader_stats: TraderStats,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct TokenStats {
    #[serde(default)]
    pub token: TokenBlock,
    #[serde(default)]
    pub total_holders: TotalHolders,
    #[serde(default)]
    pub total_holdings: TotalHoldings,
    #[serde(default)]
    pub total_statistics: HashMap<String, serde_json::Value>,
    #[serde(default)]
    pub traders: Vec<TokenTrader>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

// ── signals ──────────────────────────────────────────────────────────────────

/// Token block on the signals endpoint. Carries no market cap at all.
#[derive(Debug, Clone, Deserialize, Default)]
pub struct SignalTokenBlock {
    #[serde(default)]
    pub mint: Option<String>,
    #[serde(default)]
    pub token_name: Option<String>,
    #[serde(default)]
    pub token_supply: Option<f64>,
    #[serde(default)]
    pub token_decimals: Option<i64>,
    #[serde(default)]
    pub blockchain: Option<Chain>,
    #[serde(default)]
    pub currency: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct SignalWallet {
    #[serde(default)]
    pub profile: WalletProfile,
    #[serde(default)]
    pub bought_at: Option<String>,
    #[serde(default)]
    pub is_active: Option<bool>,
    #[serde(default)]
    pub still_holding: Option<bool>,
    #[serde(default)]
    pub buy_txn: Option<i64>,
    #[serde(default)]
    pub sell_txn: Option<i64>,
    #[serde(default)]
    pub held_tokens: Option<f64>,
    #[serde(default)]
    pub held_tokens_peak: Option<f64>,
    #[serde(default)]
    pub bag_pct: Option<f64>,
    #[serde(default)]
    pub supply_pct: Option<f64>,
    #[serde(default)]
    pub total_buy: Option<f64>,
    #[serde(default)]
    pub total_buy_usd: Option<f64>,
    #[serde(default)]
    pub total_sell: Option<f64>,
    #[serde(default)]
    pub total_sell_usd: Option<f64>,
    /// `total_sell - total_buy`. See the note at the top of this module.
    #[serde(default)]
    pub realized_pnl: Option<f64>,
    #[serde(default)]
    pub realized_pnl_usd: Option<f64>,
    #[serde(default)]
    pub realized_pnl_percentage: Option<f64>,
    #[serde(default)]
    pub window_buy_txn: Option<i64>,
    #[serde(default)]
    pub window_invested: Option<f64>,
    #[serde(default)]
    pub window_invested_usd: Option<f64>,
    #[serde(default)]
    pub window_tokens_bought: Option<f64>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct SignalWindow {
    #[serde(default)]
    pub hours: Option<f64>,
    #[serde(default)]
    pub wallet_count: Option<i64>,
    #[serde(default)]
    pub first_buy_at: Option<String>,
    #[serde(default)]
    pub latest_buy_at: Option<String>,
    #[serde(default)]
    pub time_span_minutes: Option<f64>,
    #[serde(default)]
    pub total_invested: Option<f64>,
    #[serde(default)]
    pub total_invested_usd: Option<f64>,
    #[serde(default)]
    pub avg_invested: Option<f64>,
    #[serde(default)]
    pub avg_invested_usd: Option<f64>,
    #[serde(default)]
    pub total_tokens_bought: Option<f64>,
    #[serde(default)]
    pub supply_pct_bought: Option<f64>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct Signal {
    #[serde(default)]
    pub signal_type: Option<String>,
    #[serde(default)]
    pub signal_strength: Option<String>,
    #[serde(default)]
    pub currency: Option<String>,
    #[serde(default)]
    pub token: SignalTokenBlock,
    #[serde(default)]
    pub token_stats: HashMap<String, serde_json::Value>,
    #[serde(default)]
    pub wallets: Vec<SignalWallet>,
    #[serde(default)]
    pub window: SignalWindow,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct SignalsResponse {
    #[serde(default)]
    pub blockchain: Option<Chain>,
    #[serde(default, rename = "type")]
    pub wallet_type: Option<WalletType>,
    #[serde(default)]
    pub currency: Option<String>,
    #[serde(default)]
    pub mode: Option<String>,
    #[serde(default)]
    pub signals: Vec<Signal>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

// ── feed ─────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Deserialize, Default)]
pub struct HoldingsAfter {
    #[serde(default)]
    pub token_amount: Option<f64>,
    #[serde(default)]
    pub token_amount_peak: Option<f64>,
    #[serde(default)]
    pub supply_pct: Option<f64>,
    #[serde(default)]
    pub supply_pct_peak: Option<f64>,
    #[serde(default)]
    pub bag_pct: Option<f64>,
    #[serde(default)]
    pub still_holding: Option<bool>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct FeedTransaction {
    #[serde(default)]
    pub tx_signature: Option<String>,
    #[serde(default)]
    pub mint: Option<String>,
    #[serde(default)]
    pub token_name: Option<String>,
    #[serde(default)]
    pub transaction_type: Option<String>,
    #[serde(default)]
    pub value: Option<f64>,
    #[serde(default)]
    pub value_usd: Option<f64>,
    #[serde(default)]
    pub currency: Option<String>,
    #[serde(default)]
    pub token_amount: Option<f64>,
    #[serde(default)]
    pub price_per_token: Option<f64>,
    #[serde(default)]
    pub price_per_token_usd: Option<f64>,
    /// `"YYYY-MM-DD HH:MM:SS"`, UTC without a marker.
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub profile: WalletProfile,
    #[serde(default)]
    pub holdings_after: HoldingsAfter,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct TransactionsList {
    #[serde(default)]
    pub blockchain: Option<Chain>,
    #[serde(default, rename = "type")]
    pub wallet_type: Option<WalletType>,
    #[serde(default)]
    pub mode: Option<String>,
    #[serde(default)]
    pub currency: Option<String>,
    #[serde(default)]
    pub count: Option<i64>,
    #[serde(default)]
    pub transactions: Vec<FeedTransaction>,
    #[serde(default)]
    pub time_window_seconds: Option<i64>,
    /// Present when a requested limit or time window was capped.
    #[serde(default)]
    pub warnings: Vec<String>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct CountResponse {
    #[serde(default)]
    pub blockchain: Option<Chain>,
    #[serde(default, rename = "type")]
    pub wallet_type: Option<WalletType>,
    #[serde(default)]
    pub count: Option<i64>,
    #[serde(default)]
    pub wallet_count: Option<i64>,
    #[serde(default)]
    pub time_window_seconds: Option<i64>,
    #[serde(default)]
    pub mint: Option<String>,
    #[serde(default)]
    pub warnings: Vec<String>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct VolumeResponse {
    #[serde(default)]
    pub blockchain: Option<Chain>,
    #[serde(default, rename = "type")]
    pub wallet_type: Option<WalletType>,
    #[serde(default)]
    pub volume: Option<f64>,
    #[serde(default)]
    pub volume_usd: Option<f64>,
    #[serde(default)]
    pub currency: Option<String>,
    #[serde(default)]
    pub time_window_seconds: Option<i64>,
    #[serde(default)]
    pub mint: Option<String>,
    #[serde(default)]
    pub warnings: Vec<String>,
}

// ── bundle ───────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Deserialize, Default)]
pub struct BundlePosition {
    #[serde(default)]
    pub held: Option<f64>,
    #[serde(default)]
    pub peak: Option<f64>,
    #[serde(default)]
    pub bag_pct: Option<f64>,
    #[serde(default)]
    pub supply_pct: Option<f64>,
    #[serde(default)]
    pub invested: Option<f64>,
    #[serde(default)]
    pub invested_usd: Option<f64>,
    #[serde(default)]
    pub buy_txn: Option<i64>,
    #[serde(default)]
    pub sell_txn: Option<i64>,
    #[serde(default)]
    pub sold_value: Option<f64>,
    #[serde(default)]
    pub realized_pnl_sol: Option<f64>,
    #[serde(default)]
    pub realized_pnl_usd: Option<f64>,
    #[serde(default)]
    pub unrealized_pnl_sol: Option<f64>,
    #[serde(default)]
    pub unrealized_pnl_usd: Option<f64>,
    #[serde(default)]
    pub unrealized_pnl_pct: Option<f64>,
    #[serde(default)]
    pub remaining_sol: Option<f64>,
    #[serde(default)]
    pub remaining_usd: Option<f64>,
    #[serde(default)]
    pub entry_market_cap: Option<f64>,
    #[serde(default)]
    pub entry_market_cap_usd: Option<f64>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct BundleWallet {
    #[serde(default)]
    pub address: Option<String>,
    #[serde(default)]
    pub is_kol: Option<bool>,
    #[serde(default)]
    pub transaction_type: Option<String>,
    #[serde(default)]
    pub signature: Option<String>,
    #[serde(default)]
    pub entry_source: Option<String>,
    #[serde(default)]
    pub profile: WalletProfile,
    #[serde(default)]
    pub position: BundlePosition,
    /// Side wallet evidence. Absent on the KOL wallet itself.
    #[serde(default)]
    pub fee_lamports: Option<i64>,
    #[serde(default)]
    pub block_index: Option<i64>,
    #[serde(default)]
    pub adjacent_to_kol: Option<bool>,
    #[serde(default)]
    pub same_fee: Option<bool>,
    #[serde(default)]
    pub occurrences: Option<i64>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct BundleEntry {
    #[serde(default)]
    pub bundle_id: Option<String>,
    #[serde(default)]
    pub kol_wallet: Option<String>,
    #[serde(default)]
    pub kol_profile: Option<WalletProfile>,
    #[serde(default)]
    pub confidence: Option<f64>,
    #[serde(default)]
    pub jito_confirmed: Option<bool>,
    #[serde(default)]
    pub proof_type: Option<String>,
    #[serde(default)]
    pub slot: Option<i64>,
    #[serde(default)]
    pub wallet_count: Option<i64>,
    #[serde(default)]
    pub detected_at: Option<String>,
    #[serde(default)]
    pub bundle_wallets: Vec<BundleWallet>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct BundleResponse {
    #[serde(default)]
    pub blockchain: Option<Chain>,
    #[serde(default)]
    pub token: TokenBlock,
    #[serde(default)]
    pub bundles: Vec<BundleEntry>,
}

// ── calls ────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Deserialize, Default)]
pub struct CallToken {
    #[serde(default)]
    pub mint: String,
    #[serde(default)]
    pub symbol: Option<String>,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub logo: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct CallWallet {
    #[serde(default)]
    pub address: String,
    /// `kol` or `smart`.
    #[serde(default, rename = "type")]
    pub wallet_type: Option<WalletType>,
    /// Profile name, empty when unknown.
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub amount_native: Option<f64>,
    #[serde(default)]
    pub bought_at: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct CallTrigger {
    #[serde(default)]
    pub kol_count: Option<i64>,
    #[serde(default)]
    pub smart_count: Option<i64>,
    #[serde(default)]
    pub native_symbol: Option<String>,
    #[serde(default)]
    pub total_native: Option<f64>,
    /// Empty on Big Token calls, which carry counts only.
    #[serde(default)]
    pub wallets: Vec<CallWallet>,
}

/// How a call did over the 24 hours after it.
#[derive(Debug, Clone, Deserialize, Default)]
pub struct CallPerformance {
    /// `pending` (younger than about two days), `ok`, `no_data` or `not_evaluated`.
    #[serde(default)]
    pub status: Option<String>,
    /// Highest price within 24 h after the call, divided by the price at the call.
    #[serde(default)]
    pub peak_multiple: Option<f64>,
    #[serde(default)]
    pub peak_market_cap: Option<f64>,
    #[serde(default)]
    pub minutes_to_peak: Option<i64>,
    /// Price 24 h after the call, divided by the price at the call.
    #[serde(default)]
    pub close_24h_multiple: Option<f64>,
    #[serde(default)]
    pub evaluated_at: Option<String>,
}

/// One curated call, the same as posted in the CabalSpy Discord.
#[derive(Debug, Clone, Deserialize, Default)]
pub struct Call {
    /// Stable id, the same in REST and on the call stream.
    #[serde(default)]
    pub call_id: String,
    #[serde(default)]
    pub blockchain: Option<Chain>,
    #[serde(default)]
    pub tier: Option<CallTier>,
    /// As shown in Discord, for example `"Moderate Gamble"`.
    #[serde(default)]
    pub tier_label: Option<String>,
    /// RFC 3339, UTC.
    #[serde(default)]
    pub called_at: Option<String>,
    #[serde(default)]
    pub token: CallToken,
    /// USD.
    #[serde(default)]
    pub market_cap_at_call: Option<f64>,
    #[serde(default)]
    pub trigger: CallTrigger,
    /// Trading and chart links. Solana calls include `terminal`, the CabalSpy Terminal page.
    #[serde(default)]
    pub links: HashMap<String, String>,
    #[serde(default)]
    pub performance: Option<CallPerformance>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// One block of [`CallStatsResponse`]: totals, a chain or a tier.
#[derive(Debug, Clone, Deserialize, Default)]
pub struct CallStatsBlock {
    #[serde(default)]
    pub calls: i64,
    #[serde(default)]
    pub evaluated: i64,
    #[serde(default)]
    pub pending: i64,
    #[serde(default)]
    pub no_data: i64,
    #[serde(default)]
    pub not_evaluated: i64,
    /// Percent of evaluated calls per level (`"2x"`, `"5x"`, `"10x"`), `None` when nothing was evaluated.
    #[serde(default)]
    pub hit_rate: HashMap<String, Option<f64>>,
    #[serde(default)]
    pub median_peak_multiple: Option<f64>,
    #[serde(default)]
    pub median_minutes_to_peak: Option<f64>,
    /// Per tier, only inside `chains`.
    #[serde(default)]
    pub tiers: HashMap<String, CallStatsBlock>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct BestCall {
    #[serde(default)]
    pub call_id: String,
    #[serde(default)]
    pub blockchain: Option<Chain>,
    #[serde(default)]
    pub tier: Option<CallTier>,
    #[serde(default)]
    pub symbol: Option<String>,
    #[serde(default)]
    pub mint: String,
    #[serde(default)]
    pub called_at: Option<String>,
    #[serde(default)]
    pub market_cap_at_call: Option<f64>,
    #[serde(default)]
    pub peak_multiple: Option<f64>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct CallStatsResponse {
    #[serde(default)]
    pub period: Option<String>,
    #[serde(default)]
    pub window: HashMap<String, String>,
    #[serde(default)]
    pub method: Option<String>,
    #[serde(default)]
    pub totals: CallStatsBlock,
    #[serde(default)]
    pub chains: HashMap<String, CallStatsBlock>,
    #[serde(default)]
    pub best_calls: Vec<BestCall>,
}

// ── system ───────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Deserialize, Default)]
pub struct HealthResponse {
    #[serde(default)]
    pub status: Option<String>,
    #[serde(default)]
    pub version: Option<String>,
    #[serde(default)]
    pub components: HashMap<String, serde_json::Value>,
    #[serde(default)]
    pub latency_ms: Option<f64>,
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct MetaResponse {
    #[serde(default)]
    pub chains: Vec<String>,
    #[serde(default)]
    pub wallet_types: Vec<String>,
    #[serde(default)]
    pub periods: Vec<String>,
    #[serde(default)]
    pub currencies: HashMap<String, String>,
    #[serde(default)]
    pub wallet_counts: HashMap<String, serde_json::Value>,
    #[serde(default)]
    pub limits: HashMap<String, i64>,
}