Skip to content

Commit

Permalink
Include the total cycles topped up (#7056)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Dec 13, 2024
1 parent 719d306 commit b32e38a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 22 deletions.
4 changes: 4 additions & 0 deletions backend/canisters/local_group_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -25,13 +26,20 @@ fn http_request(request: HttpRequest) -> HttpResponse {
fn get_top_ups(qs: HashMap<String, String>, 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::<u128>() 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) {
Expand All @@ -43,3 +51,9 @@ fn http_request(request: HttpRequest) -> HttpResponse {
_ => HttpResponse::not_found(),
}
}

#[derive(Serialize)]
struct TopUps {
total: f64,
top_ups: Vec<CyclesTopUpHumanReadable>,
}
1 change: 1 addition & 0 deletions backend/canisters/local_user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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::<u128>() 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 {
Expand Down Expand Up @@ -83,3 +88,9 @@ struct UserCanisterVersion {
count: u32,
users: Vec<UserId>,
}

#[derive(Serialize)]
struct TopUps {
total: f64,
top_ups: Vec<CyclesTopUpHumanReadable>,
}
13 changes: 0 additions & 13 deletions backend/libraries/constants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!(
Expand Down
34 changes: 33 additions & 1 deletion backend/libraries/types/src/cycles.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::TimestampMillis;
use candid::CandidType;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize, Serializer};

pub type Cycles = u128;

Expand Down Expand Up @@ -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<Cycles> for CyclesHumanReadable {
fn from(value: Cycles) -> Self {
CyclesHumanReadable(value)
}
}

impl Serialize for CyclesHumanReadable {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("{}T", self.0 as f64 / 1_000_000_000_000.0))
}
}

0 comments on commit b32e38a

Please sign in to comment.