Estimating Lightning Fees

Before sending or receiving payments, you can estimate the associated fees:

// Estimate fees for sending a payment
const sendFees = await sparkWallet.getLightningPaymentFees(
  "SEND",
  invoice // BOLT11 encoded invoice
);
console.log("Estimated send fees:", sendFees.toString(), "sats");

// Estimate fees for receiving a payment
const receiveFees = await sparkWallet.getLightningPaymentFees(
  "RECEIVE",
  invoice // BOLT11 encoded invoice
);
console.log("Estimated receive fees:", receiveFees.toString(), "sats");

// For zero-amount invoices, specify the amount.
// Zero-amount invoices are not currently supported on Spark.
const zeroInvoiceFees = await sparkWallet.get_lightning_payment_fees(
  "SEND",
  invoice,
  BigInt(1000) // Amount in satoshis
);

Sending a Lightning Payment

Follow these steps to send a Lightning invoice:

  1. Pay a Lightning Invoice

    const invoice = "lnbc1..."; // Encoded Lightning invoice
    
    // For zero-amount invoices, specify the amount
    const paymentRequest = await sparkWallet.payLightningInvoice(invoice, BigInt(1000));
    // Or for regular invoices
    const paymentRequest = await sparkWallet.payLightningInvoice(invoice);
    
  2. Confirm the Payment

    Once the payment is outbound, you can confirm the payment by checking it’s status:

    const status = await sparkWallet.getLightningPaymentRequest(paymentRequest.request_id);
    console.log("Payment Status:", status.status);
    console.log("Fees paid:", status.fees_sats.toString(), "sats");
    

Receiving a Lightning Payment

To receive payments, generate and share your Lightning invoice with the sender.

  1. Generate an Invoice

    const amount = BigInt(50000); // Amount in satoshis
    const expirySeconds = 3600; // Optional: Set invoice expiry (defaults to 24 hours)
    
    const { encoded_invoice, fees_on_receive_sats } = await sparkWallet.createLightningInvoice(
      amount,
      expirySeconds
    );
    
    console.log("Your Invoice:", encoded_invoice);
    console.log("Fees on receive:", fees_on_receive_sats.toString(), "sats");
    
  2. Wait for Payment

    Monitor your wallet to confirm receipt of the payment.

    await sparkWallet.syncWallet();
    const btcBalance = await sparkWallet.getBtcBalance();
    console.log("BTC Balance:", btcBalance);
    

Best Practices

  • Invoice Expiry: Set appropriate expiry times for invoices using the expirySeconds parameter (defaults to 24 hours).
  • Monitor Transactions: Regularly sync your wallet to stay updated on incoming and outgoing payments.
  • Zero Amount Invoices: When paying zero amount invoices, always specify the amount_sats parameter.
  • Payment Status: Always check the payment status using getLightningPaymentRequest to confirm successful completion.
  • Fee Estimation: Always estimate fees before sending or receiving payments to avoid surprises.

Next Steps

Proceed to Send & Receive on Spark for direct transactions within Spark.