iota_json_rpc_types/
iota_extended.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::collections::BTreeMap;
6
7use fastcrypto::traits::ToFromBytes;
8use iota_types::{
9    base_types::{AuthorityName, EpochId, ObjectID},
10    committee::Committee,
11    iota_serde::BigInt,
12    iota_system_state::iota_system_state_summary::IotaValidatorSummary,
13    messages_checkpoint::CheckpointSequenceNumber,
14};
15use move_core_types::identifier::Identifier;
16use schemars::JsonSchema;
17use serde::{Deserialize, Serialize};
18use serde_with::{DisplayFromStr, serde_as};
19
20use crate::Page;
21
22pub type EpochPage = Page<EpochInfo, BigInt<u64>>;
23pub type EpochMetricsPage = Page<EpochMetrics, BigInt<u64>>;
24
25#[serde_as]
26#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
27#[serde(rename_all = "camelCase")]
28pub struct EpochInfo {
29    /// Epoch number
30    #[schemars(with = "BigInt<u64>")]
31    #[serde_as(as = "BigInt<u64>")]
32    pub epoch: EpochId,
33    /// List of validators included in epoch
34    pub validators: Vec<IotaValidatorSummary>,
35    /// Count of tx in epoch
36    #[schemars(with = "BigInt<u64>")]
37    #[serde_as(as = "BigInt<u64>")]
38    pub epoch_total_transactions: u64,
39    /// First, last checkpoint sequence numbers
40    #[schemars(with = "BigInt<u64>")]
41    #[serde_as(as = "BigInt<u64>")]
42    pub first_checkpoint_id: CheckpointSequenceNumber,
43    /// The timestamp when the epoch started.
44    #[schemars(with = "BigInt<u64>")]
45    #[serde_as(as = "BigInt<u64>")]
46    pub epoch_start_timestamp: u64,
47    /// The end of epoch information.
48    pub end_of_epoch_info: Option<EndOfEpochInfo>,
49    /// The reference gas price for the given epoch.
50    pub reference_gas_price: Option<u64>,
51    /// Committee validators. Each element is an index
52    /// pointing to `validators`.
53    #[schemars(with = "Vec<BigInt<u64>>")]
54    #[serde_as(as = "Vec<BigInt<u64>>")]
55    #[serde(skip_serializing_if = "Vec::is_empty")]
56    #[serde(default)]
57    pub committee_members: Vec<u64>,
58}
59
60impl EpochInfo {
61    pub fn committee(&self) -> Result<Committee, fastcrypto::error::FastCryptoError> {
62        let mut voting_rights = BTreeMap::new();
63        for &i in &self.committee_members {
64            let validator = self
65                .validators
66                .get(i as usize)
67                .expect("validators should include committee members");
68            let name = AuthorityName::from_bytes(&validator.authority_pubkey_bytes)?;
69            voting_rights.insert(name, validator.voting_power);
70        }
71        Ok(Committee::new(self.epoch, voting_rights))
72    }
73}
74
75/// A light-weight version of `EpochInfo` for faster loading
76#[serde_as]
77#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
78#[serde(rename_all = "camelCase")]
79pub struct EpochMetrics {
80    /// The current epoch ID.
81    #[schemars(with = "BigInt<u64>")]
82    #[serde_as(as = "BigInt<u64>")]
83    pub epoch: EpochId,
84    /// The total number of transactions in the epoch.
85    #[schemars(with = "BigInt<u64>")]
86    #[serde_as(as = "BigInt<u64>")]
87    pub epoch_total_transactions: u64,
88    /// The first checkpoint ID of the epoch.
89    #[schemars(with = "BigInt<u64>")]
90    #[serde_as(as = "BigInt<u64>")]
91    pub first_checkpoint_id: CheckpointSequenceNumber,
92    /// The timestamp when the epoch started.
93    #[schemars(with = "BigInt<u64>")]
94    #[serde_as(as = "BigInt<u64>")]
95    pub epoch_start_timestamp: u64,
96    /// The end of epoch information.
97    pub end_of_epoch_info: Option<EndOfEpochInfo>,
98}
99
100#[serde_as]
101#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
102#[serde(rename_all = "camelCase")]
103pub struct EndOfEpochInfo {
104    #[schemars(with = "BigInt<u64>")]
105    #[serde_as(as = "BigInt<u64>")]
106    pub last_checkpoint_id: CheckpointSequenceNumber,
107    #[schemars(with = "BigInt<u64>")]
108    #[serde_as(as = "BigInt<u64>")]
109    pub epoch_end_timestamp: u64,
110    /// existing fields from `SystemEpochInfoEventV1` (without epoch)
111    #[schemars(with = "BigInt<u64>")]
112    #[serde_as(as = "BigInt<u64>")]
113    pub protocol_version: u64,
114    #[schemars(with = "BigInt<u64>")]
115    #[serde_as(as = "BigInt<u64>")]
116    pub reference_gas_price: u64,
117    #[schemars(with = "BigInt<u64>")]
118    #[serde_as(as = "BigInt<u64>")]
119    pub total_stake: u64,
120    #[schemars(with = "BigInt<u64>")]
121    #[serde_as(as = "BigInt<u64>")]
122    pub storage_charge: u64,
123    #[schemars(with = "BigInt<u64>")]
124    #[serde_as(as = "BigInt<u64>")]
125    pub storage_rebate: u64,
126    #[schemars(with = "BigInt<u64>")]
127    #[serde_as(as = "BigInt<u64>")]
128    pub storage_fund_balance: u64,
129    #[schemars(with = "BigInt<u64>")]
130    #[serde_as(as = "BigInt<u64>")]
131    pub total_gas_fees: u64,
132    #[schemars(with = "BigInt<u64>")]
133    #[serde_as(as = "BigInt<u64>")]
134    pub total_stake_rewards_distributed: u64,
135    #[schemars(with = "BigInt<u64>")]
136    #[serde_as(as = "BigInt<u64>")]
137    pub burnt_tokens_amount: u64,
138    #[schemars(with = "BigInt<u64>")]
139    #[serde_as(as = "BigInt<u64>")]
140    pub minted_tokens_amount: u64,
141}
142
143#[serde_as]
144#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone)]
145#[serde(rename_all = "camelCase")]
146pub struct NetworkMetrics {
147    /// Current TPS - Transaction Blocks per Second.
148    pub current_tps: f64,
149    /// Peak TPS in the past 30 days
150    pub tps_30_days: f64,
151    /// Total number of packages published in the network
152    #[schemars(with = "BigInt<u64>")]
153    #[serde_as(as = "BigInt<u64>")]
154    pub total_packages: u64,
155    /// Total number of addresses seen in the network
156    #[schemars(with = "BigInt<u64>")]
157    #[serde_as(as = "BigInt<u64>")]
158    pub total_addresses: u64,
159    /// Total number of live objects in the network
160    #[schemars(with = "BigInt<u64>")]
161    #[serde_as(as = "BigInt<u64>")]
162    pub total_objects: u64,
163    /// Current epoch number
164    #[schemars(with = "BigInt<u64>")]
165    #[serde_as(as = "BigInt<u64>")]
166    pub current_epoch: u64,
167    /// Current checkpoint number
168    #[schemars(with = "BigInt<u64>")]
169    #[serde_as(as = "BigInt<u64>")]
170    pub current_checkpoint: u64,
171}
172
173#[serde_as]
174#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
175#[serde(rename_all = "camelCase")]
176pub struct MoveCallMetrics {
177    /// The count of calls of each function in the last 3 days.
178    #[schemars(with = "Vec<(MoveFunctionName, BigInt<usize>)>")]
179    #[serde_as(as = "Vec<(_, BigInt<usize>)>")]
180    pub rank_3_days: Vec<(MoveFunctionName, usize)>,
181    /// The count of calls of each function in the last 7 days.
182    #[schemars(with = "Vec<(MoveFunctionName, BigInt<usize>)>")]
183    #[serde_as(as = "Vec<(_, BigInt<usize>)>")]
184    pub rank_7_days: Vec<(MoveFunctionName, usize)>,
185    /// The count of calls of each function in the last 30 days.
186    #[schemars(with = "Vec<(MoveFunctionName, BigInt<usize>)>")]
187    #[serde_as(as = "Vec<(_, BigInt<usize>)>")]
188    pub rank_30_days: Vec<(MoveFunctionName, usize)>,
189}
190
191/// Identifies a Move function.
192#[serde_as]
193#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
194#[serde(rename_all = "camelCase")]
195pub struct MoveFunctionName {
196    /// The package ID to which the function belongs.
197    pub package: ObjectID,
198    /// The module name to which the function belongs.
199    #[schemars(with = "String")]
200    #[serde_as(as = "DisplayFromStr")]
201    pub module: Identifier,
202    /// The function name.
203    #[schemars(with = "String")]
204    #[serde_as(as = "DisplayFromStr")]
205    pub function: Identifier,
206}
207
208/// Provides metrics about the addresses.
209#[serde_as]
210#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
211#[serde(rename_all = "camelCase")]
212pub struct AddressMetrics {
213    /// The checkpoint sequence number at which the metrics were computed.
214    pub checkpoint: u64,
215    /// The epoch to which the checkpoint is assigned.
216    pub epoch: u64,
217    /// The checkpoint timestamp.
218    pub timestamp_ms: u64,
219    /// The count of sender and recipient addresses.
220    pub cumulative_addresses: u64,
221    /// The count of sender addresses.
222    pub cumulative_active_addresses: u64,
223    /// The count of daily unique sender addresses.
224    pub daily_active_addresses: u64,
225}
226
227/// Provides metrics about the participation in the network.
228#[serde_as]
229#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
230#[serde(rename_all = "camelCase")]
231pub struct ParticipationMetrics {
232    /// The count of distinct addresses with delegated stake.
233    pub total_addresses: u64,
234}