Jump to content

Hack A Lapp/Introduction to Bitcoins Lightning Network App Development/C-Lightning with python/Check which payments have been received

From Wikiversity

Check which payments have been received

Learning goals

  1. work with the listinvoices() method
  2. understand that invoices have a status
  3. access data from a specific invoice

Video

Script

let us create the rankings: We start by retrieving a list of all invoices that we have created with our lightning node. This can be done again with an RPC call on the python library.The result is a json dictionary containing a list of invoices. We access the list by using the "invoice" key. we create a dictionary in which we use to count the votes. Let us call it count_dict now we use a for loop to iterate over all invoices that we have previously retrieved we store the status of the which can accept values like paid, unpaid, or expired and we store the payment hash of the invoice. now we check if the statues took another value than paid. Because if in invoice was not paid we don't want to process it and continue with the next one. however if it was paid we should count the votes. remember that the voting data was stored in the folder potential votes with the payment hash appended by .json as the file name. so we open that file and load it as a json dictionary

the votes where stored as a list with the key "v" so we access them and now we just do standard python stuff for counting with the help of our count_dict. if we have previously seen the vote we increase its counter by one and if we see it for the first time we set the counter for the vote to 1 This code has one problem. We might have a payment hash in our lightning node that came from another service so there is no file in our folder with this payment hash. We can quickfix this my surrounding this code block with a try / except statement An even bigger problem is that this Application design is really only for demonstration purposes. Imagine 1000 people have voted. It will be bad idea to check that many files. So for a real application you should use a data base or at least a better data structure. Anyway lets do the typical pyhton magic to sort the dictionary in a reversed list of counts in order to have the most popular items at the beginning. We return the list of tuples and have them displayed to the user who has paid in another part of the lapp.


Quiz

1 the call to rpc_interfce.listinvoices()["invoices"] returns a list on invoices what are the values the status of each invoice can take?

paid
Paid
Unpaid
expired
pending

2 What happens if you call function rpc_interfce.listinvoices("some-string")["invoices"]?

only the invoice with the description some string will be returned
only the invoice with the label some string will be returned
the listinvoices function does not accept any arguments!

3 you have done this statement invoice = rpc_interfce.listinvoices("some-string")["invoices"] and some value was returned. How can you access the amount of the invoice?

by using the rpc_interfce.decodepay(invoice) call
by invoice.amount()
by invoice["amount"]
the amount can be extracted with some regular expression from the human readable part of the bolt11 string which can be accessed with invoice["bolt11"]]



Discussion