From adb8e22d3bed3fdfcc5c289354ff448c1354b762 Mon Sep 17 00:00:00 2001 From: Martin Saposnic Date: Sat, 31 Jan 2026 09:40:02 -0300 Subject: [PATCH] add getBalance and getInvoice without starting node so it can be used on long running nodes --- index.d.ts | 35 +++++++++++++++++++++++++++---- src/lib.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 47a9086..980b66e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,7 +3,10 @@ /* auto-generated by NAPI-RS */ -export declare function setLogListener(callback?: (...args: any[]) => any | undefined | null, minLevel?: string | undefined | null): void +export declare function setLogListener( + callback?: (...args: any[]) => any | undefined | null, + minLevel?: string | undefined | null, +): void export declare function generateMnemonic(): string export interface MdkNodeOptions { network: string @@ -34,7 +37,7 @@ export interface PaymentEvent { export const enum PaymentEventType { Claimable = 0, Received = 1, - Failed = 2 + Failed = 2, } export interface NodeChannel { channelId: string @@ -68,6 +71,11 @@ export declare class MdkNode { stopReceiving(): void syncWallets(): void getBalance(): number + /** + * Get balance without starting/stopping the node. + * Use this when the node is already running via start_receiving(). + */ + getBalanceWhileRunning(): number listChannels(): Array /** * Manually sync the RGS snapshot. @@ -78,9 +86,28 @@ export declare class MdkNode { syncRgs(doFullSync: boolean): number receivePayment(minThresholdMs: number, quietThresholdMs: number): Array getInvoice(amount: number, description: string, expirySecs: number): PaymentMetadata - getInvoiceWithScid(humanReadableScid: string, amount: number, description: string, expirySecs: number): PaymentMetadata + /** + * Get invoice without starting/stopping the node. + * Use this when the node is already running via start_receiving(). + */ + getInvoiceWhileRunning(amount: number, description: string, expirySecs: number): PaymentMetadata + /** + * Get variable amount invoice without starting/stopping the node. + * Use this when the node is already running via start_receiving(). + */ + getVariableAmountJitInvoiceWhileRunning(description: string, expirySecs: number): PaymentMetadata + getInvoiceWithScid( + humanReadableScid: string, + amount: number, + description: string, + expirySecs: number, + ): PaymentMetadata getVariableAmountJitInvoice(description: string, expirySecs: number): PaymentMetadata - getVariableAmountJitInvoiceWithScid(humanReadableScid: string, description: string, expirySecs: number): PaymentMetadata + getVariableAmountJitInvoiceWithScid( + humanReadableScid: string, + description: string, + expirySecs: number, + ): PaymentMetadata payLnurl(lnurl: string, amountMsat: number, waitForPaymentSecs?: number | undefined | null): string payBolt11(bolt11Invoice: string): string payBolt12Offer(bolt12OfferString: string, amountMsat: number, waitForPaymentSecs?: number | undefined | null): string diff --git a/src/lib.rs b/src/lib.rs index 6fa52c4..feb147c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -499,6 +499,21 @@ impl MdkNode { u64_to_i64(total_outbound_msat / 1_000) } + /// Get balance without starting/stopping the node. + /// Use this when the node is already running via start_receiving(). + #[napi] + pub fn get_balance_while_running(&self) -> i64 { + let total_outbound_msat = self + .node + .list_channels() + .into_iter() + .fold(0u64, |acc, channel| { + acc.saturating_add(channel.outbound_capacity_msat) + }); + + u64_to_i64(total_outbound_msat / 1_000) + } + #[napi] pub fn list_channels(&self) -> Vec { self @@ -718,6 +733,51 @@ impl MdkNode { invoice_to_payment_metadata(invoice) } + /// Get invoice without starting/stopping the node. + /// Use this when the node is already running via start_receiving(). + #[napi] + pub fn get_invoice_while_running( + &self, + amount: i64, + description: String, + expiry_secs: i64, + ) -> napi::Result { + let bolt11_invoice_description = + Bolt11InvoiceDescription::Direct(Description::new(description).unwrap()); + + let invoice = self + .node + .bolt11_payment() + .receive_via_lsps4_jit_channel( + Some(amount as u64), + &bolt11_invoice_description, + expiry_secs as u32, + ) + .map_err(|e| napi::Error::from_reason(format!("Failed to get invoice: {e}")))?; + + Ok(invoice_to_payment_metadata(invoice)) + } + + /// Get variable amount invoice without starting/stopping the node. + /// Use this when the node is already running via start_receiving(). + #[napi] + pub fn get_variable_amount_jit_invoice_while_running( + &self, + description: String, + expiry_secs: i64, + ) -> napi::Result { + let bolt11_invoice_description = + Bolt11InvoiceDescription::Direct(Description::new(description).unwrap()); + + let invoice = self + .node + .bolt11_payment() + .receive_via_lsps4_jit_channel(None, &bolt11_invoice_description, expiry_secs as u32) + .map_err(|e| napi::Error::from_reason(format!("Failed to get invoice: {e}")))?; + + Ok(invoice_to_payment_metadata(invoice)) + } + #[napi] pub fn get_invoice_with_scid( &self,