@@ -1334,48 +1334,47 @@ impl<S: MutinyStorage> Node<S> {
1334
1334
1335
1335
pub async fn create_invoice (
1336
1336
& self ,
1337
- amount_sat : u64 ,
1337
+ amount_sat : Option < u64 > ,
1338
1338
route_hints : Option < Vec < PhantomRouteHints > > ,
1339
1339
labels : Vec < String > ,
1340
1340
expiry_delta_secs : Option < u32 > ,
1341
1341
) -> Result < ( Bolt11Invoice , u64 ) , MutinyError > {
1342
1342
log_trace ! ( self . logger, "calling create_invoice" ) ;
1343
1343
1344
- if amount_sat < 1 {
1345
- return Err ( MutinyError :: BadAmountError ) ;
1346
- }
1347
-
1348
1344
let res = match self . lsp_client . as_ref ( ) {
1349
1345
Some ( lsp) => {
1350
1346
let connect = lsp. get_lsp_connection_string ( ) . await ;
1351
1347
self . connect_peer ( PubkeyConnectionInfo :: new ( & connect) ?, None )
1352
1348
. 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 ) ;
1353
1355
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 {
1363
1357
log_debug ! (
1364
1358
self . logger,
1365
- "try connect unusable_channel_peers error {err:?} "
1359
+ "Inbound capacity insufficient, try to resume disconnect channels... "
1366
1360
) ;
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
+ }
1368
1367
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
+ }
1373
1373
}
1374
1374
}
1375
-
1376
1375
Ok ( (
1377
1376
self . create_internal_invoice (
1378
- Some ( amount_sat) ,
1377
+ amount_sat,
1379
1378
None ,
1380
1379
route_hints,
1381
1380
labels,
@@ -1387,7 +1386,7 @@ impl<S: MutinyStorage> Node<S> {
1387
1386
}
1388
1387
None => Ok ( (
1389
1388
self . create_internal_invoice (
1390
- Some ( amount_sat) ,
1389
+ amount_sat,
1391
1390
None ,
1392
1391
route_hints,
1393
1392
labels,
@@ -2826,14 +2825,17 @@ mod tests {
2826
2825
2827
2826
let now = crate :: utils:: now ( ) . as_secs ( ) ;
2828
2827
2829
- let amount_sats = 1_000 ;
2828
+ let amount_sats = Some ( 1_000 ) ;
2830
2829
2831
2830
let ( invoice, _) = node
2832
2831
. create_invoice ( amount_sats, None , vec ! [ ] , None )
2833
2832
. await
2834
2833
. unwrap ( ) ;
2835
2834
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
+ ) ;
2837
2839
match invoice. description ( ) {
2838
2840
Bolt11InvoiceDescription :: Direct ( desc) => {
2839
2841
assert_eq ! ( desc. to_string( ) , "" ) ;
@@ -2850,7 +2852,7 @@ mod tests {
2850
2852
assert_eq ! ( from_storage. payment_hash, invoice. payment_hash( ) . to_owned( ) ) ;
2851
2853
assert_eq ! ( from_storage. preimage, None ) ;
2852
2854
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) ;
2854
2856
assert_eq ! ( from_storage. status, HTLCStatus :: Pending ) ;
2855
2857
assert_eq ! ( from_storage. fees_paid, None ) ;
2856
2858
assert ! ( from_storage. inbound) ;
@@ -2863,7 +2865,7 @@ mod tests {
2863
2865
let node = create_node ( storage) . await ;
2864
2866
2865
2867
let invoice = node
2866
- . create_invoice ( 10_000 , None , vec ! [ ] , None )
2868
+ . create_invoice ( Some ( 10_000 ) , None , vec ! [ ] , None )
2867
2869
. await
2868
2870
. unwrap ( )
2869
2871
. 0 ;
@@ -2994,7 +2996,7 @@ mod wasm_test {
2994
2996
2995
2997
let now = crate :: utils:: now ( ) . as_secs ( ) ;
2996
2998
2997
- let amount_sats = 1_000 ;
2999
+ let amount_sats = Some ( 1_000 ) ;
2998
3000
let label = "test" . to_string ( ) ;
2999
3001
let labels = vec ! [ label. clone( ) ] ;
3000
3002
@@ -3003,7 +3005,10 @@ mod wasm_test {
3003
3005
. await
3004
3006
. unwrap ( ) ;
3005
3007
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
+ ) ;
3007
3012
match invoice. description ( ) {
3008
3013
Bolt11InvoiceDescription :: Direct ( desc) => {
3009
3014
assert_eq ! ( desc. to_string( ) , "test" ) ;
@@ -3020,7 +3025,7 @@ mod wasm_test {
3020
3025
assert_eq ! ( from_storage. payment_hash, invoice. payment_hash( ) . to_owned( ) ) ;
3021
3026
assert_eq ! ( from_storage. preimage, None ) ;
3022
3027
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) ;
3024
3029
assert_eq ! ( from_storage. status, HTLCStatus :: Pending ) ;
3025
3030
assert_eq ! ( from_storage. fees_paid, None ) ;
3026
3031
assert_eq ! ( from_storage. labels, labels. clone( ) ) ;
@@ -3046,7 +3051,7 @@ mod wasm_test {
3046
3051
let node = create_node ( storage) . await ;
3047
3052
3048
3053
let invoice = node
3049
- . create_invoice ( 10_000 , None , vec ! [ ] , None )
3054
+ . create_invoice ( Some ( 10_000 ) , None , vec ! [ ] , None )
3050
3055
. await
3051
3056
. unwrap ( )
3052
3057
. 0 ;
0 commit comments