Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit d5982f0

Browse files
Make amount required for invoices
1 parent 7b86c0f commit d5982f0

File tree

5 files changed

+18
-24
lines changed

5 files changed

+18
-24
lines changed

mutiny-core/src/lib.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub trait InvoiceHandler {
118118
) -> Result<MutinyInvoice, MutinyError>;
119119
async fn create_invoice(
120120
&self,
121-
amount: Option<u64>,
121+
amount: u64,
122122
labels: Vec<String>,
123123
) -> Result<MutinyInvoice, MutinyError>;
124124
}
@@ -1099,11 +1099,11 @@ impl<S: MutinyStorage> MutinyWallet<S> {
10991099
amount: Option<u64>,
11001100
labels: Vec<String>,
11011101
) -> Result<MutinyBip21RawMaterials, MutinyError> {
1102-
let invoice = if self.safe_mode {
1102+
let invoice = if self.safe_mode || amount.is_none() {
11031103
None
11041104
} else {
11051105
Some(
1106-
self.create_lightning_invoice(amount, labels.clone())
1106+
self.create_lightning_invoice(amount.expect("just checked"), labels.clone())
11071107
.await?
11081108
.bolt11
11091109
.ok_or(MutinyError::InvoiceCreationFailed)?,
@@ -1124,19 +1124,17 @@ impl<S: MutinyStorage> MutinyWallet<S> {
11241124

11251125
async fn create_lightning_invoice(
11261126
&self,
1127-
amount: Option<u64>,
1127+
amount: u64,
11281128
labels: Vec<String>,
11291129
) -> Result<MutinyInvoice, MutinyError> {
1130-
let amt = amount.map_or(Err(MutinyError::InvalidArgumentsError), Ok)?;
1131-
11321130
// Attempt to create federation invoice if available and below max amount
11331131
let federation_ids = self.list_federation_ids().await?;
1134-
if !federation_ids.is_empty() && amt <= MAX_FEDERATION_INVOICE_AMT {
1132+
if !federation_ids.is_empty() && amount <= MAX_FEDERATION_INVOICE_AMT {
11351133
let federation_id = &federation_ids[0];
11361134
let fedimint_client = self.federations.read().await.get(federation_id).cloned();
11371135

11381136
if let Some(client) = fedimint_client {
1139-
if let Ok(inv) = client.get_invoice(amt, labels.clone()).await {
1137+
if let Ok(inv) = client.get_invoice(amount, labels.clone()).await {
11401138
self.storage
11411139
.set_invoice_labels(inv.bolt11.clone().expect("just created"), labels)?;
11421140
return Ok(inv);
@@ -1145,7 +1143,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
11451143
}
11461144

11471145
// Fallback to node_manager invoice creation if no federation invoice created
1148-
let inv = self.node_manager.create_invoice(Some(amt)).await?;
1146+
let inv = self.node_manager.create_invoice(amount).await?;
11491147
self.storage
11501148
.set_invoice_labels(inv.bolt11.clone().expect("just created"), labels)?;
11511149
Ok(inv)
@@ -1927,7 +1925,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
19271925
// fixme: do we need to use this description?
19281926
let _description = withdraw.default_description.clone();
19291927
let mutiny_invoice = self
1930-
.create_invoice(Some(amount_sats), vec!["LNURL Withdrawal".to_string()])
1928+
.create_invoice(amount_sats, vec!["LNURL Withdrawal".to_string()])
19311929
.await?;
19321930
let invoice_str = mutiny_invoice.bolt11.expect("Invoice should have bolt11");
19331931
let res = self
@@ -1985,7 +1983,7 @@ impl<S: MutinyStorage> InvoiceHandler for MutinyWallet<S> {
19851983

19861984
async fn create_invoice(
19871985
&self,
1988-
amount: Option<u64>,
1986+
amount: u64,
19891987
labels: Vec<String>,
19901988
) -> Result<MutinyInvoice, MutinyError> {
19911989
self.create_lightning_invoice(amount, labels).await

mutiny-core/src/node.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ impl<S: MutinyStorage> Node<S> {
971971

972972
pub async fn create_invoice(
973973
&self,
974-
amount_sat: Option<u64>,
974+
amount_sat: u64,
975975
route_hints: Option<Vec<PhantomRouteHints>>,
976976
) -> Result<Bolt11Invoice, MutinyError> {
977977
match self.lsp_client.as_ref() {
@@ -982,9 +982,6 @@ impl<S: MutinyStorage> Node<S> {
982982
)
983983
.await?;
984984

985-
// LSP requires an amount:
986-
let amount_sat = amount_sat.ok_or(MutinyError::BadAmountError)?;
987-
988985
// Needs any amount over 0 if channel exists
989986
// Needs amount over minimum if no channel
990987
let inbound_capacity_msat: u64 = self
@@ -1112,7 +1109,7 @@ impl<S: MutinyStorage> Node<S> {
11121109
}
11131110
}
11141111
None => Ok(self
1115-
.create_internal_invoice(amount_sat, None, route_hints)
1112+
.create_internal_invoice(Some(amount_sat), None, route_hints)
11161113
.await?),
11171114
}
11181115
}
@@ -2420,7 +2417,7 @@ mod tests {
24202417

24212418
let amount_sats = 1_000;
24222419

2423-
let invoice = node.create_invoice(Some(amount_sats), None).await.unwrap();
2420+
let invoice = node.create_invoice(amount_sats, None).await.unwrap();
24242421

24252422
assert_eq!(invoice.amount_milli_satoshis(), Some(amount_sats * 1000));
24262423
match invoice.description() {
@@ -2451,7 +2448,7 @@ mod tests {
24512448
let storage = MemoryStorage::default();
24522449
let node = create_node(storage).await;
24532450

2454-
let invoice = node.create_invoice(Some(10_000), None).await.unwrap();
2451+
let invoice = node.create_invoice(10_000, None).await.unwrap();
24552452

24562453
let result = node
24572454
.pay_invoice_with_timeout(&invoice, None, None, vec![])
@@ -2574,7 +2571,7 @@ mod wasm_test {
25742571

25752572
let amount_sats = 1_000;
25762573

2577-
let invoice = node.create_invoice(Some(amount_sats), None).await.unwrap();
2574+
let invoice = node.create_invoice(amount_sats, None).await.unwrap();
25782575

25792576
assert_eq!(invoice.amount_milli_satoshis(), Some(amount_sats * 1000));
25802577
match invoice.description() {
@@ -2605,7 +2602,7 @@ mod wasm_test {
26052602
let storage = MemoryStorage::default();
26062603
let node = create_node(storage).await;
26072604

2608-
let invoice = node.create_invoice(Some(10_000), None).await.unwrap();
2605+
let invoice = node.create_invoice(10_000, None).await.unwrap();
26092606

26102607
let result = node
26112608
.pay_invoice_with_timeout(&invoice, None, None, vec![])

mutiny-core/src/nodemanager.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,12 +1365,11 @@ impl<S: MutinyStorage> NodeManager<S> {
13651365
// all values in sats
13661366

13671367
/// Creates a lightning invoice. The amount should be in satoshis.
1368-
/// If no amount is provided, the invoice will be created with no amount.
13691368
/// If no description is provided, the invoice will be created with no description.
13701369
///
13711370
/// If the manager has more than one node it will create a phantom invoice.
13721371
/// If there is only one node it will create an invoice just for that node.
1373-
pub async fn create_invoice(&self, amount: Option<u64>) -> Result<MutinyInvoice, MutinyError> {
1372+
pub async fn create_invoice(&self, amount: u64) -> Result<MutinyInvoice, MutinyError> {
13741373
let nodes = self.nodes.lock().await;
13751374
let use_phantom = nodes.len() > 1 && self.lsp_config.is_none();
13761375
if nodes.len() == 0 {

mutiny-core/src/nostr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ impl<S: MutinyStorage> NostrManager<S> {
996996
client.connect().await;
997997

998998
let invoice = invoice_handler
999-
.create_invoice(Some(amount_sats), vec!["Gift".to_string()])
999+
.create_invoice(amount_sats, vec!["Gift".to_string()])
10001000
.await?;
10011001
// unwrap is safe, we just created it
10021002
let bolt11 = invoice.bolt11.unwrap();

mutiny-wasm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ impl MutinyWallet {
766766
#[wasm_bindgen]
767767
pub async fn create_invoice(
768768
&self,
769-
amount: Option<u64>,
769+
amount: u64,
770770
labels: Vec<String>,
771771
) -> Result<MutinyInvoice, MutinyJsError> {
772772
Ok(self.inner.create_invoice(amount, labels).await?.into())

0 commit comments

Comments
 (0)