1use std::collections::BTreeMap;
6
7use fastcrypto::traits::ToFromBytes;
8use iota_types::{
9 base_types::{AuthorityName, EpochId},
10 committee::Committee,
11 iota_serde::BigInt,
12 iota_system_state::iota_system_state_summary::IotaValidatorSummary,
13 messages_checkpoint::CheckpointSequenceNumber,
14};
15use schemars::JsonSchema;
16use serde::{Deserialize, Serialize};
17use serde_with::{DisplayFromStr, serde_as};
18
19use crate::{
20 MoveFunctionName, Page,
21 iota_system_state_summary::IotaValidatorSummary as IotaValidatorSummarySchema,
22};
23
24pub type EpochPage = Page<EpochInfo, BigInt<u64>>;
25pub type EpochMetricsPage = Page<EpochMetrics, BigInt<u64>>;
26
27#[serde_as]
28#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
29#[serde(rename_all = "camelCase")]
30pub struct EpochInfo {
31 #[schemars(with = "String")]
33 #[serde_as(as = "DisplayFromStr")]
34 pub epoch: EpochId,
35 #[schemars(with = "Vec<IotaValidatorSummarySchema>")]
37 pub validators: Vec<IotaValidatorSummary>,
38 #[schemars(with = "String")]
40 #[serde_as(as = "DisplayFromStr")]
41 pub epoch_total_transactions: u64,
42 #[schemars(with = "String")]
44 #[serde_as(as = "DisplayFromStr")]
45 pub first_checkpoint_id: CheckpointSequenceNumber,
46 #[schemars(with = "String")]
48 #[serde_as(as = "DisplayFromStr")]
49 pub epoch_start_timestamp: u64,
50 pub end_of_epoch_info: Option<EndOfEpochInfo>,
52 pub reference_gas_price: Option<u64>,
54 #[schemars(with = "Vec<String>")]
57 #[serde_as(as = "Vec<DisplayFromStr>")]
58 #[serde(skip_serializing_if = "Vec::is_empty")]
59 #[serde(default)]
60 pub committee_members: Vec<u64>,
61}
62
63impl EpochInfo {
64 pub fn committee(&self) -> Result<Committee, fastcrypto::error::FastCryptoError> {
65 let mut voting_rights = BTreeMap::new();
66 for &i in &self.committee_members {
67 let validator = self
68 .validators
69 .get(i as usize)
70 .expect("validators should include committee members");
71 let name = AuthorityName::from_bytes(&validator.authority_pubkey_bytes)?;
72 voting_rights.insert(name, validator.voting_power);
73 }
74 Ok(Committee::new(self.epoch, voting_rights))
75 }
76}
77
78#[serde_as]
80#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
81#[serde(rename_all = "camelCase")]
82pub struct EpochMetrics {
83 #[schemars(with = "String")]
85 #[serde_as(as = "DisplayFromStr")]
86 pub epoch: EpochId,
87 #[schemars(with = "String")]
89 #[serde_as(as = "DisplayFromStr")]
90 pub epoch_total_transactions: u64,
91 #[schemars(with = "String")]
93 #[serde_as(as = "DisplayFromStr")]
94 pub first_checkpoint_id: CheckpointSequenceNumber,
95 #[schemars(with = "String")]
97 #[serde_as(as = "DisplayFromStr")]
98 pub epoch_start_timestamp: u64,
99 pub end_of_epoch_info: Option<EndOfEpochInfo>,
101}
102
103#[serde_as]
104#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
105#[serde(rename_all = "camelCase")]
106pub struct EndOfEpochInfo {
107 #[schemars(with = "String")]
108 #[serde_as(as = "DisplayFromStr")]
109 pub last_checkpoint_id: CheckpointSequenceNumber,
110 #[schemars(with = "String")]
111 #[serde_as(as = "DisplayFromStr")]
112 pub epoch_end_timestamp: u64,
113 #[schemars(with = "String")]
115 #[serde_as(as = "DisplayFromStr")]
116 pub protocol_version: u64,
117 #[schemars(with = "String")]
118 #[serde_as(as = "DisplayFromStr")]
119 pub reference_gas_price: u64,
120 #[schemars(with = "String")]
121 #[serde_as(as = "DisplayFromStr")]
122 pub total_stake: u64,
123 #[schemars(with = "String")]
124 #[serde_as(as = "DisplayFromStr")]
125 pub storage_charge: u64,
126 #[schemars(with = "String")]
127 #[serde_as(as = "DisplayFromStr")]
128 pub storage_rebate: u64,
129 #[schemars(with = "String")]
130 #[serde_as(as = "DisplayFromStr")]
131 pub storage_fund_balance: u64,
132 #[schemars(with = "String")]
133 #[serde_as(as = "DisplayFromStr")]
134 pub total_gas_fees: u64,
135 #[schemars(with = "String")]
136 #[serde_as(as = "DisplayFromStr")]
137 pub total_stake_rewards_distributed: u64,
138 #[schemars(with = "String")]
139 #[serde_as(as = "DisplayFromStr")]
140 pub burnt_tokens_amount: u64,
141 #[schemars(with = "String")]
142 #[serde_as(as = "DisplayFromStr")]
143 pub minted_tokens_amount: u64,
144}
145
146#[serde_as]
147#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone)]
148#[serde(rename_all = "camelCase")]
149pub struct NetworkMetrics {
150 pub current_tps: f64,
152 pub tps_30_days: f64,
154 #[schemars(with = "String")]
156 #[serde_as(as = "DisplayFromStr")]
157 pub total_packages: u64,
158 #[schemars(with = "String")]
160 #[serde_as(as = "DisplayFromStr")]
161 pub total_addresses: u64,
162 #[schemars(with = "String")]
164 #[serde_as(as = "DisplayFromStr")]
165 pub total_objects: u64,
166 #[schemars(with = "String")]
168 #[serde_as(as = "DisplayFromStr")]
169 pub current_epoch: u64,
170 #[schemars(with = "String")]
172 #[serde_as(as = "DisplayFromStr")]
173 pub current_checkpoint: u64,
174}
175
176#[serde_as]
177#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
178#[serde(rename_all = "camelCase")]
179pub struct MoveCallMetrics {
180 #[schemars(with = "Vec<(MoveFunctionName, String)>")]
182 #[serde_as(as = "Vec<(_, DisplayFromStr)>")]
183 pub rank_3_days: Vec<(MoveFunctionName, usize)>,
184 #[schemars(with = "Vec<(MoveFunctionName, String)>")]
186 #[serde_as(as = "Vec<(_, DisplayFromStr)>")]
187 pub rank_7_days: Vec<(MoveFunctionName, usize)>,
188 #[schemars(with = "Vec<(MoveFunctionName, String)>")]
190 #[serde_as(as = "Vec<(_, DisplayFromStr)>")]
191 pub rank_30_days: Vec<(MoveFunctionName, usize)>,
192}
193
194#[serde_as]
196#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
197#[serde(rename_all = "camelCase")]
198pub struct AddressMetrics {
199 pub checkpoint: u64,
201 pub epoch: u64,
203 pub timestamp_ms: u64,
205 pub cumulative_addresses: u64,
207 pub cumulative_active_addresses: u64,
209 pub daily_active_addresses: u64,
211}
212
213#[serde_as]
215#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
216#[serde(rename_all = "camelCase")]
217pub struct ParticipationMetrics {
218 pub total_addresses: u64,
220}