Skip to content

Commit a6b988c

Browse files
committed
refactor: Set optional amount for creating invoice
1 parent 2f9fd97 commit a6b988c

File tree

6 files changed

+44
-39
lines changed

6 files changed

+44
-39
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mutiny-core/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub trait InvoiceHandler {
124124
) -> Result<MutinyInvoice, MutinyError>;
125125
async fn create_invoice(
126126
&self,
127-
amount: u64,
127+
amount: Option<u64>,
128128
labels: Vec<String>,
129129
expiry_delta_secs: Option<u32>,
130130
) -> Result<MutinyInvoice, MutinyError>;
@@ -1272,7 +1272,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
12721272
None
12731273
} else {
12741274
Some(
1275-
self.create_lightning_invoice(amount.expect("just checked"), labels.clone(), None)
1275+
self.create_lightning_invoice(amount, labels.clone(), None)
12761276
.await?
12771277
.bolt11
12781278
.ok_or(MutinyError::InvoiceCreationFailed)?,
@@ -1428,7 +1428,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
14281428

14291429
async fn create_lightning_invoice(
14301430
&self,
1431-
amount: u64,
1431+
amount: Option<u64>,
14321432
labels: Vec<String>,
14331433
expiry_delta_secs: Option<u32>,
14341434
) -> Result<MutinyInvoice, MutinyError> {
@@ -2038,7 +2038,7 @@ impl<S: MutinyStorage> InvoiceHandler for MutinyWallet<S> {
20382038

20392039
async fn create_invoice(
20402040
&self,
2041-
amount: u64,
2041+
amount: Option<u64>,
20422042
labels: Vec<String>,
20432043
expiry_delta_secs: Option<u32>,
20442044
) -> Result<MutinyInvoice, MutinyError> {

mutiny-core/src/node.rs

+36-31
Original file line numberDiff line numberDiff line change
@@ -1334,48 +1334,47 @@ impl<S: MutinyStorage> Node<S> {
13341334

13351335
pub async fn create_invoice(
13361336
&self,
1337-
amount_sat: u64,
1337+
amount_sat: Option<u64>,
13381338
route_hints: Option<Vec<PhantomRouteHints>>,
13391339
labels: Vec<String>,
13401340
expiry_delta_secs: Option<u32>,
13411341
) -> Result<(Bolt11Invoice, u64), MutinyError> {
13421342
log_trace!(self.logger, "calling create_invoice");
13431343

1344-
if amount_sat < 1 {
1345-
return Err(MutinyError::BadAmountError);
1346-
}
1347-
13481344
let res = match self.lsp_client.as_ref() {
13491345
Some(lsp) => {
13501346
let connect = lsp.get_lsp_connection_string().await;
13511347
self.connect_peer(PubkeyConnectionInfo::new(&connect)?, None)
13521348
.await?;
1349+
if let Some(amount) = amount_sat {
1350+
if amount < 1 {
1351+
return Err(MutinyError::BadAmountError);
1352+
}
1353+
let inbound_capacity_msat: u64 = self.get_inbound_capacity_msat();
1354+
log_debug!(self.logger, "Current inbound liquidity {inbound_capacity_msat}msats, creating invoice for {}msats", amount * 1000);
13531355

1354-
let inbound_capacity_msat: u64 = self.get_inbound_capacity_msat();
1355-
log_debug!(self.logger, "Current inbound liquidity {inbound_capacity_msat}msats, creating invoice for {}msats", amount_sat * 1000);
1356-
1357-
if inbound_capacity_msat < amount_sat * 1_000 {
1358-
log_debug!(
1359-
self.logger,
1360-
"Inbound capacity insufficient, try to resume disconnect channels..."
1361-
);
1362-
if let Err(err) = self.try_connect_unusable_channel_peers().await {
1356+
if inbound_capacity_msat < amount * 1_000 {
13631357
log_debug!(
13641358
self.logger,
1365-
"try connect unusable_channel_peers error {err:?}"
1359+
"Inbound capacity insufficient, try to resume disconnect channels..."
13661360
);
1367-
}
1361+
if let Err(err) = self.try_connect_unusable_channel_peers().await {
1362+
log_debug!(
1363+
self.logger,
1364+
"try connect unusable_channel_peers error {err:?}"
1365+
);
1366+
}
13681367

1369-
let inbound_capacity_msat: u64 = self.get_inbound_capacity_msat();
1370-
log_debug!(self.logger, "Current inbound liquidity {inbound_capacity_msat}msats, creating invoice for {}msats", amount_sat * 1000);
1371-
if inbound_capacity_msat < amount_sat * 1_000 {
1372-
return Err(MutinyError::InsufficientBalance);
1368+
let inbound_capacity_msat: u64 = self.get_inbound_capacity_msat();
1369+
log_debug!(self.logger, "Current inbound liquidity {inbound_capacity_msat}msats, creating invoice for {}msats", amount * 1000);
1370+
if inbound_capacity_msat < amount * 1_000 {
1371+
return Err(MutinyError::InsufficientBalance);
1372+
}
13731373
}
13741374
}
1375-
13761375
Ok((
13771376
self.create_internal_invoice(
1378-
Some(amount_sat),
1377+
amount_sat,
13791378
None,
13801379
route_hints,
13811380
labels,
@@ -1387,7 +1386,7 @@ impl<S: MutinyStorage> Node<S> {
13871386
}
13881387
None => Ok((
13891388
self.create_internal_invoice(
1390-
Some(amount_sat),
1389+
amount_sat,
13911390
None,
13921391
route_hints,
13931392
labels,
@@ -2826,14 +2825,17 @@ mod tests {
28262825

28272826
let now = crate::utils::now().as_secs();
28282827

2829-
let amount_sats = 1_000;
2828+
let amount_sats = Some(1_000);
28302829

28312830
let (invoice, _) = node
28322831
.create_invoice(amount_sats, None, vec![], None)
28332832
.await
28342833
.unwrap();
28352834

2836-
assert_eq!(invoice.amount_milli_satoshis(), Some(amount_sats * 1000));
2835+
assert_eq!(
2836+
invoice.amount_milli_satoshis(),
2837+
amount_sats.map(|amount| amount * 1000)
2838+
);
28372839
match invoice.description() {
28382840
Bolt11InvoiceDescription::Direct(desc) => {
28392841
assert_eq!(desc.to_string(), "");
@@ -2850,7 +2852,7 @@ mod tests {
28502852
assert_eq!(from_storage.payment_hash, invoice.payment_hash().to_owned());
28512853
assert_eq!(from_storage.preimage, None);
28522854
assert_eq!(from_storage.payee_pubkey, None);
2853-
assert_eq!(from_storage.amount_sats, Some(amount_sats));
2855+
assert_eq!(from_storage.amount_sats, amount_sats);
28542856
assert_eq!(from_storage.status, HTLCStatus::Pending);
28552857
assert_eq!(from_storage.fees_paid, None);
28562858
assert!(from_storage.inbound);
@@ -2863,7 +2865,7 @@ mod tests {
28632865
let node = create_node(storage).await;
28642866

28652867
let invoice = node
2866-
.create_invoice(10_000, None, vec![], None)
2868+
.create_invoice(Some(10_000), None, vec![], None)
28672869
.await
28682870
.unwrap()
28692871
.0;
@@ -2994,7 +2996,7 @@ mod wasm_test {
29942996

29952997
let now = crate::utils::now().as_secs();
29962998

2997-
let amount_sats = 1_000;
2999+
let amount_sats = Some(1_000);
29983000
let label = "test".to_string();
29993001
let labels = vec![label.clone()];
30003002

@@ -3003,7 +3005,10 @@ mod wasm_test {
30033005
.await
30043006
.unwrap();
30053007

3006-
assert_eq!(invoice.amount_milli_satoshis(), Some(amount_sats * 1000));
3008+
assert_eq!(
3009+
invoice.amount_milli_satoshis(),
3010+
amount_sats.map(|amount| amount * 1000)
3011+
);
30073012
match invoice.description() {
30083013
Bolt11InvoiceDescription::Direct(desc) => {
30093014
assert_eq!(desc.to_string(), "test");
@@ -3020,7 +3025,7 @@ mod wasm_test {
30203025
assert_eq!(from_storage.payment_hash, invoice.payment_hash().to_owned());
30213026
assert_eq!(from_storage.preimage, None);
30223027
assert_eq!(from_storage.payee_pubkey, None);
3023-
assert_eq!(from_storage.amount_sats, Some(amount_sats));
3028+
assert_eq!(from_storage.amount_sats, amount_sats);
30243029
assert_eq!(from_storage.status, HTLCStatus::Pending);
30253030
assert_eq!(from_storage.fees_paid, None);
30263031
assert_eq!(from_storage.labels, labels.clone());
@@ -3046,7 +3051,7 @@ mod wasm_test {
30463051
let node = create_node(storage).await;
30473052

30483053
let invoice = node
3049-
.create_invoice(10_000, None, vec![], None)
3054+
.create_invoice(Some(10_000), None, vec![], None)
30503055
.await
30513056
.unwrap()
30523057
.0;

mutiny-core/src/nodemanager.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ impl<S: MutinyStorage> NodeManager<S> {
14751475
/// If there is only one node it will create an invoice just for that node.
14761476
pub async fn create_invoice(
14771477
&self,
1478-
amount: u64,
1478+
amount: Option<u64>,
14791479
labels: Vec<String>,
14801480
expiry_delta_secs: Option<u32>,
14811481
) -> Result<(MutinyInvoice, u64), MutinyError> {

mutiny-wasm/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cargo-features = ["per-package-target"]
22

33
[package]
44
name = "mutiny-wasm"
5-
version = "1.11.0"
5+
version = "1.11.1"
66
edition = "2021"
77
authors = ["utxostack"]
88
forced-target = "wasm32-unknown-unknown"

mutiny-wasm/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ impl MutinyWallet {
866866
#[wasm_bindgen]
867867
pub async fn create_invoice(
868868
&self,
869-
amount: u64,
869+
amount: Option<u64>,
870870
label: String,
871871
expiry_delta_secs: Option<u32>,
872872
) -> Result<MutinyInvoice, MutinyJsError> {

0 commit comments

Comments
 (0)