Skip to content

Commit

Permalink
Fix after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgranhao committed Dec 24, 2024
1 parent 3f1729c commit 0a2d9bb
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 42 deletions.
30 changes: 19 additions & 11 deletions lib/core/src/chain_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ impl ChainSwapHandler {
.map_err(|_| anyhow!("Invalid ChainSwapState for Chain Swap {id}: {status}"))?;

match swap_state {
// If the swap is not local (pulled from real-time sync) we do not claim twice
// If the swap is not local (pulled from real-time sync) we do not:
// - claim twice
// - accept fees twice
ChainSwapStates::TransactionServerMempool
| ChainSwapStates::TransactionServerConfirmed => {
| ChainSwapStates::TransactionServerConfirmed
| ChainSwapStates::TransactionLockupFailed => {
log::debug!("Received {swap_state:?} for non-local Chain swap {id} from status stream, skipping update.");
return Ok(());
}
Expand Down Expand Up @@ -378,11 +381,13 @@ impl ChainSwapHandler {
}

async fn handle_amountless_update(&self, swap: &ChainSwap) -> Result<(), PaymentError> {
let id = swap.id.clone();

let quote = self
.swapper
.get_zero_amount_chain_swap_quote(&swap.id)
.get_zero_amount_chain_swap_quote(&id)
.map(|quote| quote.to_sat())?;
info!("Got quote of {quote} sat for swap {}", &swap.id);
info!("Got quote of {quote} sat for swap {}", &id);

match self.validate_amountless_swap(swap, quote).await? {
ValidateAmountlessSwapResult::ReadyForAccepting {
Expand All @@ -391,12 +396,12 @@ impl ChainSwapHandler {
} => {
debug!("Zero-amount swap validated. Auto-accepting...");
self.persister.update_zero_amount_swap_values(
&swap.id,
&id,
user_lockup_amount_sat,
receiver_amount_sat,
)?;
self.swapper
.accept_zero_amount_chain_swap_quote(&swap.id, quote)
.accept_zero_amount_chain_swap_quote(&id, quote)
.map_err(Into::into)
}
ValidateAmountlessSwapResult::RequiresUserAction {
Expand All @@ -406,12 +411,15 @@ impl ChainSwapHandler {
debug!("Zero-amount swap validated. Fees are too high for automatic accepting. Moving to WaitingFeeAcceptance");
// While the user doesn't accept new fees, let's continue to show the original estimate
self.persister.update_zero_amount_swap_values(
&swap.id,
&id,
user_lockup_amount_sat,
receiver_amount_sat_original_estimate,
)?;
self.update_swap_info(&swap.id, WaitingFeeAcceptance, None, None, None, None)
.await
self.update_swap_info(&ChainSwapUpdate {
swap_id: id,
to_state: WaitingFeeAcceptance,
..Default::default()
})
}
}
}
Expand Down Expand Up @@ -940,9 +948,9 @@ impl ChainSwapHandler {
let id = &swap.id;

ensure_sdk!(
swap.state == Refundable,
swap.state.is_refundable(),
PaymentError::Generic {
err: format!("Chain Swap {id} was not marked as `Refundable`")
err: format!("Chain Swap {id} was not in refundable state")
}
);

Expand Down
10 changes: 10 additions & 0 deletions lib/core/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::PartialEq;
use std::path::PathBuf;

use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -1169,6 +1170,15 @@ impl FromSql for PaymentState {
}
}

impl PaymentState {
pub(crate) fn is_refundable(&self) -> bool {
matches!(
self,
PaymentState::Refundable | PaymentState::WaitingFeeAcceptance
)
}
}

#[derive(Debug, Copy, Clone, Eq, EnumString, Display, Hash, PartialEq, Serialize)]
#[strum(serialize_all = "lowercase")]
pub enum PaymentType {
Expand Down
6 changes: 5 additions & 1 deletion lib/core/src/persist/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,11 @@ impl Persister {
}

pub(crate) fn list_pending_chain_swaps(&self) -> Result<Vec<ChainSwap>> {
self.list_chain_swaps_by_state(vec![PaymentState::Pending, PaymentState::RefundPending, PaymentState::WaitingFeeAcceptance])
self.list_chain_swaps_by_state(vec![
PaymentState::Pending,
PaymentState::RefundPending,
PaymentState::WaitingFeeAcceptance,
])
}

pub(crate) fn list_refundable_chain_swaps(&self) -> Result<Vec<ChainSwap>> {
Expand Down
34 changes: 26 additions & 8 deletions lib/core/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2501,7 +2501,11 @@ impl LiquidSdk {
let mut pending_send_sat = 0;
let mut pending_receive_sat = 0;
let payments = self.persister.get_payments(&ListPaymentsRequest {
states: Some(vec![PaymentState::Pending, PaymentState::RefundPending]),
states: Some(vec![
PaymentState::Pending,
PaymentState::RefundPending,
PaymentState::WaitingFeeAcceptance,
]),
..Default::default()
})?;

Expand Down Expand Up @@ -2570,6 +2574,13 @@ impl LiquidSdk {
err: format!("Could not find Swap {}", req.swap_id),
})?;

ensure_sdk!(
chain_swap.state == WaitingFeeAcceptance,
SdkError::Generic {
err: "Payment is not WaitingFeeAcceptance".to_string()
}
);

let server_lockup_quote = self
.swapper
.get_zero_amount_chain_swap_quote(&req.swap_id)?;
Expand Down Expand Up @@ -2604,6 +2615,13 @@ impl LiquidSdk {
err: format!("Could not find Swap {}", swap_id),
})?;

ensure_sdk!(
chain_swap.state == WaitingFeeAcceptance,
PaymentError::Generic {
err: "Payment is not WaitingFeeAcceptance".to_string()
}
);

let server_lockup_quote = self.swapper.get_zero_amount_chain_swap_quote(&swap_id)?;

ensure_sdk!(
Expand All @@ -2618,9 +2636,11 @@ impl LiquidSdk {
)?;
self.swapper
.accept_zero_amount_chain_swap_quote(&swap_id, server_lockup_quote.to_sat())?;
self.chain_swap_handler
.update_swap_info(&swap_id, Pending, None, None, None, None)
.await
self.chain_swap_handler.update_swap_info(&ChainSwapUpdate {
swap_id,
to_state: Pending,
..Default::default()
})
}

/// Empties the Liquid Wallet cache for the [Config::network].
Expand Down Expand Up @@ -3561,8 +3581,7 @@ mod tests {
async fn test_zero_amount_chain_swap_zero_leeway() -> Result<()> {
let user_lockup_sat = 50_000;

let (_tmp_dir, persister) = new_persister()?;
let persister = Arc::new(persister);
create_persister!(persister);
let swapper = Arc::new(MockSwapper::new());
let status_stream = Arc::new(MockStatusStream::new());
let liquid_chain_service = Arc::new(Mutex::new(MockLiquidChainService::new()));
Expand Down Expand Up @@ -3626,8 +3645,7 @@ mod tests {
let user_lockup_sat = 50_000;
let onchain_fee_rate_leeway_sat_per_vbyte = 5;

let (_tmp_dir, persister) = new_persister()?;
let persister = Arc::new(persister);
create_persister!(persister);
let swapper = Arc::new(MockSwapper::new());
let status_stream = Arc::new(MockStatusStream::new());
let liquid_chain_service = Arc::new(Mutex::new(MockLiquidChainService::new()));
Expand Down
1 change: 1 addition & 0 deletions lib/core/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ mod tests {
None,
true,
None,
false,
))?;

sync_service.push().await?;
Expand Down
Loading

0 comments on commit 0a2d9bb

Please sign in to comment.