Skip to content

Commit

Permalink
Allow users to save named cryptocurrency accounts (#4434)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Sep 25, 2023
1 parent 2f16e62 commit 0f49327
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/canisters/user/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/).
- Implement tipping messages ([#4420](https://github.com/open-chat-labs/open-chat/pull/4420))
- Implement notifications for message tips ([#4427](https://github.com/open-chat-labs/open-chat/pull/4427))
- Add `followed_by_me` to the thread summary returned in GroupChatSummary ([#4431](https://github.com/open-chat-labs/open-chat/pull/4431))
- Allow users to save named cryptocurrency accounts ([#4434](https://github.com/open-chat-labs/open-chat/pull/4434))

## [[2.0.852](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.852-user)] - 2023-09-18

Expand Down
18 changes: 18 additions & 0 deletions backend/canisters/user/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,22 @@ type ArchiveUnarchiveChatsResponse = variant {
UserSuspended;
};

type NamedAccount = record {
name : text;
account : text;
};

type SaveCryptoAccountResponse = variant {
Success;
Invalid;
NameTaken;
UserSuspended;
};

type SavedCryptoAccountsResponse = variant {
Success : vec NamedAccount;
};

type InitUserPrincipalMigrationArgs = record {
new_principal : principal;
};
Expand Down Expand Up @@ -851,6 +867,7 @@ service : {
unpin_chat_v2 : (UnpinChatV2Request) -> (UnpinChatV2Response);
manage_favourite_chats : (ManageFavouriteChatsArgs) -> (ManageFavouriteChatsResponse);
archive_unarchive_chats : (ArchiveUnarchiveChatsArgs) -> (ArchiveUnarchiveChatsResponse);
save_crypto_account : (NamedAccount) -> (SaveCryptoAccountResponse);

init_user_principal_migration : (InitUserPrincipalMigrationArgs) -> (InitUserPrincipalMigrationResponse);
migrate_user_principal : (MigrateUserPrincipalArgs) -> (MigrateUserPrincipalResponse);
Expand All @@ -868,4 +885,5 @@ service : {
contacts : (ContactsArgs) -> (ContactsResponse) query;
public_profile : (PublicProfileArgs) -> (PublicProfileResponse) query;
hot_group_exclusions : (HotGroupExclusionsArgs) -> (HotGroupExclusionsResponse) query;
saved_crypto_accounts : (EmptyArgs) -> (SavedCryptoAccountsResponse) query;
};
6 changes: 6 additions & 0 deletions backend/canisters/user/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,9 @@ pub enum ChatInList {
Favourite(Chat),
Community(CommunityId, ChannelId),
}

#[derive(CandidType, Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct NamedAccount {
pub name: String,
pub account: String,
}
2 changes: 2 additions & 0 deletions backend/canisters/user/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn main() {
generate_candid_method!(user, messages_by_message_index, query);
generate_candid_method!(user, public_profile, query);
generate_candid_method!(user, search_messages, query);
generate_candid_method!(user, saved_crypto_accounts, query);
generate_candid_method!(user, updates, query);

generate_candid_method!(user, add_hot_group_exclusions, update);
Expand All @@ -35,6 +36,7 @@ fn main() {
generate_candid_method!(user, mute_notifications, update);
generate_candid_method!(user, pin_chat_v2, update);
generate_candid_method!(user, remove_reaction, update);
generate_candid_method!(user, save_crypto_account, update);
generate_candid_method!(user, send_message_with_transfer_to_channel, update);
generate_candid_method!(user, send_message_with_transfer_to_group, update);
generate_candid_method!(user, send_message_v2, update);
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user/api/src/queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ pub mod hot_group_exclusions;
pub mod initial_state;
pub mod messages_by_message_index;
pub mod public_profile;
pub mod saved_crypto_accounts;
pub mod search_messages;
pub mod updates;
11 changes: 11 additions & 0 deletions backend/canisters/user/api/src/queries/saved_crypto_accounts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::NamedAccount;
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::Empty;

pub type Args = Empty;

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success(Vec<NamedAccount>),
}
1 change: 1 addition & 0 deletions backend/canisters/user/api/src/updates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod migrate_user_principal;
pub mod mute_notifications;
pub mod pin_chat_v2;
pub mod remove_reaction;
pub mod save_crypto_account;
pub mod send_message_v2;
pub mod send_message_with_transfer_to_channel;
pub mod send_message_with_transfer_to_group;
Expand Down
13 changes: 13 additions & 0 deletions backend/canisters/user/api/src/updates/save_crypto_account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::NamedAccount;
use candid::CandidType;
use serde::{Deserialize, Serialize};

pub type Args = NamedAccount;

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success,
Invalid,
NameTaken,
UserSuspended,
}
4 changes: 4 additions & 0 deletions backend/canisters/user/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use types::{
BuildVersion, CanisterId, Chat, ChatId, ChatMetrics, CommunityId, Cryptocurrency, Cycles, Document, Notification,
TimestampMillis, Timestamped, UserId,
};
use user_canister::NamedAccount;
use utils::env::Environment;
use utils::regular_jobs::RegularJobs;

Expand Down Expand Up @@ -152,6 +153,8 @@ struct Data {
pub contacts: Contacts,
pub diamond_membership_expires_at: Option<TimestampMillis>,
pub fire_and_forget_handler: FireAndForgetHandler,
#[serde(default)]
pub saved_crypto_accounts: Vec<NamedAccount>,
}

impl Data {
Expand Down Expand Up @@ -195,6 +198,7 @@ impl Data {
contacts: Contacts::default(),
diamond_membership_expires_at: None,
fire_and_forget_handler: FireAndForgetHandler::default(),
saved_crypto_accounts: Vec::new(),
}
}

Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user/impl/src/queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ pub mod http_request;
pub mod initial_state;
pub mod messages_by_message_index;
pub mod public_profile;
pub mod saved_crypto_accounts;
pub mod search_messages;
pub mod updates;
12 changes: 12 additions & 0 deletions backend/canisters/user/impl/src/queries/saved_crypto_accounts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::{read_state, RuntimeState};
use ic_cdk_macros::query;
use user_canister::saved_crypto_accounts::{Response::*, *};

#[query]
fn saved_crypto_accounts(_args: Args) -> Response {
read_state(saved_crypto_accounts_impl)
}

fn saved_crypto_accounts_impl(state: &RuntimeState) -> Response {
Success(state.data.saved_crypto_accounts.clone())
}
1 change: 1 addition & 0 deletions backend/canisters/user/impl/src/updates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod migrate_user_principal;
pub mod mute_notifications;
pub mod pin_chat_v2;
pub mod remove_reaction;
pub mod save_crypto_account;
pub mod send_message;
pub mod send_message_with_transfer;
pub mod set_avatar;
Expand Down
43 changes: 43 additions & 0 deletions backend/canisters/user/impl/src/updates/save_crypto_account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::guards::caller_is_owner;
use crate::{mutate_state, run_regular_jobs, RuntimeState};
use candid::Principal;
use canister_tracing_macros::trace;
use ic_cdk_macros::update;
use ic_ledger_types::AccountIdentifier;
use user_canister::save_crypto_account::{Response::*, *};

#[update(guard = "caller_is_owner")]
#[trace]
fn save_crypto_account(args: Args) -> Response {
run_regular_jobs();

mutate_state(|state| save_crypto_account_impl(args, state))
}

fn save_crypto_account_impl(mut args: Args, state: &mut RuntimeState) -> Response {
if state.data.suspended.value {
return UserSuspended;
}

args.name = args.name.trim().to_string();
args.name.truncate(25);
args.account = args.account.trim().to_string();

let valid = Principal::from_text(&args.account).is_ok() || AccountIdentifier::from_hex(&args.account).is_ok();

if valid {
for named_account in state.data.saved_crypto_accounts.iter_mut() {
if named_account.account == args.account {
named_account.name = args.name;
return Success;
}
if named_account.name == args.name {
return NameTaken;
}
}
state.data.saved_crypto_accounts.push(args);
Success
} else {
Invalid
}
}
2 changes: 2 additions & 0 deletions backend/integration_tests/src/client/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use user_canister::*;
generate_query_call!(events);
generate_query_call!(events_by_index);
generate_query_call!(initial_state);
generate_query_call!(saved_crypto_accounts);
generate_query_call!(updates);

// Updates
Expand All @@ -22,6 +23,7 @@ generate_update_call!(leave_group);
generate_update_call!(mark_read);
generate_update_call!(mute_notifications);
generate_update_call!(remove_reaction);
generate_update_call!(save_crypto_account);
generate_update_call!(send_message_v2);
generate_update_call!(send_message_with_transfer_to_channel);
generate_update_call!(send_message_with_transfer_to_group);
Expand Down
1 change: 1 addition & 0 deletions backend/integration_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod register_user_tests;
mod registry_tests;
mod remove_from_group_tests;
mod rng;
mod save_crypto_account_tests;
mod send_crypto_tests;
mod send_direct_message_tests;
mod set_message_reminder_tests;
Expand Down
26 changes: 26 additions & 0 deletions backend/integration_tests/src/save_crypto_account_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::env::ENV;
use crate::rng::{random_principal, random_string};
use crate::{client, TestEnv};
use std::ops::Deref;
use types::Empty;
use user_canister::NamedAccount;

#[test]
fn save_crypto_account_succeeds() {
let mut wrapper = ENV.deref().get();
let TestEnv { env, canister_ids, .. } = wrapper.env();

let user = client::local_user_index::happy_path::register_user(env, canister_ids.local_user_index);
let name = random_string();
let account = random_principal().to_string();

let named_account = NamedAccount { name, account };

let response = client::user::save_crypto_account(env, user.principal, user.canister(), &named_account);
assert!(matches!(response, user_canister::save_crypto_account::Response::Success));

let user_canister::saved_crypto_accounts::Response::Success(accounts) =
client::user::saved_crypto_accounts(env, user.principal, user.canister(), &Empty {});

assert_eq!(accounts, vec![named_account]);
}

0 comments on commit 0f49327

Please sign in to comment.