From 04f79646cbdc5345dfa2c33e7ae259bee30b50b9 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Wed, 7 Feb 2024 16:23:06 +0000 Subject: [PATCH] Refund failed SNEED p2p swaps (#5332) --- backend/canisters/escrow/CHANGELOG.md | 1 + .../escrow/impl/src/lifecycle/post_upgrade.rs | 39 +++++++++++++++++++ .../canisters/escrow/impl/src/model/swaps.rs | 6 ++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/backend/canisters/escrow/CHANGELOG.md b/backend/canisters/escrow/CHANGELOG.md index 12797532d3..c336a136d4 100644 --- a/backend/canisters/escrow/CHANGELOG.md +++ b/backend/canisters/escrow/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Avoid usages of `make_c2c_call` and use macro instead ([#5252](https://github.com/open-chat-labs/open-chat/pull/5252)) - Hack to cater for SNEED's unique handling of transfer fees ([#5280](https://github.com/open-chat-labs/open-chat/pull/5280)) +- Refund failed SNEED p2p swaps ([#5332](https://github.com/open-chat-labs/open-chat/pull/5332)) ## [[2.0.1020](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1020-escrow)] - 2024-01-24 diff --git a/backend/canisters/escrow/impl/src/lifecycle/post_upgrade.rs b/backend/canisters/escrow/impl/src/lifecycle/post_upgrade.rs index 323d1e14ea..5be034c865 100644 --- a/backend/canisters/escrow/impl/src/lifecycle/post_upgrade.rs +++ b/backend/canisters/escrow/impl/src/lifecycle/post_upgrade.rs @@ -1,12 +1,16 @@ use crate::lifecycle::{init_env, init_state}; use crate::memory::get_upgrades_memory; +use crate::model::pending_payments_queue::{PendingPayment, PendingPaymentReason}; +use crate::mutate_state; use crate::Data; use canister_logger::LogEntry; use canister_tracing_macros::trace; use escrow_canister::post_upgrade::Args; +use escrow_canister::SwapStatus; use ic_cdk_macros::post_upgrade; use stable_memory::get_reader; use tracing::info; +use types::CanisterId; use utils::cycles::init_cycles_dispenser_client; #[post_upgrade] @@ -24,4 +28,39 @@ fn post_upgrade(args: Args) { init_state(env, data, args.wasm_version); info!(version = %args.wasm_version, "Post-upgrade complete"); + + let sneed_ledger = CanisterId::from_text("r7cp6-6aaaa-aaaag-qco5q-cai").unwrap(); + + mutate_state(|state| { + let now = state.env.now(); + for swap in state + .data + .swaps + .iter() + .filter(|s| !s.token0_received && matches!(s.status(now), SwapStatus::Expired(_)) && s.refunds.is_empty()) + { + if let Some((accepted_by, _)) = swap.accepted_by { + state.data.pending_payments_queue.push(PendingPayment { + user_id: accepted_by, + timestamp: now, + token_info: swap.token1.clone(), + amount: swap.amount1, + swap_id: swap.id, + reason: PendingPaymentReason::Refund, + }); + } + + if swap.token0.ledger == sneed_ledger { + state.data.pending_payments_queue.push(PendingPayment { + user_id: swap.created_by, + timestamp: now, + token_info: swap.token0.clone(), + amount: swap.amount0, + swap_id: swap.id, + reason: PendingPaymentReason::Refund, + }); + } + } + crate::jobs::make_pending_payments::start_job_if_required(state); + }); } diff --git a/backend/canisters/escrow/impl/src/model/swaps.rs b/backend/canisters/escrow/impl/src/model/swaps.rs index 687a83f995..397bc7cc4e 100644 --- a/backend/canisters/escrow/impl/src/model/swaps.rs +++ b/backend/canisters/escrow/impl/src/model/swaps.rs @@ -25,6 +25,10 @@ impl Swaps { self.map.get_mut(&id) } + pub fn iter(&self) -> impl Iterator { + self.map.values() + } + pub fn metrics(&self, now: TimestampMillis) -> SwapMetrics { let mut metrics = SwapMetrics { total: self.map.len() as u32, @@ -100,7 +104,7 @@ impl Swap { } pub fn status(&self, now: TimestampMillis) -> SwapStatus { - if let Some((accepted_by, accepted_at)) = self.accepted_by { + if let Some((accepted_by, accepted_at)) = self.token0_received.then_some(self.accepted_by).flatten() { if let (Some(token0_transfer_out), Some(token1_transfer_out)) = (self.token0_transfer_out.clone(), self.token1_transfer_out.clone()) {