Skip to main content

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