Skip to content

Commit

Permalink
fix list_swaps status update
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedberg committed Jul 4, 2024
1 parent a7bd42c commit 317df84
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
21 changes: 19 additions & 2 deletions src/ldk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {}))
Expand Down
9 changes: 8 additions & 1 deletion src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>,
}

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<u64>,
) -> Self {
Self {
swap_info: swap_info.clone(),
status,
initiated_at,
}
}
}
Expand Down

0 comments on commit 317df84

Please sign in to comment.