iota_json_rpc_types/
iota_governance.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use iota_types::{
6    base_types::{AuthorityName, EpochId, IotaAddress, ObjectID},
7    committee::{Committee, StakeUnit},
8};
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11use serde_with::{DisplayFromStr, serde_as};
12
13use crate::{
14    IotaAuthorityPublicKeyBytes,
15    iota_primitives::{IotaAddress as IotaAddressSchema, ObjectID as ObjectIDSchema},
16};
17
18/// RPC representation of the [Committee] type.
19#[serde_as]
20#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
21#[serde(rename = "CommitteeInfo")]
22pub struct IotaCommittee {
23    #[schemars(with = "String")]
24    #[serde_as(as = "DisplayFromStr")]
25    pub epoch: EpochId,
26    #[schemars(with = "Vec<(IotaAuthorityPublicKeyBytes, String)>")]
27    #[serde_as(as = "Vec<(_, DisplayFromStr)>")]
28    pub validators: Vec<(AuthorityName, StakeUnit)>,
29}
30
31impl From<Committee> for IotaCommittee {
32    fn from(committee: Committee) -> Self {
33        Self {
34            epoch: committee.epoch,
35            validators: committee.voting_rights.into_iter().collect(),
36        }
37    }
38}
39
40impl From<IotaCommittee> for Committee {
41    fn from(iota_committee: IotaCommittee) -> Self {
42        Committee::new(
43            iota_committee.epoch,
44            iota_committee.validators.into_iter().collect(),
45        )
46    }
47}
48
49#[serde_as]
50#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
51#[serde(rename_all = "camelCase")]
52pub struct DelegatedStake {
53    /// Validator's Address.
54    #[schemars(with = "IotaAddressSchema")]
55    pub validator_address: IotaAddress,
56    /// Staking pool object id.
57    #[schemars(with = "ObjectIDSchema")]
58    pub staking_pool: ObjectID,
59    pub stakes: Vec<Stake>,
60}
61
62#[serde_as]
63#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
64#[serde(rename_all = "camelCase")]
65pub struct DelegatedTimelockedStake {
66    #[schemars(with = "IotaAddressSchema")]
67    pub validator_address: IotaAddress,
68    #[schemars(with = "ObjectIDSchema")]
69    pub staking_pool: ObjectID,
70    pub stakes: Vec<TimelockedStake>,
71}
72
73#[serde_as]
74#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
75#[serde(tag = "status")]
76pub enum StakeStatus {
77    Pending,
78    #[serde(rename_all = "camelCase")]
79    Active {
80        #[serde_as(as = "DisplayFromStr")]
81        #[schemars(with = "String")]
82        estimated_reward: u64,
83    },
84    Unstaked,
85}
86
87#[serde_as]
88#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
89#[serde(rename_all = "camelCase")]
90pub struct Stake {
91    /// ID of the StakedIota receipt object.
92    #[schemars(with = "ObjectIDSchema")]
93    pub staked_iota_id: ObjectID,
94    #[serde_as(as = "DisplayFromStr")]
95    #[schemars(with = "String")]
96    pub stake_request_epoch: EpochId,
97    #[serde_as(as = "DisplayFromStr")]
98    #[schemars(with = "String")]
99    pub stake_active_epoch: EpochId,
100    #[serde_as(as = "DisplayFromStr")]
101    #[schemars(with = "String")]
102    pub principal: u64,
103    #[serde(flatten)]
104    pub status: StakeStatus,
105}
106
107#[serde_as]
108#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
109#[serde(rename_all = "camelCase")]
110pub struct TimelockedStake {
111    #[schemars(with = "ObjectIDSchema")]
112    pub timelocked_staked_iota_id: ObjectID,
113    #[serde_as(as = "DisplayFromStr")]
114    #[schemars(with = "String")]
115    pub stake_request_epoch: EpochId,
116    #[serde_as(as = "DisplayFromStr")]
117    #[schemars(with = "String")]
118    pub stake_active_epoch: EpochId,
119    #[serde_as(as = "DisplayFromStr")]
120    #[schemars(with = "String")]
121    pub principal: u64,
122    #[serde(flatten)]
123    pub status: StakeStatus,
124    #[serde_as(as = "DisplayFromStr")]
125    #[schemars(with = "String")]
126    pub expiration_timestamp_ms: u64,
127    pub label: Option<String>,
128}
129
130#[serde_as]
131#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
132pub struct ValidatorApys {
133    pub apys: Vec<ValidatorApy>,
134    #[serde_as(as = "DisplayFromStr")]
135    #[schemars(with = "String")]
136    pub epoch: EpochId,
137}
138
139#[serde_as]
140#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
141pub struct ValidatorApy {
142    #[schemars(with = "IotaAddressSchema")]
143    pub address: IotaAddress,
144    pub apy: f64,
145}