Skip to content

Commit

Permalink
Merge branch 'master' into enable-human
Browse files Browse the repository at this point in the history
  • Loading branch information
julianjelfs committed Jul 25, 2024
2 parents 93c454f + aa09b45 commit 25f17db
Show file tree
Hide file tree
Showing 50 changed files with 648 additions and 241 deletions.
1 change: 1 addition & 0 deletions backend/canisters/community/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/).
### Added

- Add `Locked` gate ([#6095](https://github.com/open-chat-labs/open-chat/pull/6095))
- Add `Invited` gate ([#6106](https://github.com/open-chat-labs/open-chat/pull/6106))

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use community_canister::add_members_to_channel::{Response::*, *};
use gated_groups::{check_if_passes_gate, CheckGateArgs, CheckIfPassesGateResult};
use group_chat_core::AddResult;
use ic_cdk::update;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::iter::zip;
use types::{
AccessGate, AddedToChannelNotification, CanisterId, ChannelId, EventIndex, MembersAdded, MessageIndex, Notification,
Expand Down Expand Up @@ -57,6 +57,7 @@ async fn add_members_to_channel(args: Args) -> Response {
this_canister: prepare_result.this_canister,
unique_person_proof: None,
verified_credential_args: None,
is_user_invited: prepare_result.invited.contains(user_id),
now: prepare_result.now_nanos,
},
)
Expand Down Expand Up @@ -106,6 +107,7 @@ struct PrepareResult {
member_display_name: Option<String>,
this_canister: CanisterId,
now_nanos: TimestampNanos,
invited: HashSet<UserId>,
}

#[allow(clippy::result_large_err)]
Expand Down Expand Up @@ -148,6 +150,7 @@ fn prepare(args: &Args, state: &RuntimeState) -> Result<PrepareResult, Response>
member_display_name: member.display_name().value.clone(),
this_canister: state.env.canister_id(),
now_nanos: state.env.now_nanos(),
invited: channel.chat.invited_users.users().into_iter().collect(),
})
} else {
Err(UserNotInChannel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ fn is_permitted_to_join(
ii_canister_id: state.data.internet_identity_canister_id,
ii_origin: vc.ii_origin,
}),
is_user_invited: channel.chat.invited_users.contains(&member.user_id),
now: state.env.now(),
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fn is_permitted_to_join(args: &Args, state: &RuntimeState) -> Result<Option<(Acc
ii_origin: vc.ii_origin.clone(),
}
}),
is_user_invited: state.data.invited_users.contains(&args.user_id),
now: state.env.now(),
},
)
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/group/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/).
### Added

- Add `Locked` gate ([#6095](https://github.com/open-chat-labs/open-chat/pull/6095))
- Add `Invited` gate ([#6106](https://github.com/open-chat-labs/open-chat/pull/6106))

### Changed

Expand Down
1 change: 1 addition & 0 deletions backend/canisters/group/impl/src/updates/c2c_join_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn is_permitted_to_join(args: &Args, state: &RuntimeState) -> Result<Option<(Acc
ii_origin: vc.ii_origin.clone(),
}
}),
is_user_invited: state.data.chat.invited_users.contains(&args.user_id),
now: state.env.now(),
},
)
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/group_index/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/).
### Added

- Add `Locked` gate ([#6095](https://github.com/open-chat-labs/open-chat/pull/6095))
- Add `Invited` gate ([#6106](https://github.com/open-chat-labs/open-chat/pull/6106))

### Changed

Expand Down
3 changes: 3 additions & 0 deletions backend/canisters/group_index/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ pub struct AccessGateMetrics {
pub composite: u32,
#[serde(default)]
pub locked: u32,
#[serde(default)]
pub invited: u32,
}

impl AccessGateMetrics {
Expand All @@ -396,6 +398,7 @@ impl AccessGateMetrics {
AccessGate::TokenBalance(_) => self.token_balance += 1,
AccessGate::Composite(_) => self.composite += 1,
AccessGate::Locked => self.locked += 1,
AccessGate::Invited => self.invited += 1,
}
}
}
1 change: 1 addition & 0 deletions backend/canisters/local_group_index/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/).
### Added

- Add `Locked` gate ([#6095](https://github.com/open-chat-labs/open-chat/pull/6095))
- Add `Invited` gate ([#6106](https://github.com/open-chat-labs/open-chat/pull/6106))

### Changed

Expand Down
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 @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Support submitting proof of uniqueness to LocalUserIndex ([#6068](https://github.com/open-chat-labs/open-chat/pull/6068))
- Support submitting proof of diamond membership to LocalUserIndex ([#6084](https://github.com/open-chat-labs/open-chat/pull/6084))
- Add `Locked` gate ([#6095](https://github.com/open-chat-labs/open-chat/pull/6095))
- Add `Invited` gate ([#6106](https://github.com/open-chat-labs/open-chat/pull/6106))

### Changed

Expand Down
4 changes: 4 additions & 0 deletions backend/canisters/registry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Changed

- Simplify adding tokens and add compatibility with `burn_fee` ([#6102](https://github.com/open-chat-labs/open-chat/pull/6102))

## [[2.0.1193](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1193-registry)] - 2024-06-06

### Fixed
Expand Down
46 changes: 21 additions & 25 deletions backend/canisters/registry/impl/src/jobs/check_for_token_updates.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::metadata_helper::MetadataHelper;
use crate::{mutate_state, read_state};
use ic_cdk::api::call::RejectionCode;
use icrc_ledger_types::icrc::generic_metadata_value::MetadataValue;
use std::time::Duration;
use tracing::error;
use types::CanisterId;
use utils::canister_timers::run_now_then_interval;
use utils::time::HOUR_IN_MS;
Expand All @@ -22,34 +23,29 @@ async fn run_async() {

async fn check_for_token_updates(ledger_canister_id: CanisterId) -> Result<(), (RejectionCode, String)> {
let metadata = icrc_ledger_canister_c2c_client::icrc1_metadata(ledger_canister_id).await?;
let metadata_helper = match MetadataHelper::try_parse(metadata) {
Ok(h) => h,
Err(reason) => {
let error = format!("Token metadata is incomplete: {reason}");
error!(%ledger_canister_id, error);
return Err((RejectionCode::Unknown, error));
}
};

mutate_state(|state| {
if let Some(token) = state.data.tokens.get(ledger_canister_id).cloned() {
let mut args = registry_canister::update_token::Args::new(ledger_canister_id);
for (name, value) in metadata {
match name.as_str() {
"icrc1:logo" => {
if let MetadataValue::Text(logo) = value {
if logo != token.logo {
args.logo = Some(logo);
}
}
}
"icrc1:name" => {
if let MetadataValue::Text(name) = value {
if name != token.name {
args.name = Some(name);
}
}
}
"icrc1:symbol" => {
if let MetadataValue::Text(symbol) = value {
if symbol != token.symbol {
args.symbol = Some(symbol);
}
}
}
_ => {}
if *metadata_helper.name() != token.name {
args.name = Some(metadata_helper.name().to_string());
}

if *metadata_helper.symbol() != token.symbol {
args.symbol = Some(metadata_helper.symbol().to_string());
}

if let Some(logo) = metadata_helper.logo().cloned() {
if logo != token.logo {
args.logo = Some(logo);
}
}

Expand Down
1 change: 1 addition & 0 deletions backend/canisters/registry/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod guards;
mod jobs;
mod lifecycle;
mod memory;
mod metadata_helper;
mod model;
mod queries;
mod updates;
Expand Down
77 changes: 77 additions & 0 deletions backend/canisters/registry/impl/src/metadata_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use candid::Nat;
use icrc_ledger_types::icrc::generic_metadata_value::MetadataValue;

pub struct MetadataHelper {
name: String,
symbol: String,
decimals: u8,
fee: u128,
logo: Option<String>,
is_icrc1_compatible: bool,
}

impl MetadataHelper {
pub fn try_parse(metadata: Vec<(String, MetadataValue)>) -> Result<MetadataHelper, String> {
let mut name = None;
let mut symbol = None;
let mut decimals = None;
let mut fee = None;
let mut burn_fee = None;
let mut logo = None;
let mut is_icrc1_compatible = true;

for (key, value) in metadata {
match (key.as_str(), value) {
("icrc1:name", MetadataValue::Text(s)) => name = Some(s),
("icrc1:symbol", MetadataValue::Text(s)) => symbol = Some(s),
("icrc1:decimals", MetadataValue::Nat(n)) => decimals = u8::try_from(n.0).ok(),
("icrc1:fee", MetadataValue::Nat(n)) => fee = u128::try_from(n.0).ok(),
("icrc1:burn_fee", MetadataValue::Nat(n)) => burn_fee = u128::try_from(n.0).ok(),
("icrc1:logo", MetadataValue::Text(s)) => logo = Some(s),
("icrc1:transfer_fee_rate" | "icrc1:burn_fee_rate", MetadataValue::Nat(n)) if n > Nat::default() => {
is_icrc1_compatible = false
}
_ => {}
}
}

match (name, symbol, decimals, fee) {
(Some(n), Some(s), Some(d), Some(f)) => Ok(MetadataHelper {
name: n,
symbol: s,
decimals: d,
fee: f + burn_fee.unwrap_or_default(),
logo,
is_icrc1_compatible,
}),
(None, ..) => Err("Name not found".to_string()),
(_, None, ..) => Err("Symbol not found".to_string()),
(.., None, _) => Err("Decimals not found".to_string()),
(.., None) => Err("Fee not found".to_string()),
}
}

pub fn name(&self) -> &str {
&self.name
}

pub fn symbol(&self) -> &str {
&self.symbol
}

pub fn decimals(&self) -> u8 {
self.decimals
}

pub fn fee(&self) -> u128 {
self.fee
}

pub fn logo(&self) -> Option<&String> {
self.logo.as_ref()
}

pub fn is_icrc1_compatible(&self) -> bool {
self.is_icrc1_compatible
}
}
Loading

0 comments on commit 25f17db

Please sign in to comment.