wallet.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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, amount="", 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. if tip:
  169. support_strs = ["Tipped", "tip"]
  170. else:
  171. support_strs = ["Boosted", "boost"]
  172. while True:
  173. # Just setting data directly wasn't working, so we clear it then append to it
  174. d["data"] = []
  175. # f'amount:.5f}' shows the amount in decimal notation with 5 places after the decimal
  176. d["data"].append([f'{amount:.5f}', channel_name, str(comment)])
  177. table(d, False)
  178. center("---type 'help' for support commands---")
  179. c = input(typing_dots())
  180. if not c:
  181. break
  182. if c == "amount":
  183. amount = float(input(" Amount: "))
  184. elif c == "channel":
  185. channel_name, channel_id = channel.select("Select the signing channel.", claim_id=True)
  186. elif c == "help":
  187. markdown.draw("help/support.md", "Support Help")
  188. elif c.startswith("comment"):
  189. c = c + ' '
  190. a = c[c.find(" "):]
  191. if len(a) > 1:
  192. comment = file_or_editor(a, "Type the comment here. Don't forget to save. Then return to FastLBRY.")
  193. else:
  194. comment = input(" Comment: ")
  195. elif c == "send":
  196. args = [
  197. "flbry/lbrynet",
  198. "support", "create",
  199. "--claim_id="+claim_id,
  200. "--amount="+f'{amount:.5f}'
  201. ]
  202. if channel_id:
  203. args.append("--channel_id="+channel_id)
  204. if comment:
  205. args.append("--comment="+comment)
  206. if tip:
  207. args.append("--tip")
  208. try:
  209. x = check_output(args)
  210. center(support_strs[0]+" with "+f'{amount:.5f}'+" LBC", "bdgr")
  211. except Exception as e:
  212. center("Error sending "+support_strs[1]+": "+str(e), "bdrd")
  213. break
  214. def addresses():
  215. w, h = tsize()
  216. page_size = h - 5
  217. # TODO: At the time of writing, pagination in this command is not working, so we only get the first page.
  218. # When this gets fixed, please add pagination here.
  219. out = check_output(["flbry/lbrynet",
  220. "address", "list",
  221. "--page_size="+str(page_size)])
  222. out = json.loads(out)
  223. try:
  224. data_print = {"categories":["Address"],
  225. "size":[1],
  226. "data":[]}
  227. # List what we found
  228. for n, i in enumerate(out["items"]):
  229. data_print["data"].append([i["address"]])
  230. table(data_print)
  231. center("")
  232. # Error messages
  233. except Exception as e:
  234. if "code" in out:
  235. print(" Error code: ", out["code"] )
  236. if out["code"] == -32500:
  237. print(" SDK is still starting. Patience!")
  238. else:
  239. print(" Error :", e)
  240. return
  241. def address_send(amount="", address=""):
  242. if not amount:
  243. amount = input(" Amount to send: ")
  244. if not address:
  245. address = input(" Address to send to: ")
  246. out = check_output(["flbry/lbrynet",
  247. "wallet",
  248. "send",
  249. str(amount),
  250. address])
  251. out = json.loads(out)
  252. if "message" in out:
  253. center(" Error sending to address: "+out["message"], "bdrd")
  254. else:
  255. center(" Successfully sent "+str(amount)+" to "+address, "bdgr")