wallet.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #####################################################################
  2. # #
  3. # THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE #
  4. # LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet ) #
  5. # FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk ) #
  6. # WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
  7. # IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
  8. # OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE. #
  9. # #
  10. # ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS #
  11. # (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK. #
  12. # YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
  13. # THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER #
  14. # VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG #
  15. # WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ). #
  16. # #
  17. # THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE #
  18. # NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
  19. # THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT #
  20. # SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
  21. # THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT. #
  22. # #
  23. # THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL #
  24. # FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE #
  25. # THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD #
  26. # TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT. #
  27. # #
  28. #####################################################################
  29. # This file will perform a simple search on the LBRY network.
  30. from subprocess import *
  31. import json
  32. from flbry import url
  33. from flbry.variables import *
  34. def history():
  35. # This function will output wallet history.
  36. # So instead we are going to request only the first 20 and let
  37. # the user load more.
  38. w, h = tsize()
  39. page_size = h - 6
  40. page = 1
  41. balance = check_output(["flbry/lbrynet",
  42. "wallet", "balance"])
  43. try:
  44. balance = json.loads(balance)
  45. except:
  46. print(" Connect to LBRY first.")
  47. return
  48. balance = balance["available"]
  49. while True:
  50. # Printing the search query and page number
  51. center("WALLET HISTORY. PAGE: "+str(page))
  52. center("AVAILABLE BALANCE: "+balance+" LBC")
  53. out = check_output(["flbry/lbrynet",
  54. "transaction", "list",
  55. '--page='+str(page),
  56. '--page_size='+str(page_size)])
  57. # Now we want to parse the json
  58. try:
  59. out = json.loads(out)
  60. except:
  61. print(" Connect to LBRY first.")
  62. return
  63. d = {"categories":["CONFORMATIONS", "AMOUNT", "IS TIP", "PUBLICATION"],
  64. "size":[1,1,1,3],
  65. "data":[]}
  66. try:
  67. # List what we found
  68. for n, i in enumerate(out["items"]):
  69. confirm = i["confirmations"]
  70. amount = i["value"]
  71. try:
  72. if i["support_info"][0]["is_tip"]:
  73. if i["support_info"][0]["is_spent"]:
  74. tip = "[v]"
  75. else:
  76. tip = "[ ]"
  77. publication = i["support_info"][0]["claim_name"]
  78. except:
  79. tip = " "
  80. publication = " "
  81. d["data"].append([confirm, amount, tip, publication])
  82. table(d)
  83. # Tell the user that he might want to load more
  84. center(" ---type 'more' to load more---")
  85. page = page +1
  86. # Error messages
  87. except Exception as e:
  88. if "code" in out:
  89. print(" Error code: ", out["code"] )
  90. if out["code"] == -32500:
  91. print(" SDK is still starting. Patience!")
  92. else:
  93. raise()
  94. print(" Error :", e)
  95. return
  96. # Making sure that we stop every time a new page is reached
  97. c = input(typing_dots())
  98. if c != "more":
  99. break
  100. try:
  101. c = int(c)
  102. except:
  103. return
  104. while True:
  105. try:
  106. url.get(out["items"][c]["support_info"][0]["claim_name"]+"#"+out["items"][c]["support_info"][0]["claim_id"])
  107. except:
  108. pass
  109. c = input(typing_dots())
  110. if not c:
  111. break
  112. try:
  113. c = int(c)
  114. except:
  115. return
  116. def balance():
  117. # Prints all wallet balance information
  118. balance = check_output(["flbry/lbrynet",
  119. "wallet", "balance"])
  120. try:
  121. balance = json.loads(balance)
  122. except:
  123. print(" Connect to LBRY first.")
  124. return
  125. # Get the variables
  126. total = balance["total"]
  127. available = balance["available"]
  128. reserved = balance["reserved"]
  129. claims = balance["reserved_subtotals"]["claims"]
  130. supports = balance["reserved_subtotals"]["supports"]
  131. tips = balance["reserved_subtotals"]["tips"]
  132. # Show the total, available, and reserved amounts in a table
  133. center("Balance Information")
  134. d = {"categories":["total", "available", "reserved"],
  135. "size":[1,1,1],
  136. "data":[]}
  137. d["data"].append([total, available, reserved])
  138. table(d, False)
  139. # Show the sources of the reserved balance in a table
  140. center("Reserved Balance Information")
  141. d = {"categories":["claims", "supports", "tips"],
  142. "size":[1,1,1],
  143. "data":[]}
  144. d["data"].append([claims, supports, tips])
  145. table(d, False)
  146. # Here because it looks out of place without it
  147. center("--- for wallet transaction history type 'wallet' ---")