mirror of
https://github.com/aljazceru/breez-sdk-liquid.git
synced 2026-01-18 21:44:23 +01:00
Optimistically persist accepted receiver amount
This commit is contained in:
@@ -380,6 +380,14 @@ impl ChainSwapHandler {
|
||||
async fn handle_amountless_update(&self, swap: &ChainSwap) -> Result<(), PaymentError> {
|
||||
let id = swap.id.clone();
|
||||
|
||||
// Since we optimistically persist the accepted receiver amount, if accepting a quote with
|
||||
// the swapper fails, we might still think it's accepted, so now we should get rid of the
|
||||
// old invalid accepted amount.
|
||||
if swap.accepted_receiver_amount_sat.is_some() {
|
||||
info!("Handling amountless update for swap {id} with existing accepted receiver amount. Erasing the accepted amount now...");
|
||||
self.persister.update_accepted_receiver_amount(&id, None)?;
|
||||
}
|
||||
|
||||
let quote = self
|
||||
.swapper
|
||||
.get_zero_amount_chain_swap_quote(&id)
|
||||
@@ -394,10 +402,14 @@ impl ChainSwapHandler {
|
||||
debug!("Zero-amount swap validated. Auto-accepting...");
|
||||
self.persister
|
||||
.update_actual_payer_amount(&id, user_lockup_amount_sat)?;
|
||||
self.swapper
|
||||
.accept_zero_amount_chain_swap_quote(&id, quote)?;
|
||||
self.persister
|
||||
.update_accepted_receiver_amount(&id, receiver_amount_sat)
|
||||
.update_accepted_receiver_amount(&id, Some(receiver_amount_sat))?;
|
||||
self.swapper
|
||||
.accept_zero_amount_chain_swap_quote(&id, quote)
|
||||
.inspect_err(|e| {
|
||||
error!("Failed to accept zero-amount swap {id} quote: {e} - trying to erase the accepted receiver amount...");
|
||||
let _ = self.persister.update_accepted_receiver_amount(&id, None);
|
||||
})
|
||||
}
|
||||
ValidateAmountlessSwapResult::RequiresUserAction {
|
||||
user_lockup_amount_sat,
|
||||
|
||||
@@ -318,13 +318,15 @@ impl Persister {
|
||||
}
|
||||
|
||||
/// Used for receive chain swaps, when fees are accepted and thus the agreed received amount is known
|
||||
///
|
||||
/// Can also be used to erase a previously persisted accepted amount in case of failure to accept.
|
||||
pub(crate) fn update_accepted_receiver_amount(
|
||||
&self,
|
||||
swap_id: &str,
|
||||
accepted_receiver_amount_sat: u64,
|
||||
accepted_receiver_amount_sat: Option<u64>,
|
||||
) -> Result<(), PaymentError> {
|
||||
log::info!(
|
||||
"Updating chain swap {swap_id}: accepted_receiver_amount_sat = {accepted_receiver_amount_sat}"
|
||||
"Updating chain swap {swap_id}: accepted_receiver_amount_sat = {accepted_receiver_amount_sat:?}"
|
||||
);
|
||||
let mut con: Connection = self.get_connection()?;
|
||||
let tx = con.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
||||
|
||||
@@ -2651,10 +2651,16 @@ impl LiquidSdk {
|
||||
PaymentError::InvalidOrExpiredFees
|
||||
);
|
||||
|
||||
self.swapper
|
||||
.accept_zero_amount_chain_swap_quote(&swap_id, server_lockup_quote.to_sat())?;
|
||||
self.persister
|
||||
.update_accepted_receiver_amount(&swap_id, payer_amount_sat - fees_sat)?;
|
||||
.update_accepted_receiver_amount(&swap_id, Some(payer_amount_sat - fees_sat))?;
|
||||
self.swapper
|
||||
.accept_zero_amount_chain_swap_quote(&swap_id, server_lockup_quote.to_sat())
|
||||
.inspect_err(|e| {
|
||||
error!("Failed to accept zero-amount swap {swap_id} quote: {e} - trying to erase the accepted receiver amount...");
|
||||
let _ = self
|
||||
.persister
|
||||
.update_accepted_receiver_amount(&swap_id, None);
|
||||
})?;
|
||||
self.chain_swap_handler.update_swap_info(&ChainSwapUpdate {
|
||||
swap_id,
|
||||
to_state: Pending,
|
||||
|
||||
Reference in New Issue
Block a user