Skip to content

Commit

Permalink
Set/store user's wallet configuration (#6242)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Aug 14, 2024
1 parent 8c9a275 commit 8d6207f
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 4 deletions.
4 changes: 4 additions & 0 deletions backend/canisters/user/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]

### Added

- Set/store user's wallet configuration ([#6242](https://github.com/open-chat-labs/open-chat/pull/6242))

### Changed

- Configure message visibility to non-members of public channels/groups ([#6152](https://github.com/open-chat-labs/open-chat/pull/6152))
Expand Down
24 changes: 24 additions & 0 deletions backend/canisters/user/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ type InitialStateResponse = variant {
streak_ends : TimestampMillis;
next_daily_claim : TimestampMillis;
is_unique_person : bool;
wallet_config: WalletConfig;
};
};

Expand Down Expand Up @@ -948,6 +949,7 @@ type UpdatesResponse = variant {
streak_ends : TimestampMillis;
next_daily_claim : TimestampMillis;
is_unique_person : opt bool;
wallet_config: opt WalletConfig;
};
SuccessNoUpdates;
};
Expand Down Expand Up @@ -1189,6 +1191,27 @@ type RetrieveBtcResponse = variant {
InternalError : text;
};

type ConfigureWalletArgs = record {
config : WalletConfig;
};

type WalletConfig = variant {
Auto : AutoWallet;
Manual : ManualWallet;
};

type AutoWallet = record {
min_cents_visible : nat32;
};

type ManualWallet = record {
tokens : vec CanisterId;
};

type ConfigureWalletResponse = variant {
Success;
};

service : {
send_message_v2 : (SendMessageV2Args) -> (SendMessageResponse);
edit_message_v2 : (EditMessageV2Args) -> (EditMessageResponse);
Expand Down Expand Up @@ -1236,6 +1259,7 @@ service : {
join_video_call : (JoinVideoCallArgs) -> (JoinVideoCallResponse);
end_video_call : (EndVideoCallArgs) -> (EndVideoCallResponse);
claim_daily_chit : (EmptyArgs) -> (ClaimDailyChitResponse);
configure_wallet : (ConfigureWalletArgs) -> (ConfigureWalletResponse);

events : (EventsArgs) -> (EventsResponse) query;
events_by_index : (EventsByIndexArgs) -> (EventsResponse) query;
Expand Down
22 changes: 22 additions & 0 deletions backend/canisters/user/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,25 @@ pub struct NamedAccount {
pub name: String,
pub account: String,
}

#[derive(CandidType, Serialize, Deserialize, Clone, Debug)]
pub enum WalletConfig {
Auto(AutoWallet),
Manual(ManualWallet),
}

#[derive(CandidType, Serialize, Deserialize, Clone, Debug)]
pub struct AutoWallet {
min_cents_visible: u32,
}

#[derive(CandidType, Serialize, Deserialize, Clone, Debug)]
pub struct ManualWallet {
tokens: Vec<CanisterId>,
}

impl Default for WalletConfig {
fn default() -> Self {
WalletConfig::Auto(AutoWallet { min_cents_visible: 100 })
}
}
1 change: 1 addition & 0 deletions backend/canisters/user/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn main() {
generate_candid_method!(user, cancel_message_reminder, update);
generate_candid_method!(user, cancel_p2p_swap, update);
generate_candid_method!(user, claim_daily_chit, update);
generate_candid_method!(user, configure_wallet, update);
generate_candid_method!(user, create_community, update);
generate_candid_method!(user, create_group, update);
generate_candid_method!(user, delete_community, update);
Expand Down
3 changes: 3 additions & 0 deletions backend/canisters/user/api/src/queries/initial_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::{CanisterId, Chat, ChatId, ChitEarned, DirectChatSummary, Empty, GroupChatSummary, TimestampMillis, UserId};

use crate::WalletConfig;

pub type Args = Empty;

#[derive(CandidType, Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -29,6 +31,7 @@ pub struct SuccessResult {
pub streak_ends: TimestampMillis,
pub next_daily_claim: TimestampMillis,
pub is_unique_person: bool,
pub wallet_config: WalletConfig,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
Expand Down
3 changes: 2 additions & 1 deletion backend/canisters/user/api/src/queries/updates.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::initial_state::PinNumberSettings;
use crate::{initial_state::PinNumberSettings, WalletConfig};
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::{
Expand Down Expand Up @@ -38,6 +38,7 @@ pub struct SuccessResult {
pub streak_ends: TimestampMillis,
pub next_daily_claim: TimestampMillis,
pub is_unique_person: Option<bool>,
pub wallet_config: Option<WalletConfig>,
}

#[derive(CandidType, Serialize, Deserialize, Clone, Debug)]
Expand Down
13 changes: 13 additions & 0 deletions backend/canisters/user/api/src/updates/configure_wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::WalletConfig;
use candid::CandidType;
use serde::{Deserialize, Serialize};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub config: WalletConfig,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success,
}
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 @@ -23,6 +23,7 @@ pub mod c2c_vote_on_proposal;
pub mod cancel_message_reminder;
pub mod cancel_p2p_swap;
pub mod claim_daily_chit;
pub mod configure_wallet;
pub mod create_community;
pub mod create_group;
pub mod delete_community;
Expand Down
5 changes: 4 additions & 1 deletion backend/canisters/user/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use types::{
Achievement, BuildVersion, CanisterId, Chat, ChatId, ChatMetrics, ChitEarned, ChitEarnedReason, CommunityId,
Cryptocurrency, Cycles, Document, Notification, TimestampMillis, Timestamped, UniquePersonProof, UserId,
};
use user_canister::{NamedAccount, UserCanisterEvent};
use user_canister::{NamedAccount, UserCanisterEvent, WalletConfig};
use utils::canister_event_sync_queue::CanisterEventSyncQueue;
use utils::env::Environment;
use utils::regular_jobs::RegularJobs;
Expand Down Expand Up @@ -226,6 +226,8 @@ struct Data {
pub achievements: HashSet<Achievement>,
pub achievements_last_seen: TimestampMillis,
pub unique_person_proof: Option<UniquePersonProof>,
#[serde(default)]
pub wallet_config: Timestamped<WalletConfig>,
pub rng_seed: [u8; 32],
}

Expand Down Expand Up @@ -289,6 +291,7 @@ impl Data {
achievements_last_seen: 0,
unique_person_proof: None,
rng_seed: [0; 32],
wallet_config: Timestamped::default(),
}
}

Expand Down
10 changes: 8 additions & 2 deletions backend/canisters/user/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::lifecycle::{init_env, init_state};
use crate::memory::get_upgrades_memory;
use crate::{read_state, Data};
use crate::{mutate_state, read_state, Data};
use canister_logger::LogEntry;
use canister_tracing_macros::trace;
use ic_cdk::post_upgrade;
use stable_memory::get_reader;
use std::time::Duration;
use tracing::info;
use types::{Empty, Milliseconds};
use types::{Empty, Milliseconds, Timestamped};
use user_canister::post_upgrade::Args;
use user_canister::WalletConfig;
use utils::time::DAY_IN_MS;

const SIX_MONTHS: Milliseconds = 183 * DAY_IN_MS;
Expand Down Expand Up @@ -43,6 +44,11 @@ fn post_upgrade(args: Args) {
}
}
});

// TODO: Remove this after the next release
mutate_state(|state| {
state.data.wallet_config = Timestamped::new(WalletConfig::default(), state.env.now());
});
}

fn mark_user_canister_empty() {
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user/impl/src/queries/initial_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ fn initial_state_impl(state: &RuntimeState) -> Response {
streak_ends: state.data.streak.ends(),
next_daily_claim: if state.data.streak.can_claim(now) { today(now) } else { tomorrow(now) },
is_unique_person: state.data.unique_person_proof.is_some(),
wallet_config: state.data.wallet_config.value.clone(),
})
}
4 changes: 4 additions & 0 deletions backend/canisters/user/impl/src/queries/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ fn updates_impl(updates_since: TimestampMillis, state: &RuntimeState) -> Respons
.as_ref()
.is_some_and(|p| p.timestamp > updates_since);

let wallet_config = state.data.wallet_config.if_set_after(updates_since).cloned();

let has_any_updates = username.is_some()
|| display_name.has_update()
|| avatar_id.has_update()
|| blocked_users.is_some()
|| avatar_id.has_update()
|| suspended.is_some()
|| wallet_config.is_some()
|| pin_number_updated
|| is_unique_person_updated
|| state.data.direct_chats.any_updated(updates_since)
Expand Down Expand Up @@ -166,5 +169,6 @@ fn updates_impl(updates_since: TimestampMillis, state: &RuntimeState) -> Respons
streak_ends,
next_daily_claim,
is_unique_person,
wallet_config,
})
}
19 changes: 19 additions & 0 deletions backend/canisters/user/impl/src/updates/configure_wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::guards::caller_is_owner;
use crate::{mutate_state, run_regular_jobs, RuntimeState};
use canister_tracing_macros::trace;
use ic_cdk::update;
use types::Timestamped;
use user_canister::configure_wallet::{Response::*, *};

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

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

fn configure_wallet_impl(args: Args, state: &mut RuntimeState) -> Response {
state.data.wallet_config = Timestamped::new(args.config, state.env.now());
Success
}
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 @@ -24,6 +24,7 @@ pub mod c2c_vote_on_proposal;
pub mod cancel_message_reminder;
pub mod cancel_p2p_swap;
pub mod claim_daily_chit;
pub mod configure_wallet;
pub mod create_community;
pub mod create_group;
pub mod delete_community;
Expand Down

0 comments on commit 8d6207f

Please sign in to comment.