From b32e38a41c411f010f3f39d7263bb8602bdba295 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Fri, 13 Dec 2024 08:53:43 +0000 Subject: [PATCH] Include the total cycles topped up (#7056) --- .../canisters/local_group_index/CHANGELOG.md | 4 +++ .../impl/src/queries/http_request.rs | 26 ++++++++++---- .../canisters/local_user_index/CHANGELOG.md | 1 + .../impl/src/queries/http_request.rs | 15 ++++++-- backend/libraries/constants/src/lib.rs | 13 ------- backend/libraries/types/src/cycles.rs | 34 ++++++++++++++++++- 6 files changed, 71 insertions(+), 22 deletions(-) diff --git a/backend/canisters/local_group_index/CHANGELOG.md b/backend/canisters/local_group_index/CHANGELOG.md index 9bfd8811fa..6138510a9b 100644 --- a/backend/canisters/local_group_index/CHANGELOG.md +++ b/backend/canisters/local_group_index/CHANGELOG.md @@ -10,6 +10,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Expose the cycles top-ups of Group/Community canisters ([#7053](https://github.com/open-chat-labs/open-chat/pull/7053)) +### Changed + +- Include the total cycles topped up ([#7056](https://github.com/open-chat-labs/open-chat/pull/7056)) + ## [[2.0.1505](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1505-local_group_index)] - 2024-12-09 ### Changed diff --git a/backend/canisters/local_group_index/impl/src/queries/http_request.rs b/backend/canisters/local_group_index/impl/src/queries/http_request.rs index 2e2d864c94..429990042d 100644 --- a/backend/canisters/local_group_index/impl/src/queries/http_request.rs +++ b/backend/canisters/local_group_index/impl/src/queries/http_request.rs @@ -1,8 +1,9 @@ use crate::{read_state, RuntimeState}; use http_request::{build_json_response, encode_logs, extract_route, Route}; use ic_cdk::query; +use serde::Serialize; use std::collections::HashMap; -use types::{CanisterId, HttpRequest, HttpResponse, TimestampMillis}; +use types::{CanisterId, CyclesTopUpHumanReadable, HttpRequest, HttpResponse, TimestampMillis}; #[query] fn http_request(request: HttpRequest) -> HttpResponse { @@ -25,13 +26,20 @@ fn http_request(request: HttpRequest) -> HttpResponse { fn get_top_ups(qs: HashMap, state: &RuntimeState) -> HttpResponse { let canister_id = CanisterId::from_text(qs.get("canister_id").unwrap()).unwrap(); - if let Some(group) = state.data.local_groups.get(&canister_id.into()) { - build_json_response(&group.cycle_top_ups) + let top_ups = if let Some(group) = state.data.local_groups.get(&canister_id.into()) { + &group.cycle_top_ups } else if let Some(community) = state.data.local_communities.get(&canister_id.into()) { - build_json_response(&community.cycle_top_ups) + &community.cycle_top_ups } else { - HttpResponse::not_found() - } + return HttpResponse::not_found(); + }; + + let total = top_ups.iter().map(|c| c.amount).sum::() as f64 / 1_000_000_000_000f64; + + build_json_response(&TopUps { + total, + top_ups: top_ups.iter().map(|c| c.into()).collect(), + }) } match extract_route(&request.url) { @@ -43,3 +51,9 @@ fn http_request(request: HttpRequest) -> HttpResponse { _ => HttpResponse::not_found(), } } + +#[derive(Serialize)] +struct TopUps { + total: f64, + top_ups: Vec, +} diff --git a/backend/canisters/local_user_index/CHANGELOG.md b/backend/canisters/local_user_index/CHANGELOG.md index 0ee881feb0..c293329236 100644 --- a/backend/canisters/local_user_index/CHANGELOG.md +++ b/backend/canisters/local_user_index/CHANGELOG.md @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Switch to using `PrincipalToStableMemoryMap` ([#7023](https://github.com/open-chat-labs/open-chat/pull/7023)) - Make `MessageId` comparisons use their 64bit representation ([#7030](https://github.com/open-chat-labs/open-chat/pull/7030)) - Notify CHIT updates via LocalUserIndex ([#7033](https://github.com/open-chat-labs/open-chat/pull/7033)) +- Include the total cycles topped up ([#7056](https://github.com/open-chat-labs/open-chat/pull/7056)) ### Fixes diff --git a/backend/canisters/local_user_index/impl/src/queries/http_request.rs b/backend/canisters/local_user_index/impl/src/queries/http_request.rs index 500b1b38f7..8da1be42cc 100644 --- a/backend/canisters/local_user_index/impl/src/queries/http_request.rs +++ b/backend/canisters/local_user_index/impl/src/queries/http_request.rs @@ -4,7 +4,7 @@ use ic_cdk::query; use serde::Serialize; use std::collections::{BTreeMap, HashMap}; use std::str::FromStr; -use types::{BuildVersion, CanisterId, HttpRequest, HttpResponse, TimestampMillis, UserId}; +use types::{BuildVersion, CanisterId, CyclesTopUpHumanReadable, HttpRequest, HttpResponse, TimestampMillis, UserId}; #[query] fn http_request(request: HttpRequest) -> HttpResponse { @@ -31,7 +31,12 @@ fn http_request(request: HttpRequest) -> HttpResponse { return HttpResponse::not_found(); }; - build_json_response(&user.cycle_top_ups) + let total = user.cycle_top_ups.iter().map(|c| c.amount).sum::() as f64 / 1_000_000_000_000f64; + + build_json_response(&TopUps { + total, + top_ups: user.cycle_top_ups.iter().map(|c| c.into()).collect(), + }) } fn get_user_canister_versions(state: &RuntimeState) -> HttpResponse { @@ -83,3 +88,9 @@ struct UserCanisterVersion { count: u32, users: Vec, } + +#[derive(Serialize)] +struct TopUps { + total: f64, + top_ups: Vec, +} diff --git a/backend/libraries/constants/src/lib.rs b/backend/libraries/constants/src/lib.rs index 26e53c3e96..2543761c07 100644 --- a/backend/libraries/constants/src/lib.rs +++ b/backend/libraries/constants/src/lib.rs @@ -32,11 +32,6 @@ pub const SNS_ROOT_CANISTER_ID: CanisterId = Principal::from_slice(&[0, 0, 0, 0, pub const SNS_GOVERNANCE_CANISTER_ID: CanisterId = Principal::from_slice(&[0, 0, 0, 0, 2, 0, 0, 24, 1, 1]); pub const SNS_LEDGER_CANISTER_ID: CanisterId = Principal::from_slice(&[0, 0, 0, 0, 2, 0, 0, 25, 1, 1]); -pub const DEV_TEAM_DFX_PRINCIPAL: CanisterId = Principal::from_slice(&[ - 143, 216, 195, 195, 27, 134, 102, 49, 184, 154, 196, 117, 143, 40, 192, 164, 121, 209, 89, 30, 45, 18, 30, 32, 92, 106, - 138, 30, 2, -]); - pub const IC_ROOT_KEY: &[u8; 133] = b"\x30\x81\x82\x30\x1d\x06\x0d\x2b\x06\x01\x04\x01\x82\xdc\x7c\x05\x03\x01\x02\x01\x06\x0c\x2b\x06\x01\x04\x01\x82\xdc\x7c\x05\x03\x02\x01\x03\x61\x00\x81\x4c\x0e\x6e\xc7\x1f\xab\x58\x3b\x08\xbd\x81\x37\x3c\x25\x5c\x3c\x37\x1b\x2e\x84\x86\x3c\x98\xa4\xf1\xe0\x8b\x74\x23\x5d\x14\xfb\x5d\x9c\x0c\xd5\x46\xd9\x68\x5f\x91\x3a\x0c\x0b\x2c\xc5\x34\x15\x83\xbf\x4b\x43\x92\xe4\x67\xdb\x96\xd6\x5b\x9b\xb4\xcb\x71\x71\x12\xf8\x47\x2e\x0d\x5a\x4d\x14\x50\x5f\xfd\x74\x84\xb0\x12\x91\x09\x1c\x5f\x87\xb9\x88\x83\x46\x3f\x98\x09\x1a\x0b\xaa\xae"; pub const MEMO_MESSAGE: [u8; 6] = [0x4f, 0x43, 0x5f, 0x4d, 0x53, 0x47]; // OC_MSG @@ -113,14 +108,6 @@ mod tests { ); } - #[test] - fn dev_team_dfx_principal() { - assert_eq!( - DEV_TEAM_DFX_PRINCIPAL, - Principal::from_text("tu45y-p4p3d-b4gg4-gmyy3-rgweo-whsrq-fephi-vshrn-cipca-xdkri-pae").unwrap() - ); - } - #[test] fn openchat_treasury_canister_id() { assert_eq!( diff --git a/backend/libraries/types/src/cycles.rs b/backend/libraries/types/src/cycles.rs index 766cbac820..03389c43a6 100644 --- a/backend/libraries/types/src/cycles.rs +++ b/backend/libraries/types/src/cycles.rs @@ -1,6 +1,6 @@ use crate::TimestampMillis; use candid::CandidType; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, Serializer}; pub type Cycles = u128; @@ -45,3 +45,35 @@ pub enum NotifyLowBalanceResponse { NotEnoughCyclesRemaining, FailedToDepositCycles, } + +pub struct CyclesHumanReadable(Cycles); + +#[derive(Serialize)] +pub struct CyclesTopUpHumanReadable { + date: TimestampMillis, + amount: CyclesHumanReadable, +} + +impl From<&CyclesTopUp> for CyclesTopUpHumanReadable { + fn from(value: &CyclesTopUp) -> Self { + CyclesTopUpHumanReadable { + date: value.date, + amount: value.amount.into(), + } + } +} + +impl From for CyclesHumanReadable { + fn from(value: Cycles) -> Self { + CyclesHumanReadable(value) + } +} + +impl Serialize for CyclesHumanReadable { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(&format!("{}T", self.0 as f64 / 1_000_000_000_000.0)) + } +}