Skip to content

Commit

Permalink
Sync supported standards from each token ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan committed Nov 22, 2023
1 parent a4c9b19 commit ba59d36
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 8 deletions.
6 changes: 3 additions & 3 deletions backend/canisters/community/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ impl RuntimeState {

let owner_count = owners.len() as u128;
let owner_share = (amount_available * 4 / 5) / owner_count;
let amount = owner_share.saturating_sub(gate.fee);

for owner in owners {
let amount = owner_share.saturating_sub(gate.fee);
if amount > 0 {
if amount > 0 {
for owner in owners {
self.data.pending_payments_queue.push(PendingPayment {
amount,
fee: gate.fee,
Expand Down
7 changes: 3 additions & 4 deletions backend/canisters/group/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,9 @@ impl RuntimeState {

let owner_count = owners.len() as u128;
let owner_share = (amount_available * 4 / 5) / owner_count;

for owner in owners {
let amount = owner_share.saturating_sub(gate.fee);
if amount > 0 {
let amount = owner_share.saturating_sub(gate.fee);
if amount > 0 {
for owner in owners {
self.data.pending_payments_queue.push(PendingPayment {
amount,
fee: gate.fee,
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/registry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Avoid reseeding random number generator after each upgrade ([#4755](https://github.com/open-chat-labs/open-chat/pull/4755))
- Update dependencies ([#4770](https://github.com/open-chat-labs/open-chat/pull/4770))
- Regenerate random number generator seed across upgrades ([#4814](https://github.com/open-chat-labs/open-chat/pull/4814))
- Sync `supported standards` from each token ledger ([#4827](https://github.com/open-chat-labs/open-chat/pull/4827))

## [[2.0.918](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.918-registry)] - 2023-10-30

Expand Down
3 changes: 2 additions & 1 deletion backend/canisters/registry/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type TokenDetails = record {
info_url : text;
how_to_buy_url : text;
transaction_url_format : text;
supported_standards : vec text;
added : TimestampMillis;
last_updated : TimestampMillis;
};
Expand All @@ -34,7 +35,7 @@ type NervousSystemSummary = record {
index_canister_id : CanisterId;
is_nns : bool;
proposal_rejection_fee : nat64;
submitting_proposals_enabled: bool;
submitting_proposals_enabled : bool;
};

service : {
Expand Down
2 changes: 2 additions & 0 deletions backend/canisters/registry/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct TokenDetails {
pub info_url: String,
pub how_to_buy_url: String,
pub transaction_url_format: String,
#[serde(default)]
pub supported_standards: Vec<String>,
pub added: TimestampMillis,
pub last_updated: TimestampMillis,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::{mutate_state, read_state};
use candid::Principal;
use ic_cdk::api::call::RejectionCode;
use std::time::Duration;
use utils::canister_timers::run_now_then_interval;
use utils::time::HOUR_IN_MS;

pub fn start_job() {
run_now_then_interval(Duration::from_millis(12 * HOUR_IN_MS), run);
}

fn run() {
ic_cdk::spawn(run_async());
}

async fn run_async() {
let ledger_canister_ids: Vec<_> =
read_state(|state| state.data.tokens.get_all().iter().map(|t| t.ledger_canister_id).collect());

for id in ledger_canister_ids {
if let Ok(supported_standards) = get_supported_standards(id).await {
mutate_state(|state| state.data.tokens.set_standards(id, supported_standards, state.env.now()));
}
}
}

async fn get_supported_standards(ledger_canister_id: Principal) -> Result<Vec<String>, (RejectionCode, String)> {
let result = icrc_ledger_canister_c2c_client::icrc1_supported_standards(ledger_canister_id).await?;
Ok(result.into_iter().map(|r| r.name).collect())
}
2 changes: 2 additions & 0 deletions backend/canisters/registry/impl/src/jobs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::RuntimeState;

pub mod check_for_sns_updates;
pub mod check_for_updates_to_supported_standards;

pub(crate) fn start(_state: &RuntimeState) {
check_for_sns_updates::start_job();
check_for_updates_to_supported_standards::start_job();
}
10 changes: 10 additions & 0 deletions backend/canisters/registry/impl/src/model/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Tokens {
transaction_url_format,
added: now,
last_updated: now,
supported_standards: Vec::new(),
});
self.last_updated = now;
true
Expand Down Expand Up @@ -82,6 +83,15 @@ impl Tokens {
}
}

pub fn set_standards(&mut self, ledger_canister_id: CanisterId, supported_standards: Vec<String>, now: TimestampMillis) {
if let Some(token) = self.tokens.iter_mut().find(|t| t.ledger_canister_id == ledger_canister_id) {
if token.supported_standards != supported_standards {
token.supported_standards = supported_standards;
token.last_updated = now;
}
}
}

pub fn last_updated(&self) -> TimestampMillis {
self.last_updated
}
Expand Down

0 comments on commit ba59d36

Please sign in to comment.