From 317df84f6879525dda7ed9bb15ac6beee7aa961e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zoe=20Faltib=C3=A0?= Date: Thu, 4 Jul 2024 11:43:28 +0200 Subject: [PATCH] fix list_swaps status update --- src/ldk.rs | 21 +++++++++++++++++++-- src/routes.rs | 16 ++++++++++------ src/swap.rs | 9 ++++++++- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/ldk.rs b/src/ldk.rs index 5baf1f9..0ad68d6 100644 --- a/src/ldk.rs +++ b/src/ldk.rs @@ -89,7 +89,8 @@ use crate::rgb::{get_rgb_channel_info_optional, RgbLibWalletWrapper}; use crate::routes::{HTLCStatus, SwapStatus, DUST_LIMIT_MSAT}; use crate::swap::SwapData; use crate::utils::{ - connect_peer_if_necessary, do_connect_peer, hex_str, AppState, StaticState, UnlockedAppState, + connect_peer_if_necessary, do_connect_peer, get_current_timestamp, hex_str, AppState, + StaticState, UnlockedAppState, }; pub(crate) const FEE_RATE: f32 = 7.0; @@ -156,6 +157,14 @@ impl UnlockedAppState { self.save_maker_swaps(maker_swaps); } + pub(crate) fn update_maker_swap_to_pending(&self, payment_hash: &PaymentHash) { + let mut maker_swaps = self.get_maker_swaps(); + let maker_swap = maker_swaps.swaps.get_mut(payment_hash).unwrap(); + maker_swap.status = SwapStatus::Pending; + maker_swap.initiated_at = Some(get_current_timestamp()); + self.save_maker_swaps(maker_swaps); + } + pub(crate) fn is_maker_swap(&self, payment_hash: &PaymentHash) -> bool { self.maker_swaps().contains_key(payment_hash) } @@ -173,6 +182,14 @@ impl UnlockedAppState { self.save_taker_swaps(taker_swaps); } + pub(crate) fn update_taker_swap_to_pending(&self, payment_hash: &PaymentHash) { + let mut taker_swaps = self.get_taker_swaps(); + let taker_swap = taker_swaps.swaps.get_mut(payment_hash).unwrap(); + taker_swap.status = SwapStatus::Pending; + taker_swap.initiated_at = Some(get_current_timestamp()); + self.save_taker_swaps(taker_swaps); + } + pub(crate) fn is_taker_swap(&self, payment_hash: &PaymentHash) -> bool { self.taker_swaps().contains_key(payment_hash) } @@ -1030,7 +1047,7 @@ async fn handle_ldk_events( } tracing::debug!("Swap is whitelisted, forwarding the htlc..."); - unlocked_state.update_taker_swap_status(&payment_hash, SwapStatus::Pending); + unlocked_state.update_taker_swap_to_pending(&payment_hash); unlocked_state .channel_manager diff --git a/src/routes.rs b/src/routes.rs index cdfb19f..f3f1b84 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1646,10 +1646,14 @@ pub(crate) async fn list_swaps( let map_swap = |payment_hash: &PaymentHash, swap_data: &SwapData, taker: bool| { let mut status = swap_data.status.clone(); - if [SwapStatus::Waiting, SwapStatus::Pending].contains(&status) - && get_current_timestamp() > swap_data.swap_info.expiry - { + if status == SwapStatus::Waiting && get_current_timestamp() > swap_data.swap_info.expiry { status = SwapStatus::Expired; + } else if status == SwapStatus::Pending + && get_current_timestamp() > swap_data.initiated_at.unwrap() + 86400 + { + status = SwapStatus::Failed; + } + if status != swap_data.status { if taker { unlocked_state.update_taker_swap_status(payment_hash, status.clone()); } else { @@ -2045,7 +2049,7 @@ pub(crate) async fn maker_execute( ); } - unlocked_state.update_maker_swap_status(&swapstring.payment_hash, SwapStatus::Pending); + unlocked_state.update_maker_swap_to_pending(&swapstring.payment_hash); let (_status, err) = match unlocked_state.channel_manager.send_spontaneous_payment( &route, @@ -2117,7 +2121,7 @@ pub(crate) async fn maker_init( qty_to, expiry, }; - let swap_data = SwapData::from_swap_info(&swap_info, SwapStatus::Waiting); + let swap_data = SwapData::from_swap_info(&swap_info, SwapStatus::Waiting, None); // Check that we have enough assets to send if let Some(to_asset) = to_asset { @@ -2711,7 +2715,7 @@ pub(crate) async fn taker( } } - let swap_data = SwapData::from_swap_info(&swapstring.swap_info, SwapStatus::Waiting); + let swap_data = SwapData::from_swap_info(&swapstring.swap_info, SwapStatus::Waiting, None); unlocked_state.add_taker_swap(swapstring.payment_hash, swap_data); Ok(Json(EmptyResponse {})) diff --git a/src/swap.rs b/src/swap.rs index 3d71e3a..021f388 100644 --- a/src/swap.rs +++ b/src/swap.rs @@ -10,18 +10,25 @@ use crate::{routes::SwapStatus, utils::hex_str_to_vec}; pub(crate) struct SwapData { pub(crate) swap_info: SwapInfo, pub(crate) status: SwapStatus, + pub(crate) initiated_at: Option, } impl_writeable_tlv_based!(SwapData, { (0, swap_info, required), (1, status, required), + (2, initiated_at, option), }); impl SwapData { - pub(crate) fn from_swap_info(swap_info: &SwapInfo, status: SwapStatus) -> Self { + pub(crate) fn from_swap_info( + swap_info: &SwapInfo, + status: SwapStatus, + initiated_at: Option, + ) -> Self { Self { swap_info: swap_info.clone(), status, + initiated_at, } } }