Skip to content

Commit

Permalink
Store nervous system details in the Registry (#4555)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Oct 11, 2023
1 parent c6fec21 commit 56956b4
Show file tree
Hide file tree
Showing 26 changed files with 290 additions and 62 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/canister_installer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ async fn install_service_canisters_impl(
nns_governance_canister_id: canister_ids.nns_governance,
nns_root_canister_id: canister_ids.nns_root,
sns_wasm_canister_id: canister_ids.nns_sns_wasm,
nns_index_canister_id: canister_ids.nns_index,
cycles_dispenser_canister_id: canister_ids.cycles_dispenser,
wasm_version: version,
test_mode,
Expand Down
4 changes: 4 additions & 0 deletions backend/canister_installer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async fn main() {
nns_ledger: opts.nns_ledger,
nns_cmc: opts.nns_cmc,
nns_sns_wasm: opts.nns_sns_wasm,
nns_index: opts.nns_index,
};

let identity = get_dfx_identity(&opts.controller);
Expand Down Expand Up @@ -101,4 +102,7 @@ struct Opts {

#[arg(long)]
nns_sns_wasm: CanisterId,

#[arg(long)]
nns_index: CanisterId,
}
1 change: 1 addition & 0 deletions backend/canisters/registry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed

- Add `name` and `symbol` to rendering of 'Update token' proposals ([#4167](https://github.com/open-chat-labs/open-chat/pull/4167))
- Store nervous system details in the Registry ([#4555](https://github.com/open-chat-labs/open-chat/pull/4555))

## [[2.0.797](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.797-registry)] - 2023-08-08

Expand Down
1 change: 1 addition & 0 deletions backend/canisters/registry/api/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Args {
pub nns_governance_canister_id: CanisterId,
pub nns_root_canister_id: CanisterId,
pub sns_wasm_canister_id: CanisterId,
pub nns_index_canister_id: CanisterId,
pub cycles_dispenser_canister_id: CanisterId,
pub wasm_version: BuildVersion,
pub test_mode: bool,
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/registry/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ icrc1_ledger_canister = { path = "../../../external_canisters/icrc1_ledger/api"
icrc1_ledger_canister_c2c_client = { path = "../../../external_canisters/icrc1_ledger/c2c_client" }
serde = { workspace = true }
serializer = { path = "../../../libraries/serializer" }
sns_governance_canister = { path = "../../../external_canisters/sns_governance/api" }
sns_governance_canister_c2c_client = { path = "../../../external_canisters/sns_governance/c2c_client" }
sns_swap_canister = { path = "../../../external_canisters/sns_swap/api" }
sns_swap_canister_c2c_client = { path = "../../../external_canisters/sns_swap/c2c_client" }
Expand Down
65 changes: 54 additions & 11 deletions backend/canisters/registry/impl/src/jobs/check_for_new_snses.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::model::nervous_systems::NervousSystemDetails;
use crate::updates::add_token::add_sns_token;
use crate::{mutate_state, read_state};
use ic_cdk::api::call::{CallResult, RejectionCode};
use ic_cdk::api::management_canister::main::CanisterId;
use sns_wasm_canister::list_deployed_snses::DeployedSns;
use std::collections::HashSet;
use std::time::Duration;
use tracing::info;
use types::Empty;
use tracing::{error, info};
use types::{Empty, TimestampMillis};
use utils::time::HOUR_IN_MS;

const LIFECYCLE_COMMITTED: i32 = 3;
Expand All @@ -26,11 +29,10 @@ async fn run_async() {
let unknown_snses: Vec<_> = read_state(|state| {
let known_snses: HashSet<_> = state
.data
.tokens
.nervous_systems
.get_all()
.iter()
.flat_map(|t| t.nervous_system.as_ref())
.map(|ns| ns.root)
.map(|ns| ns.root_canister_id)
.chain(state.data.failed_sns_launches.iter().copied())
.collect();

Expand All @@ -46,12 +48,9 @@ async fn run_async() {
info!(%root_canister_id, "Getting details of unknown SNS");
if let Some(success) = is_successfully_launched(sns.swap_canister_id.unwrap()).await {
if success {
add_sns_token(
sns.ledger_canister_id.unwrap(),
root_canister_id,
sns.governance_canister_id.unwrap(),
)
.await;
if let Ok(nervous_system) = get_nervous_system_details(sns).await {
add_sns_token(nervous_system).await;
}
} else {
info!(%root_canister_id, "Recording failed SNS launch");
mutate_state(|state| state.data.failed_sns_launches.insert(root_canister_id));
Expand All @@ -72,3 +71,47 @@ async fn is_successfully_launched(sns_swap_canister_id: CanisterId) -> Option<bo
_ => None,
}
}

async fn get_nervous_system_details(sns: DeployedSns) -> CallResult<NervousSystemDetails> {
let metadata = sns_governance_canister_c2c_client::get_metadata(sns.governance_canister_id.unwrap(), &Empty {}).await?;
let parameters =
sns_governance_canister_c2c_client::get_nervous_system_parameters(sns.governance_canister_id.unwrap(), &()).await?;

let now = read_state(|state| state.env.now());

match build_nervous_system_details(&sns, metadata, parameters, now) {
Some(ns) => Ok(ns),
None => {
error!(?sns, "Unable to build NervousSystemDetails due to missing data");
Err((
RejectionCode::Unknown,
"Unable to build NervousSystemDetails due to missing data".to_string(),
))
}
}
}

fn build_nervous_system_details(
sns: &DeployedSns,
metadata: sns_governance_canister::get_metadata::Response,
parameters: sns_governance_canister::get_nervous_system_parameters::Response,
now: TimestampMillis,
) -> Option<NervousSystemDetails> {
Some(NervousSystemDetails {
root_canister_id: sns.root_canister_id?,
governance_canister_id: sns.governance_canister_id?,
swap_canister_id: sns.swap_canister_id?,
ledger_canister_id: sns.ledger_canister_id?,
index_canister_id: sns.index_canister_id?,
name: metadata.name?,
url: metadata.url,
logo: metadata.logo?,
description: metadata.description,
min_dissolve_delay_to_vote: parameters.neuron_minimum_dissolve_delay_to_vote_seconds?,
min_neuron_stake: parameters.neuron_minimum_stake_e8s?,
proposal_rejection_fee: parameters.reject_cost_e8s?,
is_nns: false,
added: now,
last_updated: now,
})
}
52 changes: 39 additions & 13 deletions backend/canisters/registry/impl/src/lib.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions backend/canisters/registry/impl/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn init(args: Args) {
args.nns_ledger_canister_id,
args.nns_root_canister_id,
args.nns_governance_canister_id,
args.nns_index_canister_id,
env.now(),
);

Expand Down
14 changes: 13 additions & 1 deletion backend/canisters/registry/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::lifecycle::{init_env, init_state, UPGRADE_BUFFER_SIZE};
use crate::memory::get_upgrades_memory;
use crate::Data;
use crate::{mutate_state, Data};
use canister_logger::LogEntry;
use canister_tracing_macros::trace;
use ic_cdk_macros::post_upgrade;
use ic_stable_structures::reader::{BufferedReader, Reader};
use registry_canister::post_upgrade::Args;
use tracing::info;
use types::CanisterId;
use utils::cycles::init_cycles_dispenser_client;

#[post_upgrade]
Expand All @@ -24,5 +25,16 @@ fn post_upgrade(args: Args) {
init_cycles_dispenser_client(data.cycles_dispenser_canister_id);
init_state(env, data, args.wasm_version);

let ledger = CanisterId::from_text("ryjl3-tyaaa-aaaaa-aaaba-cai").unwrap();
let root = CanisterId::from_text("r7inp-6aaaa-aaaaa-aaabq-cai").unwrap();
let governance = CanisterId::from_text("rrkah-fqaaa-aaaaa-aaaaq-cai").unwrap();
let index = CanisterId::from_text("qhbym-qaaaa-aaaaa-aaafq-cai").unwrap();

mutate_state(|state| {
state
.data
.add_icp_token_details(ledger, root, governance, index, state.env.now())
});

info!(version = %args.wasm_version, "Post-upgrade complete");
}
1 change: 1 addition & 0 deletions backend/canisters/registry/impl/src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod nervous_systems;
pub mod tokens;
98 changes: 98 additions & 0 deletions backend/canisters/registry/impl/src/model/nervous_systems.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use registry_canister::NervousSystem;
use serde::{Deserialize, Serialize};
use types::{CanisterId, Milliseconds, TimestampMillis};

#[derive(Serialize, Deserialize, Default)]
pub struct NervousSystems {
last_updated: TimestampMillis,
nervous_systems: Vec<NervousSystemDetails>,
}

impl NervousSystems {
pub fn add(&mut self, nervous_system: NervousSystemDetails) -> bool {
if self.exists(nervous_system.root_canister_id) {
false
} else {
self.nervous_systems.push(nervous_system);
true
}
}

pub fn get_all(&self) -> &[NervousSystemDetails] {
&self.nervous_systems
}

pub fn exists(&self, root_canister_id: CanisterId) -> bool {
self.nervous_systems.iter().any(|ns| ns.root_canister_id == root_canister_id)
}
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct NervousSystemDetails {
pub root_canister_id: CanisterId,
pub governance_canister_id: CanisterId,
pub swap_canister_id: CanisterId,
pub ledger_canister_id: CanisterId,
pub index_canister_id: CanisterId,
pub name: String,
pub url: Option<String>,
pub logo: String,
pub description: Option<String>,
pub min_dissolve_delay_to_vote: Milliseconds,
pub min_neuron_stake: u64,
pub proposal_rejection_fee: u64,
pub is_nns: bool,
pub added: TimestampMillis,
pub last_updated: TimestampMillis,
}

impl From<NervousSystemDetails> for NervousSystem {
fn from(value: NervousSystemDetails) -> Self {
NervousSystem {
is_nns: value.is_nns,
root: value.root_canister_id,
governance: value.governance_canister_id,
}
}
}

#[derive(Serialize)]
pub struct NervousSystemMetrics {
root_canister_id: CanisterId,
governance_canister_id: CanisterId,
swap_canister_id: CanisterId,
ledger_canister_id: CanisterId,
index_canister_id: CanisterId,
name: String,
url: Option<String>,
logo_length: usize,
description: Option<String>,
min_dissolve_delay_to_vote: Milliseconds,
min_neuron_stake: u64,
proposal_rejection_fee: u64,
is_nns: bool,
added: TimestampMillis,
last_updated: TimestampMillis,
}

impl From<&NervousSystemDetails> for NervousSystemMetrics {
fn from(value: &NervousSystemDetails) -> Self {
NervousSystemMetrics {
root_canister_id: value.root_canister_id,
governance_canister_id: value.governance_canister_id,
swap_canister_id: value.swap_canister_id,
ledger_canister_id: value.ledger_canister_id,
index_canister_id: value.index_canister_id,
name: value.name.clone(),
url: value.url.clone(),
logo_length: value.logo.len(),
description: value.description.clone(),
min_dissolve_delay_to_vote: value.min_dissolve_delay_to_vote,
min_neuron_stake: value.min_neuron_stake,
proposal_rejection_fee: value.proposal_rejection_fee,
is_nns: value.is_nns,
added: value.added,
last_updated: value.last_updated,
}
}
}
35 changes: 35 additions & 0 deletions backend/canisters/registry/impl/src/model/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,38 @@ impl Tokens {
self.tokens.iter().any(|t| t.ledger_canister_id == ledger_canister_id)
}
}

#[derive(Serialize)]
pub struct TokenMetrics {
ledger_canister_id: CanisterId,
name: String,
symbol: String,
decimals: u8,
fee: u128,
logo_length: usize,
nervous_system: Option<NervousSystem>,
info_url: String,
how_to_buy_url: String,
transaction_url_format: String,
added: TimestampMillis,
last_updated: TimestampMillis,
}

impl From<&TokenDetails> for TokenMetrics {
fn from(value: &TokenDetails) -> Self {
TokenMetrics {
ledger_canister_id: value.ledger_canister_id,
name: value.name.clone(),
symbol: value.symbol.clone(),
decimals: value.decimals,
fee: value.fee,
logo_length: value.logo.len(),
nervous_system: value.nervous_system.clone(),
info_url: value.info_url.clone(),
how_to_buy_url: value.how_to_buy_url.clone(),
transaction_url_format: value.transaction_url_format.clone(),
added: value.added,
last_updated: value.last_updated,
}
}
}
Loading

0 comments on commit 56956b4

Please sign in to comment.