Skip to content

Commit

Permalink
Refund failed SNEED p2p swaps (#5332)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Feb 7, 2024
1 parent 1bd7fb9 commit 04f7964
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions backend/canisters/escrow/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
39 changes: 39 additions & 0 deletions backend/canisters/escrow/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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);
});
}
6 changes: 5 additions & 1 deletion backend/canisters/escrow/impl/src/model/swaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl Swaps {
self.map.get_mut(&id)
}

pub fn iter(&self) -> impl Iterator<Item = &Swap> {
self.map.values()
}

pub fn metrics(&self, now: TimestampMillis) -> SwapMetrics {
let mut metrics = SwapMetrics {
total: self.map.len() as u32,
Expand Down Expand Up @@ -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())
{
Expand Down

0 comments on commit 04f7964

Please sign in to comment.