Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 5% fee to prizes on FE #6878

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use chat_events::MessageContentInternal;
use constants::{MEMO_MESSAGE, MEMO_P2P_SWAP_CREATE, MEMO_PRIZE, NANOS_PER_MILLISECOND, PRIZE_FEE_PERCENT, SECOND_IN_MS};
use escrow_canister::deposit_subaccount;
use ic_cdk::api::call::CallResult;
use tracing::error;
use types::icrc1::Account;
use types::{
icrc1, Achievement, CanisterId, Chat, CompletedCryptoTransaction, CryptoTransaction, MessageContentInitial, MessageId,
Expand Down Expand Up @@ -331,8 +332,14 @@ fn prepare(
let oc_fee = (total_prizes * PRIZE_FEE_PERCENT as u128) / 100;
let total_amount_to_send_old = total_prizes + total_transfer_fees;
let total_amount_to_send = total_prizes + total_transfer_fees + oc_fee;

if t.units() != total_amount_to_send && t.units() != total_amount_to_send_old {
let transaction_amount = t.units();

if transaction_amount != total_amount_to_send && transaction_amount != total_amount_to_send_old {
error!(
?total_amount_to_send,
?transaction_amount,
"Expected vs Actual prize transfer"
);
return InvalidRequest("Transaction amount must equal total prizes + total fees".to_string());
}

Expand Down
27 changes: 16 additions & 11 deletions frontend/app/src/components/home/PrizeContentBuilder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
const ONE_HOUR = 1000 * 60 * 60;
const ONE_DAY = ONE_HOUR * 24;
const ONE_WEEK = ONE_DAY * 7;
const OC_FEE_PERCENTAGE = 5n;
const client = getContext<OpenChat>("client");
const dispatch = createEventDispatcher();
const streaks = ["3", "7", "14", "30", "100"];
Expand Down Expand Up @@ -73,15 +74,17 @@
$: tokenDetails = $cryptoLookup[ledger];
$: symbol = tokenDetails.symbol;
$: howToBuyUrl = tokenDetails.howToBuyUrl;
$: transferFees = tokenDetails.transferFee;
$: totalFees = transferFees + transferFees * BigInt(numberOfWinners ?? 0);
$: transferFee = tokenDetails.transferFee;
$: transferFees = transferFee * BigInt(numberOfWinners ?? 0);
$: prizeFees = transferFees + (draftAmount * OC_FEE_PERCENTAGE) / 100n;
$: totalFees = transferFee + prizeFees;
$: multiUserChat = chat.kind === "group_chat" || chat.kind === "channel";
$: remainingBalance =
draftAmount > 0n ? cryptoBalance - draftAmount - totalFees : cryptoBalance;
$: minAmount = 100n * BigInt(numberOfWinners ?? 0) * transferFees;
$: minAmount = 100n * BigInt(numberOfWinners ?? 0) * transferFee;
$: maxAmount = bigIntMax(cryptoBalance - totalFees, BigInt(0));
$: valid = error === undefined && tokenInputState === "ok" && !tokenChanging;
$: zero = cryptoBalance <= transferFees && !tokenChanging;
$: zero = cryptoBalance <= transferFee && !tokenChanging;
$: errorMessage = error !== undefined ? i18nKey(error) : $pinNumberErrorMessageStore;

$: {
Expand Down Expand Up @@ -126,7 +129,9 @@

function send() {
const prizes = generatePrizes();
const prizeFees = transferFees * BigInt(numberOfWinners ?? 0);
const amountE8s = prizes.reduce((total, p) => total + p) + prizeFees;

console.log(`Prize amount: ${amountE8s}`);
const content: PrizeContentInitial = {
kind: "prize_content_initial",
caption: message === "" ? undefined : message,
Expand All @@ -140,8 +145,8 @@
ledger,
token: symbol,
recipient: recipientFromContext(context),
amountE8s: prizes.reduce((total, p) => total + p) + prizeFees,
feeE8s: transferFees,
amountE8s,
feeE8s: transferFee,
createdAtNanos: BigInt(Date.now()) * BigInt(1_000_000),
},
prizes,
Expand Down Expand Up @@ -183,7 +188,7 @@
tokenChanging = false;
if (remainingBalance < 0) {
remainingBalance = BigInt(0);
draftAmount = cryptoBalance - transferFees;
draftAmount = cryptoBalance - transferFee;
if (draftAmount < 0) {
draftAmount = BigInt(0);
}
Expand Down Expand Up @@ -239,9 +244,9 @@
let maxIndex = 0;
for (let i = 0; i < prizes.length; i++) {
const prize = prizes[i];
if (prize < transferFees) {
prizes[i] = transferFees;
totalAdded += transferFees - prize;
if (prize < transferFee) {
prizes[i] = transferFee;
totalAdded += transferFee - prize;
} else if (prize > max) {
max = prize;
maxIndex = i;
Expand Down
1 change: 1 addition & 0 deletions frontend/openchat-agent/src/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ export const ChatMetrics = Type.Object({
file_messages: Type.BigInt(),
polls: Type.BigInt(),
poll_votes: Type.BigInt(),
crypto_messages: Type.BigInt(),
icp_messages: Type.BigInt(),
sns1_messages: Type.BigInt(),
ckbtc_messages: Type.BigInt(),
Expand Down
2 changes: 1 addition & 1 deletion tsBindings/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export type SwapStatusErrorExpired = { token0_txn_out?: bigint | undefined, };
export type PinNumberSettings = { length: number, attempts_blocked_until?: bigint | undefined, };
export type ApproveError = { "BadFee": { expected_fee: bigint, } } | { "InsufficientFunds": { balance: bigint, } } | { "AllowanceChanged": { current_allowance: bigint, } } | { "Expired": { ledger_time: bigint, } } | "TooOld" | { "CreatedInFuture": { ledger_time: bigint, } } | { "Duplicate": { duplicate_of: bigint, } } | "TemporarilyUnavailable" | { "GenericError": { error_code: bigint, message: string, } };
export type VideoCallPresence = "Default" | "Owner" | "Hidden";
export type ChatMetrics = { text_messages: bigint, image_messages: bigint, video_messages: bigint, audio_messages: bigint, file_messages: bigint, polls: bigint, poll_votes: bigint, icp_messages: bigint, sns1_messages: bigint, ckbtc_messages: bigint, chat_messages: bigint, kinic_messages: bigint, deleted_messages: bigint, giphy_messages: bigint, prize_messages: bigint, prize_winner_messages: bigint, replies: bigint, edits: bigint, reactions: bigint, proposals: bigint, reported_messages: bigint, message_reminders: bigint, custom_type_messages: bigint, last_active: bigint, };
export type ChatMetrics = { text_messages: bigint, image_messages: bigint, video_messages: bigint, audio_messages: bigint, file_messages: bigint, polls: bigint, poll_votes: bigint, crypto_messages: bigint, icp_messages: bigint, sns1_messages: bigint, ckbtc_messages: bigint, chat_messages: bigint, kinic_messages: bigint, deleted_messages: bigint, giphy_messages: bigint, prize_messages: bigint, prize_winner_messages: bigint, replies: bigint, edits: bigint, reactions: bigint, proposals: bigint, reported_messages: bigint, message_reminders: bigint, custom_type_messages: bigint, last_active: bigint, };
export type VideoCallType = "Broadcast" | "Default";
export type VideoCall = { message_index: MessageIndex, call_type: VideoCallType, };
export type GroupRole = "Owner" | "Admin" | "Moderator" | "Participant";
Expand Down
Loading