async def r_invoice_gen(user: User, *_): # create new new pub sub client for streaming locally paid invoices local_stream = PUBSUB.add_client(user.username) # create stream for remotely paid invoices remote_stream = await LND.stub.SubscribeInvoices(ln.InvoiceSubscription()) global_stream = stream.merge(local_stream, remote_stream) async with global_stream.stream() as streamer: async for response in streamer: try: # check if response if from lnd # external payment or pubsub - local payment if isinstance(response, Invoice): # invoice model received from pubsub client # yield and default resolver will retrieve requested fields yield response else: # payment comes from lnd, # check if its associated with this user invoice = None if response.state == 1: invoice = await Invoice.get(response.r_hash) if invoice and invoice.payee == user.username: # received a paid invoice with this user as payee updated = await invoice.update( paid=True, paid_at=invoice.settle_date ).apply() yield updated except GeneratorExit: # user closed stream, del pubsub queue del local_stream if len(PUBSUB[user.username]) == 0: del PUBSUB[user.username]
def subscribe_invoices(self): try: request = ln.InvoiceSubscription() return self.stub.SubscribeInvoices(request) except grpc.RpcError as e: logging.error(e) return e.details()
def subscribe_invoices(self): try: request = ln.InvoiceSubscription() invoices = self.stub.SubscribeInvoices(request) for invoice in invoices: self.broadcast.updateClients(MessageToJson(invoice)) except grpc.RpcError as e: logging.error(e) return e.details()
def invoice_subscription(self, add_index): try: request = ln.InvoiceSubscription(add_index=add_index, # settle_index= 3, ) for response in self.client.SubscribeInvoices(request): print(response) logger.warning(response) except Exception as e: logger.exception(e)
def watch_and_update_tip_invoice(app, tip, invoice): expiration = time() + EXPIRY_SECONDS stub = get_stub(tip.recipient.node_url, tip.recipient.macaroon, tip.recipient.cert) request = ln.InvoiceSubscription(add_index=invoice.add_index) for inv in stub.SubscribeInvoices(request): # If the invoice we're watching has expired anyway, break out if time() > expiration: break # If it's our invoice that's been paid, mark it as such and break out if inv.r_hash.hex() == invoice.r_hash.hex() and hasattr(inv, 'amt_paid_sat') and inv.amt_paid_sat: with app.app_context(): local_tip = db.session.merge(tip) local_tip.confirm(inv.amt_paid_sat) db.session.commit() break
def payments_listener_thread(): logger.debug("payments_listener thread started") for invoice in stub.SubscribeInvoices(ln.InvoiceSubscription()): if hasattr(invoice, 'settled') and invoice.settled == True: handle_payment(invoice) logger.critical("paymens_listener thread ended")
def subinvs(): res = stub.SubscribeInvoices(ln.InvoiceSubscription()) for r in res: print r
import rpc_pb2 as ln import rpc_pb2 as ln, rpc_pb2_grpc as lnrpc import grpc, os import codecs import time from paramiko.client import SSHClient os.environ['GRPC_SSL_CIPHER_SUITES'] = 'HIGH+ECDSA' cert = open(os.path.expanduser('~/.lnd/tls.cert'), 'rb').read() ssl_creds = grpc.ssl_channel_credentials(cert) channel = grpc.secure_channel('localhost:10009', ssl_creds) stub = lnrpc.LightningStub(channel) request = ln.InvoiceSubscription() for response in stub.SubscribeInvoices(request): if response.settled: #do something print('starting ssh call') client = SSHClient() client.load_system_host_keys() client.connect('192.168.0.104', username='******', password='******') stdin, stdout, stderr = client.exec_command( 'source ~/.ludwigsbashrc && rostopic pub /roboy/control/matrix/leds/mode/simple std_msgs/Int32 \"data: 2\" --once' ) time.sleep(10) stdin, stdout, stderr = client.exec_command( 'source ~/.ludwigsbashrc && rostopic pub /roboy/control/matrix/leds/mode/simple std_msgs/Int32 \"data: 0\" --once' )
def subscribeInvoices(self, addIndex=0, settleIndex=0): request = ln.InvoiceSubscription(add_index=addIndex, settle_index=settleIndex) return self.stub.SubscribeInvoices(request)
channel = grpc.secure_channel('localhost:10009', ssl_creds) stub = lnrpc.LightningStub(channel) request = ln.ListChannelsRequest() response = stub.ListChannels(request, metadata=[('macaroon', macaroon)]) for chan in response.channels: request = ln.NodeInfoRequest(pub_key=chan.remote_pubkey) r = stub.GetNodeInfo(request, metadata=[('macaroon', macaroon)]) chan_partner_alias = r.node.alias print("%s\t%s\t%f" % (chan_partner_alias, chan.remote_pubkey, (chan.local_balance / (chan.local_balance + chan.remote_balance)))) request = ln.InvoiceSubscription( add_index=0, settle_index=0, ) # Query routes for one direction request = ln.QueryRoutesRequest( pub_key= "027ce055380348d7812d2ae7745701c9f93e70c1adeb2657f053f91df4f2843c71", amt=300000, num_routes=5, ) response = stub.QueryRoutes(request, metadata=[('macaroon', macaroon)]) print(response) topartner = response.routes request = ln.GetInfoRequest() response = stub.GetInfo(request, metadata=[('macaroon', macaroon)]) print(response)