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

Liquid/Lightning drain #553

Merged
merged 6 commits into from
Nov 11, 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
18 changes: 14 additions & 4 deletions cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ pub(crate) enum Command {
#[arg(short, long)]
amount_sat: Option<u64>,

/// Delay for the send, in seconds
/// Whether or not this is a drain operation. If true, all available funds will be used.
#[arg(short, long)]
drain: Option<bool>,

/// Delay for the send, in seconds
#[arg(long)]
delay: Option<u64>,
},
/// Fetch the current limits for Send and Receive payments
Expand Down Expand Up @@ -310,6 +314,7 @@ pub(crate) async fn handle_command(
invoice,
address,
amount_sat,
drain,
delay,
} => {
let destination = match (invoice, address) {
Expand All @@ -326,11 +331,16 @@ pub(crate) async fn handle_command(
))
}
};
let amount = match (amount_sat, drain.unwrap_or(false)) {
(Some(amount_sat), _) => Some(PayAmount::Receiver { amount_sat }),
(_, true) => Some(PayAmount::Drain),
(_, _) => None,
};

let prepare_response = sdk
.prepare_send_payment(&PrepareSendRequest {
destination,
amount_sat,
amount,
})
.await?;

Expand Down Expand Up @@ -366,8 +376,8 @@ pub(crate) async fn handle_command(
fee_rate_sat_per_vbyte,
} => {
let amount = match drain.unwrap_or(false) {
true => PayOnchainAmount::Drain,
false => PayOnchainAmount::Receiver {
true => PayAmount::Drain,
false => PayAmount::Receiver {
amount_sat: receiver_amount_sat.ok_or(anyhow::anyhow!(
"Must specify `receiver_amount_sat` if not draining"
))?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,21 +279,21 @@ typedef struct wire_cst_prepare_ln_url_pay_request {
bool *validate_success_action_url;
} wire_cst_prepare_ln_url_pay_request;

typedef struct wire_cst_PayOnchainAmount_Receiver {
typedef struct wire_cst_PayAmount_Receiver {
uint64_t amount_sat;
} wire_cst_PayOnchainAmount_Receiver;
} wire_cst_PayAmount_Receiver;

typedef union PayOnchainAmountKind {
struct wire_cst_PayOnchainAmount_Receiver Receiver;
} PayOnchainAmountKind;
typedef union PayAmountKind {
struct wire_cst_PayAmount_Receiver Receiver;
} PayAmountKind;

typedef struct wire_cst_pay_onchain_amount {
typedef struct wire_cst_pay_amount {
int32_t tag;
union PayOnchainAmountKind kind;
} wire_cst_pay_onchain_amount;
union PayAmountKind kind;
} wire_cst_pay_amount;

typedef struct wire_cst_prepare_pay_onchain_request {
struct wire_cst_pay_onchain_amount amount;
struct wire_cst_pay_amount amount;
uint32_t *fee_rate_sat_per_vbyte;
} wire_cst_prepare_pay_onchain_request;

Expand All @@ -310,7 +310,7 @@ typedef struct wire_cst_prepare_refund_request {

typedef struct wire_cst_prepare_send_request {
struct wire_cst_list_prim_u_8_strict *destination;
uint64_t *amount_sat;
struct wire_cst_pay_amount *amount;
} wire_cst_prepare_send_request;

typedef struct wire_cst_prepare_receive_response {
Expand Down Expand Up @@ -1166,6 +1166,8 @@ struct wire_cst_ln_url_withdraw_success_data *frbgen_breez_liquid_cst_new_box_au

struct wire_cst_message_success_action_data *frbgen_breez_liquid_cst_new_box_autoadd_message_success_action_data(void);

struct wire_cst_pay_amount *frbgen_breez_liquid_cst_new_box_autoadd_pay_amount(void);

struct wire_cst_pay_onchain_request *frbgen_breez_liquid_cst_new_box_autoadd_pay_onchain_request(void);

struct wire_cst_payment *frbgen_breez_liquid_cst_new_box_autoadd_payment(void);
Expand Down Expand Up @@ -1253,6 +1255,7 @@ static int64_t dummy_method_to_enforce_bundling(void) {
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_ln_url_withdraw_request_data);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_ln_url_withdraw_success_data);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_message_success_action_data);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_pay_amount);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_pay_onchain_request);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_payment);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_prepare_buy_bitcoin_request);
Expand Down
6 changes: 3 additions & 3 deletions lib/bindings/src/breez_sdk_liquid.udl
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ dictionary LnUrlPayRequest {

dictionary PrepareSendRequest {
string destination;
u64? amount_sat = null;
PayAmount? amount = null;
};

[Enum]
Expand Down Expand Up @@ -439,13 +439,13 @@ dictionary OnchainPaymentLimitsResponse {
};

[Enum]
interface PayOnchainAmount {
interface PayAmount {
Receiver(u64 amount_sat);
Drain();
};

dictionary PreparePayOnchainRequest {
PayOnchainAmount amount;
PayAmount amount;
u32? fee_rate_sat_per_vbyte = null;
};

Expand Down
24 changes: 4 additions & 20 deletions lib/core/src/chain_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,30 +612,14 @@ impl ChainSwapHandler {
lockup_details.amount, lockup_details.lockup_address
);

let lockup_tx = match self
let lockup_tx = self
.onchain_wallet
.build_tx(
self.config
.lowball_fee_rate_msat_per_vbyte()
.map(|v| v as f32),
.build_tx_or_drain_tx(
self.config.lowball_fee_rate_msat_per_vbyte(),
&lockup_details.lockup_address,
lockup_details.amount,
)
.await
{
Err(PaymentError::InsufficientFunds) => {
warn!("Cannot build normal lockup tx due to insufficient funds, attempting to build drain tx");
self.onchain_wallet
.build_drain_tx(
None,
&lockup_details.lockup_address,
Some(lockup_details.amount),
)
.await
}
Err(e) => Err(e),
Ok(lockup_tx) => Ok(lockup_tx),
}?;
.await?;

let lockup_tx_id = self
.liquid_chain_service
Expand Down
Loading
Loading