nym_types/
nym_node.rs

1// Copyright 2024 - Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::currency::{DecCoin, RegisteredCoins};
5use crate::error::TypesError;
6use crate::mixnode::NodeRewarding;
7use nym_mixnet_contract_common::{NodeId, NymNode, PendingNodeChanges};
8use nym_mixnet_contract_common::{
9    NymNodeBond as MixnetContractNymNodeBond, NymNodeDetails as MixnetContractNymNodeDetails,
10};
11use schemars::JsonSchema;
12use serde::{Deserialize, Serialize};
13
14/// Full details associated with given node.
15#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
16#[cfg_attr(
17    feature = "generate-ts",
18    ts(
19        export,
20        export_to = "ts-packages/types/src/types/rust/NymNodeDetails.ts"
21    )
22)]
23#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
24pub struct NymNodeDetails {
25    /// Basic bond information of this node, such as owner address, original pledge, etc.
26    pub bond_information: NymNodeBond,
27
28    /// Details used for computation of rewarding related data.
29    pub rewarding_details: NodeRewarding,
30
31    /// Adjustments to the node that are scheduled to happen during future epoch/interval transitions.
32    pub pending_changes: PendingNodeChanges,
33}
34
35impl NymNodeDetails {
36    pub fn from_mixnet_contract_nym_node_details(
37        details: MixnetContractNymNodeDetails,
38        reg: &RegisteredCoins,
39    ) -> Result<NymNodeDetails, TypesError> {
40        Ok(NymNodeDetails {
41            bond_information: NymNodeBond::from_mixnet_contract_mixnode_bond(
42                details.bond_information,
43                reg,
44            )?,
45            rewarding_details: NodeRewarding::from_mixnet_contract_node_rewarding(
46                details.rewarding_details,
47                reg,
48            )?,
49            pending_changes: details.pending_changes,
50        })
51    }
52}
53
54#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
55#[cfg_attr(
56    feature = "generate-ts",
57    ts(export, export_to = "ts-packages/types/src/types/rust/NymNodeBond.ts")
58)]
59#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
60pub struct NymNodeBond {
61    /// Unique id assigned to the bonded node.
62    pub node_id: NodeId,
63
64    /// Address of the owner of this nym-node.
65    pub owner: String,
66
67    /// Original amount pledged by the operator of this node.
68    pub original_pledge: DecCoin,
69
70    /// Block height at which this nym-node has been bonded.
71    pub bonding_height: u64,
72
73    /// Flag to indicate whether this node is in the process of unbonding,
74    /// that will conclude upon the epoch finishing.
75    pub is_unbonding: bool,
76
77    #[serde(flatten)]
78    /// Information provided by the operator for the purposes of bonding.
79    pub node: NymNode,
80}
81
82impl NymNodeBond {
83    pub fn from_mixnet_contract_mixnode_bond(
84        bond: MixnetContractNymNodeBond,
85        reg: &RegisteredCoins,
86    ) -> Result<NymNodeBond, TypesError> {
87        Ok(NymNodeBond {
88            node_id: bond.node_id,
89            owner: bond.owner.into_string(),
90            original_pledge: reg
91                .attempt_convert_to_display_dec_coin(bond.original_pledge.into())?,
92            node: bond.node,
93            bonding_height: bond.bonding_height,
94            is_unbonding: bond.is_unbonding,
95        })
96    }
97}