Skip to content

Commit

Permalink
Log error if any steps during a swap fail (#4972)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Dec 8, 2023
1 parent ce05a5a commit 72486fb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions backend/canisters/user/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Changed

- Log error if any steps during a swap fail ([#4972](https://github.com/open-chat-labs/open-chat/pull/4972))

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

### Changed
Expand Down
10 changes: 9 additions & 1 deletion backend/canisters/user/api/src/updates/swap_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::{CanisterId, TokenInfo};
use types::{CanisterId, ExchangeId, TokenInfo};

#[derive(CandidType, Serialize, Deserialize, Clone, Debug)]
pub struct Args {
Expand All @@ -17,6 +17,14 @@ pub enum ExchangeArgs {
ICPSwap(ICPSwapArgs),
}

impl ExchangeArgs {
pub fn exchange_id(&self) -> ExchangeId {
match self {
ExchangeArgs::ICPSwap(_) => ExchangeId::ICPSwap,
}
}
}

#[derive(CandidType, Serialize, Deserialize, Clone, Debug)]
pub struct ICPSwapArgs {
pub swap_canister_id: CanisterId,
Expand Down
17 changes: 17 additions & 0 deletions backend/canisters/user/impl/src/updates/swap_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use canister_tracing_macros::trace;
use ic_cdk_macros::update;
use icpswap_client::ICPSwapClient;
use icrc_ledger_types::icrc1::transfer::TransferArg;
use tracing::error;
use types::{TimestampMillis, Timestamped};
use user_canister::swap_tokens::{Response::*, *};
use utils::consts::MEMO_SWAP;
Expand Down Expand Up @@ -49,6 +50,7 @@ pub(crate) async fn process_token_swap(mut token_swap: TokenSwap, attempt: u32)
token_swap.success = Some(Timestamped::new(false, now));
state.data.token_swaps.upsert(token_swap);
});
log_error("Failed to get deposit account", msg.as_str(), &args, attempt);
return InternalError(msg);
}
}
Expand Down Expand Up @@ -91,6 +93,7 @@ pub(crate) async fn process_token_swap(mut token_swap: TokenSwap, attempt: u32)
token_swap.success = Some(Timestamped::new(false, now));
state.data.token_swaps.upsert(token_swap);
});
log_error("Failed to transfer tokens", msg.as_str(), &args, attempt);
return InternalError(msg);
}
}
Expand All @@ -105,6 +108,7 @@ pub(crate) async fn process_token_swap(mut token_swap: TokenSwap, attempt: u32)
state.data.token_swaps.upsert(token_swap.clone());
enqueue_token_swap(token_swap, attempt, now, &mut state.data);
});
log_error("Failed to deposit tokens", msg.as_str(), &args, attempt);
return InternalError(msg);
} else {
mutate_state(|state| {
Expand Down Expand Up @@ -138,6 +142,7 @@ pub(crate) async fn process_token_swap(mut token_swap: TokenSwap, attempt: u32)
state.data.token_swaps.upsert(token_swap.clone());
enqueue_token_swap(token_swap, attempt, now, &mut state.data);
});
log_error("Failed to swap tokens", msg.as_str(), &args, attempt);
return InternalError(msg);
}
}
Expand All @@ -158,6 +163,7 @@ pub(crate) async fn process_token_swap(mut token_swap: TokenSwap, attempt: u32)
state.data.token_swaps.upsert(token_swap.clone());
enqueue_token_swap(token_swap, attempt, now, &mut state.data);
});
log_error("Failed to withdraw tokens", msg.as_str(), &args, attempt);
return InternalError(msg);
} else {
mutate_state(|state| {
Expand Down Expand Up @@ -211,3 +217,14 @@ fn enqueue_token_swap(token_swap: TokenSwap, attempt: u32, now: TimestampMillis,
fn extract_result<T>(subtask: &Option<Timestamped<Result<T, String>>>) -> Option<&T> {
subtask.as_ref().and_then(|t| t.value.as_ref().ok())
}

fn log_error(message: &str, error: &str, args: &Args, attempt: u32) {
error!(
exchange_id = %args.exchange_args.exchange_id(),
input_token = args.input_token.token.token_symbol(),
output_token = args.output_token.token.token_symbol(),
error,
attempt,
message
);
}

0 comments on commit 72486fb

Please sign in to comment.