Skip to content

Commit

Permalink
Refund failed SNEED p2p swaps
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Jan 31, 2024
1 parent 7f9834e commit 9de2733
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
40 changes: 40 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,17 @@
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::timer_job_types::{ExpireSwapJob, TimerJob};

Check failure on line 5 in backend/canisters/escrow/impl/src/lifecycle/post_upgrade.rs

View workflow job for this annotation

GitHub Actions / run clippy

unused imports: `ExpireSwapJob`, `TimerJob`

Check warning on line 5 in backend/canisters/escrow/impl/src/lifecycle/post_upgrade.rs

View workflow job for this annotation

GitHub Actions / run unit tests

unused imports: `ExpireSwapJob`, `TimerJob`
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 +29,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.token0.fee,
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 9de2733

Please sign in to comment.