use std::collections::BTreeMap;
use fastcrypto::traits::ToFromBytes;
use iota_types::{
base_types::{AuthorityName, EpochId, ObjectID},
committee::Committee,
iota_serde::BigInt,
iota_system_state::iota_system_state_summary::IotaValidatorSummary,
messages_checkpoint::CheckpointSequenceNumber,
};
use move_core_types::identifier::Identifier;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_with::{DisplayFromStr, serde_as};
use crate::Page;
pub type EpochPage = Page<EpochInfo, BigInt<u64>>;
pub type EpochMetricsPage = Page<EpochMetrics, BigInt<u64>>;
#[serde_as]
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct EpochInfo {
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub epoch: EpochId,
pub validators: Vec<IotaValidatorSummary>,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub epoch_total_transactions: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub first_checkpoint_id: CheckpointSequenceNumber,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub epoch_start_timestamp: u64,
pub end_of_epoch_info: Option<EndOfEpochInfo>,
pub reference_gas_price: Option<u64>,
}
impl EpochInfo {
pub fn committee(&self) -> Result<Committee, fastcrypto::error::FastCryptoError> {
let mut voting_rights = BTreeMap::new();
for validator in &self.validators {
let name = AuthorityName::from_bytes(&validator.authority_pubkey_bytes)?;
voting_rights.insert(name, validator.voting_power);
}
Ok(Committee::new(self.epoch, voting_rights))
}
}
#[serde_as]
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct EpochMetrics {
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub epoch: EpochId,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub epoch_total_transactions: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub first_checkpoint_id: CheckpointSequenceNumber,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub epoch_start_timestamp: u64,
pub end_of_epoch_info: Option<EndOfEpochInfo>,
}
#[serde_as]
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct EndOfEpochInfo {
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub last_checkpoint_id: CheckpointSequenceNumber,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub epoch_end_timestamp: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub protocol_version: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub reference_gas_price: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub total_stake: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub storage_charge: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub storage_rebate: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub storage_fund_balance: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub total_gas_fees: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub total_stake_rewards_distributed: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub burnt_tokens_amount: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub minted_tokens_amount: u64,
}
#[serde_as]
#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone)]
#[serde(rename_all = "camelCase")]
pub struct NetworkMetrics {
pub current_tps: f64,
pub tps_30_days: f64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub total_packages: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub total_addresses: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub total_objects: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub current_epoch: u64,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub current_checkpoint: u64,
}
#[serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct MoveCallMetrics {
#[schemars(with = "Vec<(MoveFunctionName, BigInt<usize>)>")]
#[serde_as(as = "Vec<(_, BigInt<usize>)>")]
pub rank_3_days: Vec<(MoveFunctionName, usize)>,
#[schemars(with = "Vec<(MoveFunctionName, BigInt<usize>)>")]
#[serde_as(as = "Vec<(_, BigInt<usize>)>")]
pub rank_7_days: Vec<(MoveFunctionName, usize)>,
#[schemars(with = "Vec<(MoveFunctionName, BigInt<usize>)>")]
#[serde_as(as = "Vec<(_, BigInt<usize>)>")]
pub rank_30_days: Vec<(MoveFunctionName, usize)>,
}
#[serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct MoveFunctionName {
pub package: ObjectID,
#[schemars(with = "String")]
#[serde_as(as = "DisplayFromStr")]
pub module: Identifier,
#[schemars(with = "String")]
#[serde_as(as = "DisplayFromStr")]
pub function: Identifier,
}
#[serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct AddressMetrics {
pub checkpoint: u64,
pub epoch: u64,
pub timestamp_ms: u64,
pub cumulative_addresses: u64,
pub cumulative_active_addresses: u64,
pub daily_active_addresses: u64,
}