Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate reverse swap amounts calculation #55

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
53 changes: 40 additions & 13 deletions lib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -266,18 +266,46 @@ impl Wallet {
&self,
req: ReceivePaymentRequest,
) -> Result<SwapLbtcResponse, SwapError> {
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();
Expand All @@ -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,
))?
};

Expand Down