Hack A Lapp: Introduction to Bitcoins Lightning Network App Development/C-Lightning with python/Check which payments have been received
Check which payments have been received
- work with the listinvoices() method
- understand that invoices have a status
- access data from a specific invoice
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.