Skip to content

Commit

Permalink
Combine max_reverse_swap_amount and onchain_payment_limits (breez…
Browse files Browse the repository at this point in the history
…#850)

* Merge max_reverse_swap_amt into onchain_payment_limits

* Update bindings

* Combine both max fields into one
  • Loading branch information
ok300 authored Mar 8, 2024
1 parent c6b9f9a commit 908bed4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
21 changes: 17 additions & 4 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::min;
use std::fs::OpenOptions;
use std::io::Write;
use std::str::FromStr;
Expand Down Expand Up @@ -841,6 +842,8 @@ impl BreezServices {
/// minus the expected fees.
/// This is possible since the route to the swapper node is known in advance and is expected
/// to consist of maximum 3 hops.
///
/// Deprecated. Please use [BreezServices::onchain_payment_limits] instead.
pub async fn max_reverse_swap_amount(&self) -> SdkResult<MaxReverseSwapAmountResponse> {
// fetch the last hop hints from the swapper
let last_hop = self.btc_send_swapper.last_hop_for_payment().await?;
Expand Down Expand Up @@ -921,10 +924,20 @@ impl BreezServices {

pub async fn onchain_payment_limits(&self) -> SdkResult<OnchainPaymentLimitsResponse> {
let fee_info = self.btc_send_swapper.fetch_reverse_swap_fees().await?;
Ok(OnchainPaymentLimitsResponse {
min_sat: fee_info.min,
max_sat: fee_info.max,
})
debug!("Reverse swap pair info: {fee_info:?}");
let max_amt_current_channels = self.max_reverse_swap_amount().await?;
debug!("Max send amount possible with current channels: {max_amt_current_channels:?}");

let composite_max = min(fee_info.max, max_amt_current_channels.total_sat);
let (min_sat, max_sat) = match composite_max < fee_info.min {
true => {
warn!("Reverse swap max < min, setting limits to zero because no reverse swap is possible");
(0, 0)
}
false => (fee_info.min, composite_max),
};

Ok(OnchainPaymentLimitsResponse { min_sat, max_sat })
}

/// Supersedes [BreezServices::fetch_reverse_swap_fees]
Expand Down
6 changes: 6 additions & 0 deletions libs/sdk-core/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,13 @@ pub struct PrepareOnchainPaymentRequest {

#[derive(Serialize)]
pub struct OnchainPaymentLimitsResponse {
/// Minimum amount that can be sent. This value is influenced by
/// - what can be sent given the available channels and balance
/// - the lower limit of what the reverse swap service accepts as a send amount
pub min_sat: u64,
/// Maximum amount that can be sent. This value is influenced by
/// - what can be sent given the available channels and balance
/// - the upper limit of what the reverse swap service accepts as a send amount
pub max_sat: u64,
}

Expand Down
7 changes: 7 additions & 0 deletions libs/sdk-flutter/lib/bridge_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,14 @@ class NodeState {
}

class OnchainPaymentLimitsResponse {
/// Minimum amount that can be sent. This value is influenced by
/// - what can be sent given the available channels and balance
/// - the lower limit of what the reverse swap service accepts as a send amount
final int minSat;

/// Maximum amount that can be sent. This value is influenced by
/// - what can be sent given the available channels and balance
/// - the upper limit of what the reverse swap service accepts as a send amount
final int maxSat;

const OnchainPaymentLimitsResponse({
Expand Down

0 comments on commit 908bed4

Please sign in to comment.