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

A few fixes for swapping tokens #4937

Merged
merged 2 commits into from
Dec 5, 2023
Merged
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
4 changes: 4 additions & 0 deletions backend/canisters/user/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- When swapping tokens take fee out of amount passed in ([#4934](https://github.com/open-chat-labs/open-chat/pull/4934))

### Fixed

- A few fixes for swapping tokens ([#4937](https://github.com/open-chat-labs/open-chat/pull/4937))

## [[2.0.961](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.961-user)] - 2023-12-05

### Added
Expand Down
10 changes: 6 additions & 4 deletions backend/canisters/user/impl/src/updates/swap_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub(crate) async fn process_token_swap(mut token_swap: TokenSwap, attempt: u32)
}
};

let amount_to_dex = args.input_amount.saturating_sub(args.input_token.fee);

if extract_result(&token_swap.transfer).is_none() {
let now = read_state(|state| state.env.now());
let transfer_result = match icrc_ledger_canister_c2c_client::icrc1_transfer(
Expand All @@ -64,7 +66,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.saturating_sub(args.input_token.fee).into(),
amount: amount_to_dex.into(),
},
)
.await
Expand Down Expand Up @@ -95,7 +97,7 @@ pub(crate) async fn process_token_swap(mut token_swap: TokenSwap, attempt: u32)
}

if extract_result(&token_swap.notified_dex_at).is_none() {
if let Err(error) = swap_client.deposit(args.input_amount).await {
if let Err(error) = swap_client.deposit(amount_to_dex).await {
let msg = format!("{error:?}");
mutate_state(|state| {
let now = state.env.now();
Expand All @@ -117,7 +119,7 @@ pub(crate) async fn process_token_swap(mut token_swap: TokenSwap, attempt: u32)
a
} else {
match swap_client
.swap(args.input_amount.saturating_sub(args.input_token.fee), args.min_output_amount)
.swap(amount_to_dex.saturating_sub(args.input_token.fee), args.min_output_amount)
.await
{
Ok(a) => {
Expand All @@ -144,7 +146,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(2 * args.input_token.fee))
(false, amount_to_dex.saturating_sub(args.input_token.fee))
};

if extract_result(&token_swap.withdrawn_from_dex_at).is_none() {
Expand Down
Loading