-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to save named cryptocurrency accounts (#4434)
- Loading branch information
Showing
16 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
backend/canisters/user/api/src/queries/saved_crypto_accounts.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
backend/canisters/user/api/src/updates/save_crypto_account.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
backend/canisters/user/impl/src/queries/saved_crypto_accounts.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
backend/canisters/user/impl/src/updates/save_crypto_account.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
backend/integration_tests/src/save_crypto_account_tests.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |