nym-api-requests 1.21.5-rc.2

Nym API request types and functions
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
// Copyright 2025 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0

use crate::helpers::PlaceholderJsonSchemaImpl;
use crate::models::{CoinSchema, DisplayRole};
use crate::pagination::PaginatedResponse;
use cosmwasm_std::{Coin, Decimal};
use nym_contracts_common::{IdentityKey, NaiveFloat};
use nym_crypto::asymmetric::ed25519;
use nym_crypto::asymmetric::ed25519::serde_helpers::bs58_ed25519_pubkey;
use nym_mixnet_contract_common::reward_params::Performance;
use nym_mixnet_contract_common::NodeId;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use time::{Date, OffsetDateTime};
use utoipa::ToSchema;

pub use config_score::*;

pub type StakeSaturation = Decimal;

#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/StakeSaturationResponse.ts"
    )
)]
pub struct StakeSaturationResponse {
    #[cfg_attr(feature = "generate-ts", ts(type = "string"))]
    #[schema(value_type = String)]
    pub saturation: StakeSaturation,

    #[cfg_attr(feature = "generate-ts", ts(type = "string"))]
    #[schema(value_type = String)]
    pub uncapped_saturation: StakeSaturation,
    pub as_at: i64,
}

pub mod config_score {
    use nym_contracts_common::NaiveFloat;
    use serde::{Deserialize, Serialize};
    use std::cmp::Ordering;
    use utoipa::ToSchema;

    #[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
    pub struct ConfigScoreDataResponse {
        pub parameters: ConfigScoreParams,
        pub version_history: Vec<HistoricalNymNodeVersionEntry>,
    }

    #[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema, PartialEq)]
    pub struct HistoricalNymNodeVersionEntry {
        /// The unique, ordered, id of this particular entry
        pub id: u32,

        /// Data associated with this particular version
        pub version_information: HistoricalNymNodeVersion,
    }

    impl PartialOrd for HistoricalNymNodeVersionEntry {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            // we only care about id for the purposes of ordering as they should have unique data
            self.id.partial_cmp(&other.id)
        }
    }

    impl From<nym_mixnet_contract_common::HistoricalNymNodeVersionEntry>
        for HistoricalNymNodeVersionEntry
    {
        fn from(value: nym_mixnet_contract_common::HistoricalNymNodeVersionEntry) -> Self {
            HistoricalNymNodeVersionEntry {
                id: value.id,
                version_information: value.version_information.into(),
            }
        }
    }

    #[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema, PartialEq)]
    pub struct HistoricalNymNodeVersion {
        /// Version of the nym node that is going to be used for determining the version score of a node.
        /// note: value stored here is pre-validated `semver::Version`
        pub semver: String,

        /// Block height of when this version has been added to the contract
        pub introduced_at_height: u64,
        // for now ignore that field. it will give nothing useful to the users
        //     pub difference_since_genesis: TotalVersionDifference,
    }

    impl From<nym_mixnet_contract_common::HistoricalNymNodeVersion> for HistoricalNymNodeVersion {
        fn from(value: nym_mixnet_contract_common::HistoricalNymNodeVersion) -> Self {
            HistoricalNymNodeVersion {
                semver: value.semver,
                introduced_at_height: value.introduced_at_height,
            }
        }
    }

    #[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
    pub struct ConfigScoreParams {
        /// Defines weights for calculating numbers of versions behind the current release.
        pub version_weights: OutdatedVersionWeights,

        /// Defines the parameters of the formula for calculating the version score
        pub version_score_formula_params: VersionScoreFormulaParams,
    }

    /// Defines weights for calculating numbers of versions behind the current release.
    #[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
    pub struct OutdatedVersionWeights {
        pub major: u32,
        pub minor: u32,
        pub patch: u32,
        pub prerelease: u32,
    }

    /// Given the formula of version_score = penalty ^ (versions_behind_factor ^ penalty_scaling)
    /// define the relevant parameters
    #[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
    pub struct VersionScoreFormulaParams {
        pub penalty: f64,
        pub penalty_scaling: f64,
    }

    impl From<nym_mixnet_contract_common::ConfigScoreParams> for ConfigScoreParams {
        fn from(value: nym_mixnet_contract_common::ConfigScoreParams) -> Self {
            ConfigScoreParams {
                version_weights: value.version_weights.into(),
                version_score_formula_params: value.version_score_formula_params.into(),
            }
        }
    }

    impl From<nym_mixnet_contract_common::OutdatedVersionWeights> for OutdatedVersionWeights {
        fn from(value: nym_mixnet_contract_common::OutdatedVersionWeights) -> Self {
            OutdatedVersionWeights {
                major: value.major,
                minor: value.minor,
                patch: value.patch,
                prerelease: value.prerelease,
            }
        }
    }

    impl From<nym_mixnet_contract_common::VersionScoreFormulaParams> for VersionScoreFormulaParams {
        fn from(value: nym_mixnet_contract_common::VersionScoreFormulaParams) -> Self {
            VersionScoreFormulaParams {
                penalty: value.penalty.naive_to_f64(),
                penalty_scaling: value.penalty_scaling.naive_to_f64(),
            }
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
pub struct NodeRefreshBody {
    #[serde(with = "bs58_ed25519_pubkey")]
    #[schemars(with = "String")]
    #[schema(value_type = String)]
    pub node_identity: ed25519::PublicKey,

    // a poor man's nonce
    pub request_timestamp: i64,

    #[schemars(with = "PlaceholderJsonSchemaImpl")]
    #[schema(value_type = String)]
    pub signature: ed25519::Signature,
}

impl NodeRefreshBody {
    pub fn plaintext(node_identity: ed25519::PublicKey, request_timestamp: i64) -> Vec<u8> {
        node_identity
            .to_bytes()
            .into_iter()
            .chain(request_timestamp.to_be_bytes())
            .chain(b"describe-cache-refresh-request".iter().copied())
            .collect()
    }

    pub fn new(private_key: &ed25519::PrivateKey) -> Self {
        let node_identity = private_key.public_key();
        let request_timestamp = OffsetDateTime::now_utc().unix_timestamp();
        let signature = private_key.sign(Self::plaintext(node_identity, request_timestamp));
        NodeRefreshBody {
            node_identity,
            request_timestamp,
            signature,
        }
    }

    pub fn verify_signature(&self) -> bool {
        self.node_identity
            .verify(
                Self::plaintext(self.node_identity, self.request_timestamp),
                &self.signature,
            )
            .is_ok()
    }

    pub fn is_stale(&self) -> bool {
        let Ok(encoded) = OffsetDateTime::from_unix_timestamp(self.request_timestamp) else {
            return true;
        };
        let now = OffsetDateTime::now_utc();

        if encoded > now {
            return true;
        }

        if (encoded + Duration::from_secs(30)) < now {
            return true;
        }

        false
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
pub struct UptimeResponse {
    #[schema(value_type = u32)]
    pub mix_id: NodeId,
    // The same as node_performance.last_24h. Legacy
    pub avg_uptime: u8,
    pub node_performance: NodePerformance,
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
pub struct GatewayUptimeResponse {
    pub identity: String,
    // The same as node_performance.last_24h. Legacy
    pub avg_uptime: u8,
    pub node_performance: NodePerformance,
}

type Uptime = u8;

#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
pub struct MixnodeStatusReportResponse {
    pub mix_id: NodeId,
    pub identity: IdentityKey,
    pub owner: String,
    #[schema(value_type = u8)]
    pub most_recent: Uptime,
    #[schema(value_type = u8)]
    pub last_hour: Uptime,
    #[schema(value_type = u8)]
    pub last_day: Uptime,
}

#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
pub struct GatewayStatusReportResponse {
    pub identity: String,
    pub owner: String,
    #[schema(value_type = u8)]
    pub most_recent: Uptime,
    #[schema(value_type = u8)]
    pub last_hour: Uptime,
    #[schema(value_type = u8)]
    pub last_day: Uptime,
}

#[derive(Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/PerformanceHistoryResponse.ts"
    )
)]
pub struct PerformanceHistoryResponse {
    #[schema(value_type = u32)]
    pub node_id: NodeId,
    pub history: PaginatedResponse<HistoricalPerformanceResponse>,
}

#[derive(Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/UptimeHistoryResponse.ts"
    )
)]
pub struct UptimeHistoryResponse {
    #[schema(value_type = u32)]
    pub node_id: NodeId,
    pub history: PaginatedResponse<HistoricalUptimeResponse>,
}

#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/HistoricalUptimeResponse.ts"
    )
)]
pub struct HistoricalUptimeResponse {
    #[schema(value_type = String, example = "1970-01-01")]
    #[schemars(with = "String")]
    #[cfg_attr(feature = "generate-ts", ts(type = "string"))]
    pub date: Date,

    pub uptime: Uptime,
}

#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/HistoricalPerformanceResponse.ts"
    )
)]
pub struct HistoricalPerformanceResponse {
    #[schema(value_type = String, example = "1970-01-01")]
    #[schemars(with = "String")]
    #[cfg_attr(feature = "generate-ts", ts(type = "string"))]
    pub date: Date,

    pub performance: f64,
}

#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
pub struct OldHistoricalUptimeResponse {
    pub date: String,
    #[schema(value_type = u8)]
    pub uptime: Uptime,
}

#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
pub struct MixnodeUptimeHistoryResponse {
    pub mix_id: NodeId,
    pub identity: String,
    pub history: Vec<OldHistoricalUptimeResponse>,
}

#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
pub struct GatewayUptimeHistoryResponse {
    pub identity: String,
    pub history: Vec<OldHistoricalUptimeResponse>,
}

#[derive(
    Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema, ToSchema, Default,
)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/MixnodeStatus.ts"
    )
)]
#[serde(rename_all = "snake_case")]
pub enum MixnodeStatus {
    Active,   // in both the active set and the rewarded set
    Standby,  // only in the rewarded set
    Inactive, // in neither the rewarded set nor the active set, but is bonded
    #[default]
    NotFound, // doesn't even exist in the bonded set
}
impl MixnodeStatus {
    pub fn is_active(&self) -> bool {
        *self == MixnodeStatus::Active
    }
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/MixnodeStatusResponse.ts"
    )
)]
pub struct MixnodeStatusResponse {
    pub status: MixnodeStatus,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
pub struct NodePerformance {
    #[schema(value_type = String)]
    pub most_recent: Performance,
    #[schema(value_type = String)]
    pub last_hour: Performance,
    #[schema(value_type = String)]
    pub last_24h: Performance,
}

// imo for now there's no point in exposing more than that,
// nym-api shouldn't be calculating apy or stake saturation for you.
// it should just return its own metrics (performance) and then you can do with it as you wish
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/NodeAnnotationV1.ts"
    )
)]
pub struct NodeAnnotationV1 {
    #[cfg_attr(feature = "generate-ts", ts(type = "string"))]
    // legacy
    #[schema(value_type = String)]
    pub last_24h_performance: Performance,
    pub current_role: Option<DisplayRole>,

    pub detailed_performance: DetailedNodePerformanceV1,
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/ChainInteractionCapabilitiesDetailed.ts"
    )
)]
pub struct ChainInteractionCapabilitiesDetailed {
    #[schema(value_type = CoinSchema)]
    #[cfg_attr(feature = "generate-ts", ts(type = "Coin"))]
    pub on_chain_balance: Coin,

    // later to be expanded with information on whether the grant would cover
    // cosmwasm executemsg, but for now we assume any feegrant is sufficient
    pub is_feegrant_grantee: bool,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/NodeAnnotationV2.ts"
    )
)]
pub struct NodeAnnotationV2 {
    pub current_role: Option<DisplayRole>,

    pub chain_interaction_capabilities: Option<ChainInteractionCapabilitiesDetailed>,

    pub detailed_performance: DetailedNodePerformanceV2,
}

impl From<NodeAnnotationV2> for NodeAnnotationV1 {
    fn from(value: NodeAnnotationV2) -> Self {
        // map it from 0-1 range into 0-100
        let scaled_performance =
            value.detailed_performance.performance_score.clamp(0.0, 1.0) * 100.;
        #[allow(clippy::unwrap_used)]
        let legacy_performance =
            Performance::from_percentage_value(scaled_performance as u64).unwrap();

        NodeAnnotationV1 {
            last_24h_performance: legacy_performance,
            current_role: value.current_role,
            detailed_performance: DetailedNodePerformanceV1 {
                performance_score: value.detailed_performance.performance_score,
                routing_score: value.detailed_performance.routing_score,
                config_score: value.detailed_performance.config_score.into(),
            },
        }
    }
}

#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/DetailedNodePerformanceV1.ts"
    )
)]
#[non_exhaustive]
pub struct DetailedNodePerformanceV1 {
    /// routing_score * config_score
    pub performance_score: f64,

    pub routing_score: RoutingScore,
    pub config_score: ConfigScoreV1,
}

impl DetailedNodePerformanceV1 {
    pub fn new(
        performance_score: f64,
        routing_score: RoutingScore,
        config_score: ConfigScoreV1,
    ) -> DetailedNodePerformanceV1 {
        Self {
            performance_score,
            routing_score,
            config_score,
        }
    }

    pub fn to_rewarding_performance(&self) -> Performance {
        Performance::naive_try_from_f64(self.performance_score).unwrap_or_default()
    }
}

#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/DetailedNodePerformanceV2.ts"
    )
)]
#[non_exhaustive]
pub struct DetailedNodePerformanceV2 {
    /// routing_score * config_score
    /// or
    /// routing_score * config_score * stress_testing_score, if enabled
    pub performance_score: f64,

    pub routing_score: RoutingScore,
    pub config_score: ConfigScoreV2,
    pub stress_testing_score: StressTestingScore,
}

impl DetailedNodePerformanceV2 {
    pub fn new(
        performance_score: f64,
        routing_score: RoutingScore,
        config_score: ConfigScoreV2,
        stress_testing_score: StressTestingScore,
    ) -> DetailedNodePerformanceV2 {
        Self {
            performance_score,
            routing_score,
            config_score,
            stress_testing_score,
        }
    }

    pub fn to_rewarding_performance(&self) -> Performance {
        Performance::naive_try_from_f64(self.performance_score).unwrap_or_default()
    }
}

#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(export, export_to = "ts-packages/types/src/types/rust/RoutingScore.ts")
)]
#[non_exhaustive]
pub struct RoutingScore {
    /// Total score after taking all the criteria into consideration
    pub score: f64,
}

impl RoutingScore {
    pub fn new(score: f64) -> RoutingScore {
        Self { score }
    }

    pub const fn zero() -> RoutingScore {
        RoutingScore { score: 0.0 }
    }

    pub fn legacy_performance(&self) -> Performance {
        Performance::naive_try_from_f64(self.score).unwrap_or_default()
    }
}

#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/StressTestingScore.ts"
    )
)]
pub struct StressTestingScore {
    pub score: f64,
    /// Distinguishes a genuine zero score (node was tested and scored 0) from
    /// "node was unreachable" (no successful sample was collected). Consumers may use
    /// this to decide whether to penalise the node or treat the score as missing.
    pub was_reachable: bool,
}

impl StressTestingScore {
    pub fn unreachable() -> Self {
        StressTestingScore {
            score: 0.0,
            was_reachable: false,
        }
    }
}

#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/ConfigScoreV2.ts"
    )
)]
#[non_exhaustive]
pub struct ConfigScoreV2 {
    /// Total score after taking all the criteria into consideration
    pub score: f64,

    pub versions_behind: Option<u32>,
    pub self_described_api_available: bool,
    pub accepted_terms_and_conditions: bool,
    pub runs_nym_node_binary: bool,

    /// Describes the node is capable of sending chain/contract transactions
    pub chain_interaction_capabilities: ChainInteractionCapabilities,
}

impl ConfigScoreV2 {
    pub fn new(
        score: f64,
        versions_behind: u32,
        accepted_terms_and_conditions: bool,
        runs_nym_node_binary: bool,
        chain_interaction_capabilities: ChainInteractionCapabilities,
    ) -> ConfigScoreV2 {
        Self {
            score,
            versions_behind: Some(versions_behind),
            self_described_api_available: true,
            accepted_terms_and_conditions,
            runs_nym_node_binary,
            chain_interaction_capabilities,
        }
    }

    pub fn bad_semver() -> ConfigScoreV2 {
        ConfigScoreV2 {
            score: 0.0,
            versions_behind: None,
            self_described_api_available: true,
            accepted_terms_and_conditions: false,
            runs_nym_node_binary: false,
            chain_interaction_capabilities: Default::default(),
        }
    }

    pub fn unavailable() -> ConfigScoreV2 {
        ConfigScoreV2 {
            score: 0.0,
            versions_behind: None,
            self_described_api_available: false,
            accepted_terms_and_conditions: false,
            runs_nym_node_binary: false,
            chain_interaction_capabilities: Default::default(),
        }
    }
}

#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/ChainInteractionCapabilities.ts"
    )
)]
pub struct ChainInteractionCapabilities {
    pub has_sufficient_tokens: bool,
    pub is_fee_grant_grantee: bool,
}

impl ChainInteractionCapabilities {
    pub fn new(has_sufficient_tokens: bool, is_fee_grant_grantee: bool) -> Self {
        Self {
            has_sufficient_tokens,
            is_fee_grant_grantee,
        }
    }

    pub fn can_send_transactions(&self) -> bool {
        self.has_sufficient_tokens || self.is_fee_grant_grantee
    }
}

impl From<ConfigScoreV2> for ConfigScoreV1 {
    fn from(score_v2: ConfigScoreV2) -> ConfigScoreV1 {
        ConfigScoreV1 {
            score: score_v2.score,
            versions_behind: score_v2.versions_behind,
            self_described_api_available: score_v2.self_described_api_available,
            accepted_terms_and_conditions: score_v2.accepted_terms_and_conditions,
            runs_nym_node_binary: score_v2.runs_nym_node_binary,
        }
    }
}

#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/ConfigScoreV1.ts"
    )
)]
#[non_exhaustive]
pub struct ConfigScoreV1 {
    /// Total score after taking all the criteria into consideration
    pub score: f64,

    pub versions_behind: Option<u32>,
    pub self_described_api_available: bool,
    pub accepted_terms_and_conditions: bool,
    pub runs_nym_node_binary: bool,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/AnnotationResponseV1.ts"
    )
)]
pub struct AnnotationResponseV1 {
    #[schema(value_type = u32)]
    pub node_id: NodeId,
    pub annotation: Option<NodeAnnotationV1>,
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/AnnotationResponseV2.ts"
    )
)]
pub struct AnnotationResponseV2 {
    #[schema(value_type = u32)]
    pub node_id: NodeId,
    pub annotation: Option<NodeAnnotationV2>,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/NodePerformanceResponse.ts"
    )
)]
pub struct NodePerformanceResponse {
    #[schema(value_type = u32)]
    pub node_id: NodeId,
    pub performance: Option<f64>,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
    feature = "generate-ts",
    ts(
        export,
        export_to = "ts-packages/types/src/types/rust/NodeDatePerformanceResponse.ts"
    )
)]
pub struct NodeDatePerformanceResponse {
    #[schema(value_type = u32)]
    pub node_id: NodeId,
    #[schema(value_type = String, example = "1970-01-01")]
    #[schemars(with = "String")]
    #[cfg_attr(feature = "generate-ts", ts(type = "string"))]
    pub date: Date,
    pub performance: Option<f64>,
}