diff --git a/backend/canisters/user/CHANGELOG.md b/backend/canisters/user/CHANGELOG.md index d154be64d8..afd8d2fecf 100644 --- a/backend/canisters/user/CHANGELOG.md +++ b/backend/canisters/user/CHANGELOG.md @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [unreleased] +### Changed + +- When swapping tokens take fee out of amount passed in ([#4934](https://github.com/open-chat-labs/open-chat/pull/4934)) + ## [[2.0.961](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.961-user)] - 2023-12-05 ### Added diff --git a/backend/canisters/user/impl/src/updates/swap_tokens.rs b/backend/canisters/user/impl/src/updates/swap_tokens.rs index 628817f7d4..aa4e7edb90 100644 --- a/backend/canisters/user/impl/src/updates/swap_tokens.rs +++ b/backend/canisters/user/impl/src/updates/swap_tokens.rs @@ -64,7 +64,7 @@ pub(crate) async fn process_token_swap(mut token_swap: TokenSwap, attempt: u32) fee: Some(args.input_token.fee.into()), created_at_time: Some(now * NANOS_PER_MILLISECOND), memo: Some(MEMO_SWAP.to_vec().into()), - amount: args.input_amount.into(), + amount: args.input_amount.saturating_sub(args.input_token.fee).into(), }, ) .await @@ -85,7 +85,7 @@ pub(crate) async fn process_token_swap(mut token_swap: TokenSwap, attempt: u32) Err(msg) => { mutate_state(|state| { let now = state.env.now(); - token_swap.notified_dex_at = Some(Timestamped::new(Err(msg.clone()), now)); + token_swap.transfer = Some(Timestamped::new(Err(msg.clone()), now)); token_swap.success = Some(Timestamped::new(false, now)); state.data.token_swaps.upsert(token_swap); }); @@ -99,7 +99,7 @@ pub(crate) async fn process_token_swap(mut token_swap: TokenSwap, attempt: u32) let msg = format!("{error:?}"); mutate_state(|state| { let now = state.env.now(); - token_swap.transfer = Some(Timestamped::new(Err(msg.clone()), now)); + token_swap.notified_dex_at = Some(Timestamped::new(Err(msg.clone()), now)); state.data.token_swaps.upsert(token_swap.clone()); enqueue_token_swap(token_swap, attempt, now, &mut state.data); }); @@ -144,7 +144,7 @@ pub(crate) async fn process_token_swap(mut token_swap: TokenSwap, attempt: u32) let (successful_swap, amount_out) = if let Ok(amount_swapped) = swap_result { (true, amount_swapped.saturating_sub(args.output_token.fee)) } else { - (false, args.input_amount.saturating_sub(args.input_token.fee)) + (false, args.input_amount.saturating_sub(2 * args.input_token.fee)) }; if extract_result(&token_swap.withdrawn_from_dex_at).is_none() {