Skip to content

Commit

Permalink
Add ReceiveAmount and extend PayAmount to handle assets
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross committed Jan 8, 2025
1 parent 8e447d0 commit 2ece61c
Show file tree
Hide file tree
Showing 14 changed files with 1,342 additions and 378 deletions.
45 changes: 29 additions & 16 deletions cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub(crate) enum Command {
/// The amount in satoshi to pay, in case of a direct Liquid address
/// or amount-less BIP21
#[arg(short, long)]
amount_sat: Option<u64>,
receiver_amount: Option<u64>,

/// Optional id of the asset, in case of a direct Liquid address
/// or amount-less BIP21
Expand Down Expand Up @@ -77,11 +77,11 @@ pub(crate) enum Command {
#[arg(short = 'm', long = "method")]
payment_method: Option<PaymentMethod>,

/// Amount the payer will send, in satoshi units. If receiving a non Liquid Bitcoin
/// Amount the payer will send, in satoshi. If receiving a non Liquid Bitcoin
/// asset, this is the units of the asset to receive.
/// If not specified, it will generate a BIP21 URI/Liquid address with no amount
#[arg(short, long)]
payer_amount_sat: Option<u64>,
payer_amount: Option<u64>,

/// Optional id of the asset to receive when the 'payment_method' is "liquid"
#[clap(long = "asset")]
Expand Down Expand Up @@ -280,21 +280,29 @@ pub(crate) async fn handle_command(
Ok(match command {
Command::ReceivePayment {
payment_method,
payer_amount_sat,
payer_amount,
asset_id,
description,
use_description_hash,
} => {
let amount = match asset_id {
Some(asset_id) => Some(ReceiveAmount::Asset {
asset_id,
payer_amount,
}),
None => {
payer_amount.map(|payer_amount_sat| ReceiveAmount::Bitcoin { payer_amount_sat })
}
};
let prepare_response = sdk
.prepare_receive_payment(&PrepareReceiveRequest {
payer_amount_sat,
asset_id,
payment_method: payment_method.unwrap_or(PaymentMethod::Lightning),
amount: amount.clone(),
})
.await?;

let fees = prepare_response.fees_sat;
let confirmation_msg = match payer_amount_sat {
let confirmation_msg = match amount {
Some(_) => format!("Fees: {fees} sat. Are the fees acceptable? (y/N)"),
None => {
let min = prepare_response.min_payer_amount_sat;
Expand Down Expand Up @@ -348,14 +356,14 @@ pub(crate) async fn handle_command(
invoice,
offer,
address,
amount_sat,
receiver_amount,
asset_id,
drain,
delay,
} => {
let destination = match (invoice, offer, address) {
(Some(invoice), None, None) => Ok(invoice),
(None, Some(offer), None) => match amount_sat {
(None, Some(offer), None) => match receiver_amount {
Some(_) => Ok(offer),
None => Err(anyhow!(
"Must specify an amount for a BOLT12 offer."
Expand All @@ -371,17 +379,22 @@ pub(crate) async fn handle_command(
"Must specify either a BOLT11 invoice, a BOLT12 offer or a direct/BIP21 address."
))
}?;
let amount = match (amount_sat, drain.unwrap_or(false)) {
(Some(amount_sat), _) => Some(PayAmount::Receiver { amount_sat }),
(_, true) => Some(PayAmount::Drain),
(_, _) => None,
let amount = match (asset_id, receiver_amount, drain.unwrap_or(false)) {
(Some(asset_id), Some(receiver_amount), _) => Some(PayAmount::Asset {
asset_id,
receiver_amount,
}),
(None, Some(receiver_amount_sat), _) => Some(PayAmount::Bitcoin {
receiver_amount_sat,
}),
(_, _, true) => Some(PayAmount::Drain),
_ => None,
};

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

Expand Down Expand Up @@ -418,8 +431,8 @@ pub(crate) async fn handle_command(
} => {
let amount = match drain.unwrap_or(false) {
true => PayAmount::Drain,
false => PayAmount::Receiver {
amount_sat: receiver_amount_sat.ok_or(anyhow::anyhow!(
false => PayAmount::Bitcoin {
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 @@ -355,12 +355,18 @@ 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_PayAmount_Receiver {
uint64_t amount_sat;
} wire_cst_PayAmount_Receiver;
typedef struct wire_cst_PayAmount_Bitcoin {
uint64_t receiver_amount_sat;
} wire_cst_PayAmount_Bitcoin;

typedef struct wire_cst_PayAmount_Asset {
struct wire_cst_list_prim_u_8_strict *asset_id;
uint64_t receiver_amount;
} wire_cst_PayAmount_Asset;

typedef union PayAmountKind {
struct wire_cst_PayAmount_Receiver Receiver;
struct wire_cst_PayAmount_Bitcoin Bitcoin;
struct wire_cst_PayAmount_Asset Asset;
} PayAmountKind;

typedef struct wire_cst_pay_amount {
Expand All @@ -373,10 +379,28 @@ typedef struct wire_cst_prepare_pay_onchain_request {
uint32_t *fee_rate_sat_per_vbyte;
} wire_cst_prepare_pay_onchain_request;

typedef struct wire_cst_ReceiveAmount_Bitcoin {
uint64_t payer_amount_sat;
} wire_cst_ReceiveAmount_Bitcoin;

typedef struct wire_cst_ReceiveAmount_Asset {
struct wire_cst_list_prim_u_8_strict *asset_id;
uint64_t *payer_amount;
} wire_cst_ReceiveAmount_Asset;

typedef union ReceiveAmountKind {
struct wire_cst_ReceiveAmount_Bitcoin Bitcoin;
struct wire_cst_ReceiveAmount_Asset Asset;
} ReceiveAmountKind;

typedef struct wire_cst_receive_amount {
int32_t tag;
union ReceiveAmountKind kind;
} wire_cst_receive_amount;

typedef struct wire_cst_prepare_receive_request {
int32_t payment_method;
uint64_t *payer_amount_sat;
struct wire_cst_list_prim_u_8_strict *asset_id;
struct wire_cst_receive_amount *amount;
} wire_cst_prepare_receive_request;

typedef struct wire_cst_prepare_refund_request {
Expand All @@ -388,13 +412,11 @@ typedef struct wire_cst_prepare_refund_request {
typedef struct wire_cst_prepare_send_request {
struct wire_cst_list_prim_u_8_strict *destination;
struct wire_cst_pay_amount *amount;
struct wire_cst_list_prim_u_8_strict *asset_id;
} wire_cst_prepare_send_request;

typedef struct wire_cst_prepare_receive_response {
int32_t payment_method;
uint64_t *payer_amount_sat;
struct wire_cst_list_prim_u_8_strict *asset_id;
struct wire_cst_receive_amount *amount;
uint64_t fees_sat;
uint64_t *min_payer_amount_sat;
uint64_t *max_payer_amount_sat;
Expand Down Expand Up @@ -1339,6 +1361,8 @@ struct wire_cst_prepare_refund_request *frbgen_breez_liquid_cst_new_box_autoadd_

struct wire_cst_prepare_send_request *frbgen_breez_liquid_cst_new_box_autoadd_prepare_send_request(void);

struct wire_cst_receive_amount *frbgen_breez_liquid_cst_new_box_autoadd_receive_amount(void);

struct wire_cst_receive_payment_request *frbgen_breez_liquid_cst_new_box_autoadd_receive_payment_request(void);

struct wire_cst_refund_request *frbgen_breez_liquid_cst_new_box_autoadd_refund_request(void);
Expand Down Expand Up @@ -1435,6 +1459,7 @@ static int64_t dummy_method_to_enforce_bundling(void) {
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_prepare_receive_request);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_prepare_refund_request);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_prepare_send_request);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_receive_amount);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_receive_payment_request);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_refund_request);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_box_autoadd_restore_request);
Expand Down
16 changes: 10 additions & 6 deletions lib/bindings/src/breez_sdk_liquid.udl
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ dictionary LnUrlPayRequest {
dictionary PrepareSendRequest {
string destination;
PayAmount? amount = null;
string? asset_id = null;
};

[Enum]
Expand Down Expand Up @@ -438,17 +437,21 @@ enum PaymentMethod {
"LiquidAddress",
};

[Enum]
interface ReceiveAmount {
Bitcoin(u64 payer_amount_sat);
Asset(string asset_id, u64? payer_amount);
};

dictionary PrepareReceiveRequest {
PaymentMethod payment_method;
u64? payer_amount_sat = null;
string? asset_id = null;
ReceiveAmount? amount = null;
};

dictionary PrepareReceiveResponse {
PaymentMethod payment_method;
u64 fees_sat;
u64? payer_amount_sat;
string? asset_id;
ReceiveAmount? amount;
u64? min_payer_amount_sat;
u64? max_payer_amount_sat;
f64? swapper_feerate;
Expand Down Expand Up @@ -482,7 +485,8 @@ dictionary OnchainPaymentLimitsResponse {

[Enum]
interface PayAmount {
Receiver(u64 amount_sat);
Bitcoin(u64 receiver_amount_sat);
Asset(string asset_id, u64 receiver_amount);
Drain();
};

Expand Down
Loading

0 comments on commit 2ece61c

Please sign in to comment.