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