alexbosworth / ln-service

Node.js interface to LND
MIT License
319 stars 60 forks source link

How can I monitor incoming payments? #160

Open basharov opened 2 years ago

basharov commented 2 years ago

I can subscribe to outcoming payments with this:

const subPayments = subscribeToPastPayments({ lnd })
subPayments.on('payment', async (payment: any) => {})

When I get this event, I fetch the balance via calling getChainBalance() which returns me the accurate updated balance.

However, when I create an invoice and pay it from another wallet, I only see this option to track the event of the incoming payment:

const subInvoices = subscribeToInvoices({ lnd })
subInvoices.on('invoice_updated', async (invoice: any) => {
  console.log(invoice)
  if (invoice.is_confirmed) {
    // check balance here
  }
})

But, it won't return the accurate updated balance sometimes. It returns some balance but then it changes and this is not accurate.

So, I hope there is some other subscription I can use to see the fact of the balance change.

Is there any?

Thanks!

alexbosworth commented 2 years ago

You could try subscribeToForwards

balance changing overall does not have an API

basharov commented 2 years ago

You could try subscribeToForwards

balance changing overall does not have an API

First, thank you Alex for the lightning-fast response! Second, yes, seems like it's what I need, shows the correct final balance, thank you!

basharov commented 2 years ago

Actually seems like this one returns the balance in similar way as invoice subscription, with the value which then changes without any way to detect it. Probably I can just run a background service which would query via rpc periodically, like every 5 seconds?

alexbosworth commented 2 years ago

Yes since there is no balance changing API you can do polling