Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -34,7 +37,7 @@ export interface PaymentEvent {
export const enum PaymentEventType {
Claimable = 0,
Received = 1,
Failed = 2
Failed = 2,
}
export interface NodeChannel {
channelId: string
Expand Down Expand Up @@ -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<NodeChannel>
/**
* Manually sync the RGS snapshot.
Expand All @@ -78,9 +86,28 @@ export declare class MdkNode {
syncRgs(doFullSync: boolean): number
receivePayment(minThresholdMs: number, quietThresholdMs: number): Array<ReceivedPayment>
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
Expand Down
60 changes: 60 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NodeChannel> {
self
Expand Down Expand Up @@ -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<PaymentMetadata> {
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<PaymentMetadata> {
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,
Expand Down