wallet.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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([lbrynet_binary["b"],
  45. "wallet", "balance"])
  46. try:
  47. balance = json.loads(balance)
  48. except:
  49. center("Connect to LBRY first.", "bdrd")
  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([lbrynet_binary["b"],
  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. center("Connect to LBRY first.", "bdrd")
  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 they 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. center("Error code: "+out["code"], "bdrd")
  95. if "message" in out:
  96. center("Error: "+out["message"], "bdrd")
  97. else:
  98. center("Error: "+e, "bdrd")
  99. return
  100. while True:
  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. try:
  110. url.get(out["items"][c]["support_info"][0]["claim_name"]+"#"+out["items"][c]["support_info"][0]["claim_id"])
  111. except:
  112. pass
  113. # Print the list again
  114. table(d)
  115. center("---type 'more' to load more---")
  116. def balance():
  117. # Prints all wallet balance information
  118. balance = check_output([lbrynet_binary["b"],
  119. "wallet", "balance"])
  120. try:
  121. balance = json.loads(balance)
  122. except:
  123. center("Connect to LBRY first.", "bdrd")
  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' ---")
  148. def support(claim_id, amount="", tip=False):
  149. # Dialog to send support or tip to claim_id
  150. d = {"categories": ["Amount", "Channel", "Comment"],
  151. "size": [1,3,5],
  152. "data": []}
  153. amount = float(settings.get("default_tip"))
  154. channel_name = "[anonymous]"
  155. channel_id = ""
  156. comment = None
  157. complete([
  158. "amount",
  159. "channel",
  160. "send",
  161. "comment"
  162. ])
  163. if tip:
  164. support_strs = ["Tipped", "tip"]
  165. else:
  166. support_strs = ["Boosted", "boost"]
  167. while True:
  168. # Just setting data directly wasn't working, so we clear it then append to it
  169. d["data"] = []
  170. # f'amount:.5f}' shows the amount in decimal notation with 5 places after the decimal
  171. d["data"].append([f'{amount:.5f}', channel_name, str(comment)])
  172. table(d, False)
  173. center("---type 'help' for support commands---")
  174. c = input(typing_dots())
  175. if not c:
  176. break
  177. if c == "amount":
  178. am = ""
  179. # while am is not a float
  180. while type(am) != type(5.0):
  181. am = input(typing_dots("Amount"))
  182. # If the user types nothing just keep the amount the same
  183. if not am:
  184. break
  185. try:
  186. amount = float(am)
  187. am = amount
  188. except:
  189. pass
  190. elif c == "channel":
  191. channel_name, channel_id = channel.select("Select the signing channel.", claim_id=True)
  192. elif c == "help":
  193. markdown.draw("help/support.md", "Support Help")
  194. elif c.startswith("comment"):
  195. c = c + ' '
  196. a = c[c.find(" "):]
  197. if len(a) > 1:
  198. comment = file_or_editor(a, "Type the comment here. Don't forget to save. Then return to FastLBRY.")
  199. else:
  200. comment = input(typing_dots("Comment", give_space=True))
  201. elif c == "send":
  202. args = [
  203. lbrynet_binary["b"],
  204. "support", "create",
  205. "--claim_id="+claim_id,
  206. "--amount="+f'{amount:.5f}'
  207. ]
  208. if channel_id:
  209. args.append("--channel_id="+channel_id)
  210. if comment:
  211. args.append("--comment="+comment)
  212. if tip:
  213. args.append("--tip")
  214. try:
  215. x = check_output(args)
  216. center(support_strs[0]+" with "+f'{amount:.5f}'+" LBC", "bdgr")
  217. except Exception as e:
  218. center("Error sending "+support_strs[1]+": "+str(e), "bdrd")
  219. break
  220. def addresses():
  221. w, h = tsize()
  222. page_size = h - 5
  223. # TODO: At the time of writing, pagination in this command is not working, so we only get the first page.
  224. # When this gets fixed, please add pagination here.
  225. out = check_output([lbrynet_binary["b"],
  226. "address", "list",
  227. "--page_size="+str(page_size)])
  228. out = json.loads(out)
  229. try:
  230. data_print = {"categories":["Address"],
  231. "size":[1],
  232. "data":[]}
  233. # List what we found
  234. for n, i in enumerate(out["items"]):
  235. data_print["data"].append([i["address"]])
  236. table(data_print)
  237. center("")
  238. # Error messages
  239. except Exception as e:
  240. if "code" in out:
  241. center("Error code: "+out["code"], "bdrd")
  242. if "message" in out:
  243. center("Error: "+out["message"], "bdrd")
  244. else:
  245. center("Error: "+e, "bdrd")
  246. return
  247. def address_send(amount="", address=""):
  248. if not amount:
  249. amount = input(typing_dots(("Amount to send")))
  250. # Making sure it's actually a number
  251. try:
  252. float(amount)
  253. except:
  254. center("Amount cannot be '"+amount+"'", "bdrd")
  255. return
  256. # Making sure that it's a float
  257. if "." not in str(amount):
  258. amount = str(amount)+".0"
  259. if not address:
  260. address = input(typing_dots("Address to send to", give_space=True))
  261. out = check_output([lbrynet_binary["b"],
  262. "wallet",
  263. "send",
  264. str(amount),
  265. address])
  266. out = json.loads(out)
  267. if "message" in out:
  268. center("Error sending to address: "+out["message"], "bdrd")
  269. else:
  270. center("Successfully sent "+str(amount)+" to "+address, "bdgr")