wallet.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 import settings
  34. from flbry import markdown
  35. from flbry import channel
  36. from flbry.variables import *
  37. def history():
  38. # This function will output wallet history.
  39. # So instead we are going to request only the first 20 and let
  40. # the user load more.
  41. w, h = tsize()
  42. page_size = h - 6
  43. page = 1
  44. balance = check_output(["flbry/lbrynet",
  45. "wallet", "balance"])
  46. try:
  47. balance = json.loads(balance)
  48. except:
  49. print(" Connect to LBRY first.")
  50. return
  51. balance = balance["available"]
  52. while True:
  53. # Printing the search query and page number
  54. center("WALLET HISTORY. PAGE: "+str(page))
  55. center("AVAILABLE BALANCE: "+balance+" LBC")
  56. out = check_output(["flbry/lbrynet",
  57. "transaction", "list",
  58. '--page='+str(page),
  59. '--page_size='+str(page_size)])
  60. # Now we want to parse the json
  61. try:
  62. out = json.loads(out)
  63. except:
  64. print(" Connect to LBRY first.")
  65. return
  66. d = {"categories":["CONFORMATIONS", "AMOUNT", "IS TIP", "PUBLICATION"],
  67. "size":[1,1,1,3],
  68. "data":[]}
  69. try:
  70. # List what we found
  71. for n, i in enumerate(out["items"]):
  72. confirm = i["confirmations"]
  73. amount = i["value"]
  74. tip = " "
  75. publication = " "
  76. try:
  77. if i["support_info"][0]["is_tip"]:
  78. if i["support_info"][0]["is_spent"]:
  79. tip = "[v]"
  80. else:
  81. tip = "[ ]"
  82. publication = i["support_info"][0]["claim_name"]
  83. except:
  84. tip = " "
  85. publication = " "
  86. d["data"].append([confirm, amount, tip, publication])
  87. table(d)
  88. # Tell the user that he might want to load more
  89. center(" ---type 'more' to load more---")
  90. page = page +1
  91. # Error messages
  92. except Exception as e:
  93. if "code" in out:
  94. print(" Error code: ", out["code"] )
  95. if out["code"] == -32500:
  96. print(" SDK is still starting. Patience!")
  97. else:
  98. raise()
  99. print(" Error :", e)
  100. return
  101. # Making sure that we stop every time a new page is reached
  102. c = input(typing_dots())
  103. if c != "more":
  104. break
  105. try:
  106. c = int(c)
  107. except:
  108. return
  109. while True:
  110. try:
  111. url.get(out["items"][c]["support_info"][0]["claim_name"]+"#"+out["items"][c]["support_info"][0]["claim_id"])
  112. except:
  113. pass
  114. c = input(typing_dots())
  115. if not c:
  116. break
  117. try:
  118. c = int(c)
  119. except:
  120. return
  121. def balance():
  122. # Prints all wallet balance information
  123. balance = check_output(["flbry/lbrynet",
  124. "wallet", "balance"])
  125. try:
  126. balance = json.loads(balance)
  127. except:
  128. print(" Connect to LBRY first.")
  129. return
  130. # Get the variables
  131. total = balance["total"]
  132. available = balance["available"]
  133. reserved = balance["reserved"]
  134. claims = balance["reserved_subtotals"]["claims"]
  135. supports = balance["reserved_subtotals"]["supports"]
  136. tips = balance["reserved_subtotals"]["tips"]
  137. # Show the total, available, and reserved amounts in a table
  138. center("Balance Information")
  139. d = {"categories":["total", "available", "reserved"],
  140. "size":[1,1,1],
  141. "data":[]}
  142. d["data"].append([total, available, reserved])
  143. table(d, False)
  144. # Show the sources of the reserved balance in a table
  145. center("Reserved Balance Information")
  146. d = {"categories":["claims", "supports", "tips"],
  147. "size":[1,1,1],
  148. "data":[]}
  149. d["data"].append([claims, supports, tips])
  150. table(d, False)
  151. # Here because it looks out of place without it
  152. center("--- for wallet transaction history type 'wallet' ---")
  153. def support(claim_id, tip=False):
  154. # Dialog to send support or tip to claim_id
  155. d = {"categories": ["Amount", "Channel", "Comment"],
  156. "size": [1,3,5],
  157. "data": []}
  158. amount = float(settings.get("default_tip"))
  159. channel_name = "[anonymous]"
  160. channel_id = ""
  161. comment = None
  162. complete([
  163. "amount",
  164. "channel",
  165. "send",
  166. "comment"
  167. ])
  168. while True:
  169. # Just setting data directly wasn't working, so we clear it then append to it
  170. d["data"] = []
  171. # f'amount:.5f}' shows the amount in decimal notation with 5 places after the decimal
  172. d["data"].append([f'{amount:.5f}', channel_name, str(comment)])
  173. table(d, False)
  174. center("---type 'help' for support commands---")
  175. c = input(typing_dots())
  176. if not c:
  177. break
  178. if c == "amount":
  179. amount = float(input(" Amount: "))
  180. elif c == "channel":
  181. channel_name, channel_id = channel.select("Select the signing channel.", claim_id=True)
  182. elif c == "help":
  183. markdown.draw("help/support.md", "Support Help")
  184. elif c.startswith("comment"):
  185. c = c + ' '
  186. a = c[c.find(" "):]
  187. if len(a) > 1:
  188. comment = file_or_editor(a, "Type the comment here. Don't forget to save. Then return to FastLBRY.")
  189. else:
  190. comment = input(" Comment: ")
  191. elif c == "send":
  192. args = [
  193. "flbry/lbrynet",
  194. "support", "create",
  195. "--claim_id="+claim_id,
  196. "--amount="+f'{amount:.5f}'
  197. ]
  198. if channel_id:
  199. args.append("--channel_id="+channel_id)
  200. if comment:
  201. args.append("--comment="+comment)
  202. if tip:
  203. args.append("--tip")
  204. try:
  205. x = check_output(args)
  206. center("Supported with "+f'{amount:.5f}'+" LBC", "bdgr")
  207. except Exception as e:
  208. center("Error sending support: "+str(e), "bdrd")
  209. break
  210. def addresses():
  211. w, h = tsize()
  212. page_size = h - 5
  213. # TODO: At the time of writing, pagination in this command is not working, so we only get the first page.
  214. # When this gets fixed, please add pagination here.
  215. out = check_output(["flbry/lbrynet",
  216. "address", "list",
  217. "--page_size="+str(page_size)])
  218. out = json.loads(out)
  219. try:
  220. data_print = {"categories":["Address"],
  221. "size":[1],
  222. "data":[]}
  223. # List what we found
  224. for n, i in enumerate(out["items"]):
  225. data_print["data"].append([i["address"]])
  226. table(data_print)
  227. center("")
  228. # Error messages
  229. except Exception as e:
  230. if "code" in out:
  231. print(" Error code: ", out["code"] )
  232. if out["code"] == -32500:
  233. print(" SDK is still starting. Patience!")
  234. else:
  235. print(" Error :", e)
  236. return
  237. def address_send(amount="", address=""):
  238. if not amount:
  239. amount = input(" Amount to send: ")
  240. if not address:
  241. address = input(" Address to send to: ")
  242. out = check_output(["flbry/lbrynet",
  243. "wallet",
  244. "send",
  245. str(amount),
  246. address])
  247. out = json.loads(out)
  248. if "message" in out:
  249. center(" Error sending to address: "+out["message"], "bdrd")
  250. else:
  251. center(" Successfully sent "+str(amount)+" to "+address, "bdgr")