Skip to content

Commit

Permalink
Support prize messages in any token by getting fee from original tran…
Browse files Browse the repository at this point in the history
…sfer (#4470)
  • Loading branch information
hpeebles authored Oct 2, 2023
1 parent efd9c97 commit a6799b2
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- All members can mention @everyone by default in private communities ([#4458](https://github.com/open-chat-labs/open-chat/pull/4458))
- Notifications for custom messages should use the sub-type ([#4465](https://github.com/open-chat-labs/open-chat/pull/4465))
- Join all community members to channels that are made public ([#4469](https://github.com/open-chat-labs/open-chat/pull/4469))
- Support prize messages in any token by getting fee from original transfer ([#4470](https://github.com/open-chat-labs/open-chat/pull/4470))

## [[2.0.864](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.864-community)] - 2023-09-27

Expand Down
8 changes: 3 additions & 5 deletions backend/canisters/community/impl/src/updates/claim_prize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,20 @@ fn prepare(args: &Args, state: &mut RuntimeState) -> Result<PrepareResult, Box<R
let min_visible_event_index = channel_member.min_visible_event_index();
let user_id = member.user_id;

let (token, ledger, amount) =
let (token, ledger, amount, fee) =
match channel
.chat
.events
.reserve_prize(args.message_id, min_visible_event_index, user_id, now)
{
ReservePrizeResult::AlreadyClaimed => return Err(Box::new(AlreadyClaimed)),
ReservePrizeResult::Success(t, l, a) => (t, l, a),
ReservePrizeResult::Success(t, l, a, f) => (t, l, a, f),
ReservePrizeResult::MessageNotFound => return Err(Box::new(MessageNotFound)),
ReservePrizeResult::PrizeFullyClaimed => return Err(Box::new(PrizeFullyClaimed)),
ReservePrizeResult::PrizeEnded => return Err(Box::new(PrizeEnded)),
};

let fee = token.fee().unwrap(); // TODO send up the transaction fee when creating the prize message

let transaction = create_pending_transaction(token, ledger, amount.e8s() as u128, fee, user_id, now_nanos);
let transaction = create_pending_transaction(token, ledger, amount, fee, user_id, now_nanos);

Ok(PrepareResult {
group: state.env.canister_id(),
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/).
### Changed

- Notifications for custom messages should use the sub-type ([#4465](https://github.com/open-chat-labs/open-chat/pull/4465))
- Support prize messages in any token by getting fee from original transfer ([#4470](https://github.com/open-chat-labs/open-chat/pull/4470))

## [[2.0.865](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.865-group)] - 2023-09-27

Expand Down
8 changes: 3 additions & 5 deletions backend/canisters/group/impl/src/updates/claim_prize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,21 @@ fn prepare(args: &Args, state: &mut RuntimeState) -> Result<PrepareResult, Box<R
let min_visible_event_index = member.min_visible_event_index();
let user_id = member.user_id;

let (token, ledger, amount) =
let (token, ledger, amount, fee) =
match state
.data
.chat
.events
.reserve_prize(args.message_id, min_visible_event_index, user_id, now)
{
ReservePrizeResult::AlreadyClaimed => return Err(Box::new(AlreadyClaimed)),
ReservePrizeResult::Success(t, l, a) => (t, l, a),
ReservePrizeResult::Success(t, l, a, f) => (t, l, a, f),
ReservePrizeResult::MessageNotFound => return Err(Box::new(MessageNotFound)),
ReservePrizeResult::PrizeFullyClaimed => return Err(Box::new(PrizeFullyClaimed)),
ReservePrizeResult::PrizeEnded => return Err(Box::new(PrizeEnded)),
};

let fee = token.fee().unwrap(); // TODO send up the transaction fee when creating the prize message

let transaction = create_pending_transaction(token, ledger, amount.e8s() as u128, fee, user_id, now_nanos);
let transaction = create_pending_transaction(token, ledger, amount, fee, user_id, now_nanos);

Ok(PrepareResult {
group: state.env.canister_id(),
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user/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/).
### Changed

- Notifications for custom messages should use the sub-type ([#4465](https://github.com/open-chat-labs/open-chat/pull/4465))
- Support prize messages in any token by getting fee from original transfer ([#4470](https://github.com/open-chat-labs/open-chat/pull/4470))
- Prevent transfers to yourself ([#4471](https://github.com/open-chat-labs/open-chat/pull/4471))

## [[2.0.867](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.867-user)] - 2023-09-27
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ fn prepare(content: &MessageContentInitial, state: &RuntimeState) -> PrepareResu
match &c.transfer {
CryptoTransaction::Pending(t) => {
let total_prize = c.prizes.iter().map(|t| t.e8s()).sum::<u64>() as u128;
let prize_fees = c.prizes.len() as u128 * t.token().fee().unwrap();
let prize_fees = c.prizes.len() as u128 * t.fee();
let total_amount_to_send = total_prize + prize_fees;

if t.units() != total_amount_to_send {
Expand Down
5 changes: 3 additions & 2 deletions backend/libraries/chat_events/src/chat_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,12 +601,13 @@ impl ChatEvents {
let amount = content.prizes_remaining.pop().expect("some prizes_remaining");
let token = content.transaction.token();
let ledger_canister_id = content.transaction.ledger_canister_id();
let fee = content.transaction.fee();

content.reservations.insert(user_id);
message.last_updated = Some(now);
self.last_updated_timestamps.mark_updated(None, event_index, now);

return ReservePrizeResult::Success(token, ledger_canister_id, amount);
return ReservePrizeResult::Success(token, ledger_canister_id, amount.e8s() as u128, fee);
}
}

Expand Down Expand Up @@ -1454,7 +1455,7 @@ pub enum TipMessageResult {
}

pub enum ReservePrizeResult {
Success(Cryptocurrency, CanisterId, Tokens),
Success(Cryptocurrency, CanisterId, u128, u128),
MessageNotFound,
AlreadyClaimed,
PrizeFullyClaimed,
Expand Down
33 changes: 32 additions & 1 deletion backend/libraries/types/src/cryptocurrency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use candid::{CandidType, Principal};
use ic_ledger_types::{AccountIdentifier, Subaccount, DEFAULT_SUBACCOUNT};
use serde::{Deserialize, Serialize};

const ICP_FEE: u128 = 10_000;

#[derive(CandidType, Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Hash)]
pub enum Cryptocurrency {
InternetComputer,
Expand Down Expand Up @@ -39,7 +41,7 @@ impl Cryptocurrency {

pub fn fee(&self) -> Option<u128> {
match self {
Cryptocurrency::InternetComputer => Some(10_000),
Cryptocurrency::InternetComputer => Some(ICP_FEE),
Cryptocurrency::SNS1 => Some(1_000),
Cryptocurrency::CKBTC => Some(10),
Cryptocurrency::CHAT => Some(100_000),
Expand Down Expand Up @@ -115,6 +117,14 @@ impl CryptoTransaction {
CryptoTransaction::Failed(f) => f.units(),
}
}

pub fn fee(&self) -> u128 {
match self {
CryptoTransaction::Pending(p) => p.fee(),
CryptoTransaction::Completed(c) => c.fee(),
CryptoTransaction::Failed(f) => f.fee(),
}
}
}

impl PendingCryptoTransaction {
Expand Down Expand Up @@ -143,6 +153,13 @@ impl PendingCryptoTransaction {
}
}

pub fn fee(&self) -> u128 {
match self {
PendingCryptoTransaction::NNS(_) => ICP_FEE,
PendingCryptoTransaction::ICRC1(t) => t.fee,
}
}

pub fn user_id(&self) -> Option<UserId> {
match self {
PendingCryptoTransaction::NNS(t) => {
Expand Down Expand Up @@ -204,6 +221,13 @@ impl CompletedCryptoTransaction {
CompletedCryptoTransaction::ICRC1(t) => t.amount,
}
}

pub fn fee(&self) -> u128 {
match self {
CompletedCryptoTransaction::NNS(_) => ICP_FEE,
CompletedCryptoTransaction::ICRC1(t) => t.fee,
}
}
}

impl FailedCryptoTransaction {
Expand Down Expand Up @@ -234,6 +258,13 @@ impl FailedCryptoTransaction {
FailedCryptoTransaction::ICRC1(t) => t.amount,
}
}

pub fn fee(&self) -> u128 {
match self {
FailedCryptoTransaction::NNS(_) => ICP_FEE,
FailedCryptoTransaction::ICRC1(t) => t.fee,
}
}
}

pub mod nns {
Expand Down

0 comments on commit a6799b2

Please sign in to comment.