From 6add85a06e19ddd08e3f7fd93da22484b1fe14c9 Mon Sep 17 00:00:00 2001 From: ok300 <106775972+ok300@users.noreply.github.com> Date: Fri, 22 Mar 2024 19:40:24 +0100 Subject: [PATCH] Simplify reverse swap amounts calculation --- lib/src/lib.rs | 9 ++++++++ lib/src/wallet.rs | 53 +++++++++++++++++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 86a0db489..887c21707 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -10,6 +10,15 @@ const CLAIM_ABSOLUTE_FEES: u64 = 134; const DEFAULT_DATA_DIR: &str = ".data"; const DEFAULT_ELECTRUM_URL: &str = "blockstream.info:465"; +#[macro_export] +macro_rules! ensure_sdk { + ($cond:expr, $err:expr) => { + if !$cond { + return Err($err); + } + }; +} + #[cfg(test)] mod tests { use std::{env, fs, io, path::PathBuf, str::FromStr}; diff --git a/lib/src/wallet.rs b/lib/src/wallet.rs index d4447f5b4..5b307d5b0 100644 --- a/lib/src/wallet.rs +++ b/lib/src/wallet.rs @@ -24,9 +24,9 @@ use lwk_wollet::{ }; use crate::{ - persist::Persister, Network, OngoingReceiveSwap, Payment, PaymentType, ReceivePaymentRequest, - SendPaymentResponse, SwapError, SwapLbtcResponse, WalletInfo, WalletOptions, - CLAIM_ABSOLUTE_FEES, DEFAULT_DATA_DIR, DEFAULT_ELECTRUM_URL, + ensure_sdk, persist::Persister, Network, OngoingReceiveSwap, Payment, PaymentType, + ReceivePaymentRequest, SendPaymentResponse, SwapError, SwapLbtcResponse, WalletInfo, + WalletOptions, CLAIM_ABSOLUTE_FEES, DEFAULT_DATA_DIR, DEFAULT_ELECTRUM_URL, }; pub struct Wallet { @@ -266,18 +266,46 @@ impl Wallet { &self, req: ReceivePaymentRequest, ) -> Result { - let mut amount_sat = req - .onchain_amount_sat - .or(req.invoice_amount_sat) - .ok_or(SwapError::AmountOutOfRange)?; - let client = self.boltz_client(); - let lbtc_pair = client.get_pairs()?.get_lbtc_pair()?; + let (onchain_amount_sat, invoice_amount_sat) = + match (req.onchain_amount_sat, req.invoice_amount_sat) { + (Some(onchain_amount_sat), None) => { + // TODO Calculate correct fees, once these PRs are merged and published + // https://github.com/SatoshiPortal/boltz-rust/pull/31 + // https://github.com/SatoshiPortal/boltz-rust/pull/32 + + // TODO Until above is fixed, this is only an approximation (using onchain instead of invoice amount to calc. fees) + let fees_boltz = lbtc_pair.fees.reverse_boltz(onchain_amount_sat)?; + let fees_lockup = lbtc_pair.fees.reverse_lockup()?; + let fees_claim = CLAIM_ABSOLUTE_FEES; // lbtc_pair.fees.reverse_claim_estimate(); + let fees_total = fees_boltz + fees_lockup + fees_claim; + + Ok((onchain_amount_sat, onchain_amount_sat + fees_total)) + } + (None, Some(invoice_amount_sat)) => { + let fees_boltz = lbtc_pair.fees.reverse_boltz(invoice_amount_sat)?; + let fees_lockup = lbtc_pair.fees.reverse_lockup()?; + let fees_claim = CLAIM_ABSOLUTE_FEES; // lbtc_pair.fees.reverse_claim_estimate(); + let fees_total = fees_boltz + fees_lockup + fees_claim; + + ensure_sdk!(invoice_amount_sat > fees_total, SwapError::AmountOutOfRange); + + Ok((invoice_amount_sat - fees_total, invoice_amount_sat)) + } + (None, None) => Err(SwapError::AmountOutOfRange), + + // TODO The request should not allow setting both invoice and onchain amounts, so this case shouldn't be possible. + // See example of how it's done in the SDK. + _ => Err(SwapError::BoltzGeneric { + err: "Both invoice and onchain amounts were specified".into(), + }), + }?; + lbtc_pair .limits - .within(amount_sat) + .within(invoice_amount_sat) .map_err(|_| SwapError::AmountOutOfRange)?; let mnemonic = self.signer.mnemonic(); @@ -290,19 +318,18 @@ impl Wallet { let preimage_hash = preimage.sha256.to_string(); let swap_response = if req.onchain_amount_sat.is_some() { - amount_sat += CLAIM_ABSOLUTE_FEES; client.create_swap(CreateSwapRequest::new_lbtc_reverse_onchain_amt( lbtc_pair.hash, preimage_hash.clone(), lsk.keypair.public_key().to_string(), - amount_sat, + onchain_amount_sat, ))? } else { client.create_swap(CreateSwapRequest::new_lbtc_reverse_invoice_amt( lbtc_pair.hash, preimage_hash.clone(), lsk.keypair.public_key().to_string(), - amount_sat, + invoice_amount_sat, ))? };