Skip to content

Commit

Permalink
implement max_onchain_amount
Browse files Browse the repository at this point in the history
  • Loading branch information
roeierez committed Nov 5, 2023
1 parent 1b559da commit fb7ee74
Show file tree
Hide file tree
Showing 12 changed files with 754 additions and 32 deletions.
3 changes: 2 additions & 1 deletion libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ enum LnUrlPayError {
"PaymentFailed",
"PaymentTimeout",
"RouteNotFound",
"RouteTooExpensive",
"RouteTooExpensive",
"ServiceConnectivity",
};

Expand Down Expand Up @@ -143,6 +143,7 @@ dictionary LNInvoice {
u64 expiry;
sequence<RouteHint> routing_hints;
sequence<u8> payment_secret;
u64 min_final_cltv_expiry_delta;
};

dictionary UnspentTransactionOutput {
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-core/src/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl BackupWorker {
Ok((new_version, data))
}
Err(e) => {
debug!("Optimistic sync failed, trying to sync remote changes {e}");
info!("Optimistic sync failed, trying to sync remote changes {e}");
// We need to sync remote changes and then retry the push
self.sync_remote_and_push(sync_dir, data, &mut last_sync_request_id)
.await
Expand Down
30 changes: 30 additions & 0 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,36 @@ impl BreezServices {
Ok(res)
}

/// Returns the max amount that can be sent on-chain using the send_onchain method.
/// The returned amount is the sum of the max amount that can be sent on each channel
/// minus the expected fees.
/// This is possible since the route to the swapper node is known in advance ans is expected
/// to consist of maximum 3 hops.
pub async fn max_onchain_payment(
&self,
_request: MaxOnchainPaymentRequest,
) -> Result<MaxOnchainPaymentResponse> {
// fetch the last hop hints from the swapper
let last_hop = self.btc_send_swapper.last_hop_for_payment().await?;

// calculate the largest payment we can send over this route using maximum 3 hops
// as follows:
// User Node -> LSP Node -> Routing Node -> Swapper Node
let (max_to_pay, _) = self
.node_api
.max_amount_to_send(
Some(hex::decode(&last_hop.src_node_id)?),
swap_out::reverseswap::MAX_PAYMENT_PATH_HOPS,
Some(&last_hop),
)
.await?;

// Sum the max amount per channel and return the result
let total = max_to_pay.into_iter().map(|m| m.amount_msat).sum();

Ok(MaxOnchainPaymentResponse { total })
}

/// Creates a reverse swap and attempts to pay the HODL invoice
pub async fn send_onchain(
&self,
Expand Down
Loading

0 comments on commit fb7ee74

Please sign in to comment.