mod classification;
mod economics;
mod events;
mod metrics;
use super::{NNS_GOVERNANCE_REPORT_SCHEMA_VERSION, NnsGovernanceRequest};
use crate::nns::MAINNET_GOVERNANCE_CANISTER_ID;
#[cfg(feature = "nns-host")]
use crate::{nns::governance::NnsGovernanceCacheMetadata, snapshot_cache::SnapshotEnvelope};
use serde::{Deserialize as SerdeDeserialize, Serialize};
use std::fmt;
pub use classification::NnsGovernanceVote;
#[derive(Clone, Copy, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum NnsGovernanceCollection {
Neurons,
Proposals,
}
impl NnsGovernanceCollection {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Neurons => "neurons",
Self::Proposals => "proposals",
}
}
}
impl fmt::Display for NnsGovernanceCollection {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
pub(in crate::nns) use classification::{
deserialize_code_label, native_code_classification, native_optional_code_classification,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum NnsGovernanceCollectionStatus {
Ready,
Collecting,
Complete,
PageLimitReached,
}
impl NnsGovernanceCollectionStatus {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Ready => "ready",
Self::Collecting => "collecting",
Self::Complete => "complete",
Self::PageLimitReached => "page_limit_reached",
}
}
}
impl fmt::Display for NnsGovernanceCollectionStatus {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum NnsGovernanceExecutionAssurance {
UnreplicatedQuery,
ReplicatedExecution,
}
impl NnsGovernanceExecutionAssurance {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::UnreplicatedQuery => "unreplicated_query",
Self::ReplicatedExecution => "replicated_execution",
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
#[serde(tag = "source_transport", rename_all = "snake_case")]
pub enum NnsGovernanceSourceProvenance {
ReplicaQuery {
endpoint: String,
fetched_by: String,
},
ReplicatedInterCanisterCall {
collector_canister_id: String,
},
}
impl NnsGovernanceSourceProvenance {
#[must_use]
pub const fn execution_assurance(&self) -> NnsGovernanceExecutionAssurance {
match self {
Self::ReplicaQuery { .. } => NnsGovernanceExecutionAssurance::UnreplicatedQuery,
Self::ReplicatedInterCanisterCall { .. } => {
NnsGovernanceExecutionAssurance::ReplicatedExecution
}
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct NnsGovernanceAnalyticsContext {
pub schema_version: u32,
pub network: String,
pub governance_canister_id: String,
pub source: NnsGovernanceSourceProvenance,
pub collection_started_at: String,
pub collection_updated_at: String,
pub collection_page_count: u32,
pub point_in_time_guaranteed: bool,
}
#[derive(Clone, Debug, Eq, PartialEq, SerdeDeserialize, Serialize)]
pub struct NnsGovernanceReportContext {
pub schema_version: u32,
pub network: String,
pub governance_canister_id: String,
pub fetched_at: String,
pub source: NnsGovernanceSourceProvenance,
}
impl NnsGovernanceReportContext {
pub(in crate::nns) fn from_source(
request: &NnsGovernanceRequest,
source: NnsGovernanceSourceProvenance,
) -> Self {
Self {
schema_version: NNS_GOVERNANCE_REPORT_SCHEMA_VERSION,
network: request.network.clone(),
governance_canister_id: MAINNET_GOVERNANCE_CANISTER_ID.to_string(),
fetched_at: request.fetched_at.clone(),
source,
}
}
#[cfg(feature = "nns-host")]
pub(in crate::nns) fn from_snapshot<Rows>(
cache: &SnapshotEnvelope<NnsGovernanceCacheMetadata, Rows>,
) -> Self {
Self {
schema_version: 1,
network: cache.network.clone(),
governance_canister_id: cache.metadata.governance_canister_id.clone(),
fetched_at: cache.fetched_at.clone(),
source: NnsGovernanceSourceProvenance::ReplicaQuery {
endpoint: cache.source_endpoint.clone(),
fetched_by: cache.fetched_by.clone(),
},
}
}
}
pub use economics::{
NnsGovernanceDecimal, NnsGovernanceEconomics, NnsGovernanceEconomicsReport,
NnsGovernancePercentage, NnsNeuronsFundEconomics,
NnsNeuronsFundMatchedFundingCurveCoefficients, NnsVotingPowerEconomics,
};
pub use events::{
NnsGovernanceMaturityModulation, NnsGovernanceMaturityModulationReport,
NnsGovernanceProposalId, NnsGovernanceRewardEvent, NnsGovernanceRewardEventReport,
};
pub use metrics::{
NnsGovernanceMetricBucket, NnsGovernanceMetrics, NnsGovernanceMetricsReport,
NnsGovernanceNeuronSubsetMetrics,
};
#[cfg(test)]
mod tests;