Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import grpc
  4. import bchrpc_pb2 as pb
  5. import bchrpc_pb2_grpc as bchrpc
  6.  
  7. def run():
  8. with grpc.secure_channel('bchd.greyh.at:8335', grpc.ssl_channel_credentials()) as channel:
  9. stub = bchrpc.bchrpcStub(channel)
  10.  
  11. ## Fetch tx history for bch address
  12. address = "qrj279sdafw2r9zkcw7uncdhks2s3lpzmvwlle5243"
  13.  
  14. req = pb.GetAddressTransactionsRequest()
  15. req.address = address
  16.  
  17. resp = stub.GetAddressTransactions(req)
  18.  
  19. print("Fetch tx history for address: " + address + "\n")
  20. # print(resp)
  21. for tx in resp.confirmed_transactions:
  22. incoming = 0
  23. outgoing = 0
  24. for txInput in tx.inputs:
  25. if (txInput.address == address):
  26. outgoing += txInput.value
  27.  
  28. for txOutput in tx.outputs:
  29. if (txOutput.address == address):
  30. incoming += txOutput.value
  31.  
  32. print("%s => INCOMING: %14d sats - OUTGOING: %14d sats" % (bytearray(tx.hash[::-1]).hex(), incoming, outgoing))
  33.  
  34. print("\nUNCONFIRMED")
  35. for tx in resp.unconfirmed_transactions:
  36. incoming = 0
  37. outgoing = 0
  38. for txInput in tx.transaction.inputs:
  39. if (txInput.address == address):
  40. outgoing += txInput.value
  41.  
  42. for txOutput in tx.transaction.outputs:
  43. if (txOutput.address == address):
  44. incoming += txOutput.value
  45.  
  46. print("%s => INCOMING: %14d sats - OUTGOING: %14d sats" % (bytearray(tx.transaction.hash[::-1]).hex(), incoming, outgoing))
  47.  
  48.  
  49. ## Monitor address for incoming txs
  50. ## Ideally this should run in a separate thread since this is a blocking call
  51. txFilter = pb.TransactionFilter()
  52. txFilter.addresses.append(address)
  53. # txFilter.all_transactions = True
  54.  
  55. req = pb.SubscribeTransactionsRequest()
  56. req.include_in_block = True
  57. req.include_mempool = True
  58. req.subscribe.CopyFrom(txFilter)
  59.  
  60. print("\nMonitor txs for address: " + address + "\n")
  61. for notification in stub.SubscribeTransactions(req):
  62. tx = notification.unconfirmed_transaction.transaction
  63.  
  64. incoming = 0
  65. outgoing = 0
  66. for txInput in tx.inputs:
  67. if (txInput.address == address):
  68. outgoing += txInput.value
  69.  
  70. for txOutput in tx.outputs:
  71. if (txOutput.address == address):
  72. incoming += txOutput.value
  73.  
  74. print("%s => INCOMING: %14d sats - OUTGOING: %14d sats" % (bytearray(tx.hash[::-1]).hex(), incoming, outgoing))
  75.  
  76.  
  77. if __name__ == '__main__':
  78. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement