@@ -57,6 +57,46 @@ def __eq__(self, other):
5757 return all (p1 == p2 for p1 , p2 in zip (self ._items , other ))
5858
5959
60+ class Payment :
61+
62+ def __init__ (self , epoch_time , amount_sats , comment ):
63+ self .epoch_time = epoch_time
64+ self .amount_sats = amount_sats
65+ self .comment = comment
66+
67+ def __str__ (self ):
68+ sattext = "sats"
69+ if self .amount_sats == 1 :
70+ sattext = "sat"
71+ return f"{ self .amount_sats } { sattext } : { self .comment } "
72+
73+ def __eq__ (self , other ):
74+ if not isinstance (other , Payment ):
75+ return False
76+ return self .epoch_time == other .epoch_time and self .amount_sats == other .amount_sats and self .comment == other .comment
77+
78+ def __lt__ (self , other ):
79+ if not isinstance (other , Payment ):
80+ return NotImplemented
81+ return (self .epoch_time , self .amount_sats , self .comment ) < (other .epoch_time , other .amount_sats , other .comment )
82+
83+ def __le__ (self , other ):
84+ if not isinstance (other , Payment ):
85+ return NotImplemented
86+ return (self .epoch_time , self .amount_sats , self .comment ) <= (other .epoch_time , other .amount_sats , other .comment )
87+
88+ def __gt__ (self , other ):
89+ if not isinstance (other , Payment ):
90+ return NotImplemented
91+ return (self .epoch_time , self .amount_sats , self .comment ) > (other .epoch_time , other .amount_sats , other .comment )
92+
93+ def __ge__ (self , other ):
94+ if not isinstance (other , Payment ):
95+ return NotImplemented
96+ return (self .epoch_time , self .amount_sats , self .comment ) >= (other .epoch_time , other .amount_sats , other .comment )
97+
98+
99+
60100
61101class Wallet :
62102
@@ -84,6 +124,11 @@ def handle_new_balance(self, new_balance, fetchPaymentsIfChanged=True):
84124 print ("Refreshing payments..." )
85125 self .fetch_payments () # if the balance changed, then re-list transactions
86126
127+ def handle_new_payment (self , new_payment ):
128+ print ("handle_new_payment" )
129+ self .payment_list .add (new_payment )
130+ self .payments_updated_cb ()
131+
87132 def handle_new_payments (self , new_payments ):
88133 print ("handle_new_payments" )
89134 if self .payment_list != new_payments :
@@ -142,11 +187,9 @@ def on_message(self, class_obj, message: str):
142187 if new_balance :
143188 self .handle_new_balance (new_balance , False ) # handle new balance BUT don't trigger a full fetch_payments
144189 transaction = payment_notification .get ("payment" )
145- new_payments = UniqueSortedList ()
146190 print (f"Got transaction: { transaction } " )
147191 paymentObj = parseLNBitsPayment (transaction )
148- new_payments .add (paymentObj )
149- self .handle_new_payments (new_payments )
192+ self .handle_new_payment (paymentObj )
150193 except Exception as e :
151194 print (f"websocket on_message got exception: { e } " )
152195
@@ -222,12 +265,10 @@ def fetch_payments(self):
222265 try :
223266 payments_reply = json .loads (response_text )
224267 #print(f"Got payments: {payments_reply}")
225- new_payments = UniqueSortedList ()
226268 for transaction in payments_reply :
227269 #print(f"Got transaction: {transaction}")
228270 paymentObj = parseLNBitsPayment (transaction )
229- new_payments .add (paymentObj )
230- self .handle_new_payments (new_payments )
271+ self .handle_new_payment (paymentObj )
231272 except Exception as e :
232273 print (f"Could not parse reponse text '{ response_text } ' as JSON: { e } " )
233274 raise e
@@ -310,22 +351,21 @@ def wallet_manager_thread(self):
310351 result = response .get ("result" )
311352 if result :
312353 if result .get ("balance" ):
313- new_balance = round (int (response [ " result" ] ["balance" ]) / 1000 )
354+ new_balance = round (int (result ["balance" ]) / 1000 )
314355 print (f"Got balance: { new_balance } " )
315356 self .handle_new_balance (new_balance )
316357 elif result .get ("transactions" ):
317358 print ("Response contains transactions!" )
318- new_payments = UniqueSortedList ()
319359 for transaction in result ["transactions" ]:
320360 amount = transaction ["amount" ]
321361 amount = round (amount / 1000 )
322362 comment = self .getCommentFromTransaction (transaction )
323363 epoch_time = transaction ["created_at" ]
324- payment = Payment (epoch_time , amount , comment )
325- new_payments . add ( payment )
326- self . handle_new_payments ( new_payments )
327- elif result .get ("notification" ): # it's a notification
328- notification = result . get ( "notification" )
364+ paymentObj = Payment (epoch_time , amount , comment )
365+ self . handle_new_payment ( paymentObj )
366+ else :
367+ notification = response .get ("notification" )
368+ if notification :
329369 amount = notification ["amount" ]
330370 amount = round (amount / 1000 )
331371 type = notification ["type" ]
@@ -334,17 +374,14 @@ def wallet_manager_thread(self):
334374 elif type != "incoming" :
335375 print (f"WARNING: invalid notification type { type } , ignoring." )
336376 continue
377+ new_balance = self .balance + amount
337378 self .handle_new_balance (new_balance , False )
338379 epoch_time = transaction ["created_at" ]
339380 comment = self .getCommentFromTransaction (notification )
340- payment = Payment (epoch_time , amount , comment )
341- new_payments = UniqueSortedList ()
342- new_payments .add (payment )
343- self .handle_new_payments (new_payments )
381+ paymentObj = Payment (epoch_time , amount , comment )
382+ self .handle_new_payment (paymentObj )
344383 else :
345384 print ("Unsupported response, ignoring." )
346- else :
347- print ("Event doesn't contain result, ignoring." )
348385 except Exception as e :
349386 print (f"DEBUG: Error processing response: { e } " )
350387 time .sleep (1 )
@@ -440,42 +477,3 @@ def parse_nwc_url(self, nwc_url):
440477 except Exception as e :
441478 print (f"DEBUG: Error parsing NWC URL: { e } " )
442479
443-
444- class Payment :
445-
446- def __init__ (self , epoch_time , amount_sats , comment ):
447- self .epoch_time = epoch_time
448- self .amount_sats = amount_sats
449- self .comment = comment
450-
451- def __str__ (self ):
452- sattext = "sats"
453- if self .amount_sats == 1 :
454- sattext = "sat"
455- return f"{ self .amount_sats } { sattext } : { self .comment } "
456-
457- def __eq__ (self , other ):
458- if not isinstance (other , Payment ):
459- return False
460- return self .epoch_time == other .epoch_time and self .amount_sats == other .amount_sats and self .comment == other .comment
461-
462- def __lt__ (self , other ):
463- if not isinstance (other , Payment ):
464- return NotImplemented
465- return (self .epoch_time , self .amount_sats , self .comment ) < (other .epoch_time , other .amount_sats , other .comment )
466-
467- def __le__ (self , other ):
468- if not isinstance (other , Payment ):
469- return NotImplemented
470- return (self .epoch_time , self .amount_sats , self .comment ) <= (other .epoch_time , other .amount_sats , other .comment )
471-
472- def __gt__ (self , other ):
473- if not isinstance (other , Payment ):
474- return NotImplemented
475- return (self .epoch_time , self .amount_sats , self .comment ) > (other .epoch_time , other .amount_sats , other .comment )
476-
477- def __ge__ (self , other ):
478- if not isinstance (other , Payment ):
479- return NotImplemented
480- return (self .epoch_time , self .amount_sats , self .comment ) >= (other .epoch_time , other .amount_sats , other .comment )
481-
0 commit comments