whallets_cli.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. """CLI for users to manage the wallet database and run research functions"""
  2. #TODO:
  3. # 1) Learn operating Blockscan APIs
  4. # 2) Program functions pulling APIs data based on users interaction
  5. # 3) CLI function to easy add new wallets and other data to the wallets_dict.json
  6. # - loop through each info separately
  7. # - ask to re-add info if matches
  8. # - ask for additional wallets for the same user
  9. # - add and print all the added wallets
  10. # - csv export (get away the line row)
  11. # 4) Prints options for the user (-h)
  12. import json
  13. from tabulate import tabulate
  14. import texts_cli as tc
  15. import csv
  16. def cli_main():
  17. """Main program for the cli"""
  18. print(tabulate(tc._main_menu()))
  19. main_menu_choice()
  20. def main_menu_choice():
  21. """main cli operations choice"""
  22. choice = input(tc._menu_choice())
  23. if choice == '1':
  24. add_wallet()
  25. elif choice == '2':
  26. print(tc._missing_operation())
  27. _new_choice()
  28. elif choice == '3':
  29. display_all_wallets()
  30. elif choice == '4':
  31. print(tc._missing_operation())
  32. _new_choice()
  33. elif choice == '5':
  34. print(tc._missing_operation())
  35. _new_choice()
  36. elif choice == '6':
  37. csv_export()
  38. _new_choice()
  39. elif choice.lower() == "q":
  40. quit()
  41. else:
  42. _new_choice()
  43. _new_choice()
  44. def _new_choice():
  45. """Offer user a new choice"""
  46. x = 1
  47. while x < 3:
  48. chc = input(tc._ask_new_choice())
  49. if chc == '1':
  50. cli_main()
  51. elif chc == '2':
  52. quit()
  53. else:
  54. x += 1
  55. else:
  56. quit()
  57. def add_wallet():
  58. """A function to add wallet to the wallets_dict.json"""
  59. new_wallet, addresses, ntw_i = get_inputs()
  60. print(tabulate(tc._save_wallet_confirm(addresses, new_wallet)))
  61. confirm_entry(ntw_i, addresses, new_wallet)
  62. def confirm_entry(ntw_i, addresses, new_wallet):
  63. """Preview the new wallet and confirm saving it"""
  64. x = input(tc._confirm_entry()[0])
  65. if x == '1':
  66. print(tc._confirm_entry()[1])
  67. new_wallet_dictionary = refactor_wallet(addresses, new_wallet)
  68. save_wallet(ntw_i, new_wallet_dictionary)
  69. refactor_wallet(addresses, new_wallet)
  70. x = input(tc._confirm_entry()[2])
  71. if x == '1':
  72. add_wallet()
  73. def save_wallet(ntw_i, new_wallet_dictionary):
  74. """Saves the wallet into the database/dictionary and informs the user"""
  75. filename = 'wallets_dict.json'
  76. with open(filename) as f:
  77. all_wallets = json.load(f)
  78. dict_0 = all_wallets['evm_wallets']
  79. dict_1 = all_wallets['spl_wallets']
  80. dicts = [dict_0,dict_1]
  81. dict = dicts[ntw_i]
  82. dict.update(new_wallet_dictionary)
  83. with open(filename, 'w') as f:
  84. json.dump(all_wallets,f, indent=4)
  85. def refactor_wallet(addresses, new_wallet):
  86. """Refactor the wallet items to the wallet_dict format"""
  87. username = new_wallet["username"]
  88. twtr = new_wallet["twitter address"]
  89. ens = new_wallet["ENS"]
  90. info = new_wallet["info/note"]
  91. new_wallet_dictionary = {username: {
  92. "twitter": twtr,
  93. "info": info,
  94. "ens":ens,
  95. "wallets": {}
  96. }
  97. }
  98. for idx, addr in enumerate(addresses):
  99. wallet = {f"wallet_{idx}":{
  100. "address":f"{addr}",
  101. "networks":[
  102. "erc"
  103. # need to add a code how to add networks automatically
  104. ]
  105. }
  106. }
  107. new_wallet_dictionary[username]["wallets"].update(wallet)
  108. return new_wallet_dictionary
  109. def remove_wallet():
  110. """Removes wallet from the wallet dictionary"""
  111. # This function needs to be developed
  112. def get_inputs():
  113. """Get infor to add a new wallet"""
  114. ntw_i = _check_network()
  115. network = tc._return_network(ntw_i)
  116. addresses = []
  117. new_wallet = {
  118. "username":" ",
  119. "twitter address":" ",
  120. "ENS":" ",
  121. "info/note":" ",
  122. "address": addresses
  123. }
  124. for key, value in new_wallet.items():
  125. x = input(tc._prompt_new_info(key))
  126. new_item = check_wallet_item(ntw_i, x, key)
  127. if key == 'address':
  128. addresses.append(new_item)
  129. y = input(tc._ask_more_wallets()[0])
  130. while y == '1':
  131. new_item = input(tc._ask_more_wallets()[1])
  132. addresses.append(new_item)
  133. y = input(tc._ask_more_wallets()[0])
  134. new_wallet[key] = new_item
  135. new_wallet["Network"] = network
  136. return new_wallet, addresses, ntw_i
  137. def check_wallet_item(ntw_i,x,key,):
  138. """
  139. Scans through wallets to check if a new wallet is not already in the
  140. database
  141. """
  142. dict = get_wallets()[ntw_i]
  143. if x == "" or x == " ":
  144. new_item = x
  145. else:
  146. for a,b in dict.items():
  147. if x == a or x == b:
  148. # print("\n\na or b\n\n")
  149. new_item = _correct_item(x, key)
  150. for v in b.values():
  151. if x == v:
  152. # print("\n\nv\n\n")
  153. new_item = _correct_item(x, key)
  154. return new_item
  155. else:
  156. new_item = x
  157. return new_item
  158. def _correct_item(x,key):
  159. """Allows user to rewrite an exisitng item in the wallet"""
  160. choice = input(tc._display_wallet_check_result(key)[0])
  161. if choice == '1':
  162. new_item = x
  163. elif choice == '2':
  164. new_item = input(tc._enter_new_info(key))
  165. return new_item
  166. def _check_network():
  167. """Check if the existing network"""
  168. print(tabulate(tc._choose_network()))
  169. ntw = int(input(tc._menu_choice()))
  170. i = ntw - 1
  171. if i != 0:
  172. print(tc._missing_operation())
  173. _new_choice()
  174. else:
  175. return i
  176. def get_wallets():
  177. """ Gets the info from the dictionary """
  178. filename = 'wallets_dict.json'
  179. with open(filename) as f:
  180. all_wallets = json.load(f)
  181. evm_wallets = all_wallets['evm_wallets']
  182. spl_wallets = all_wallets['spl_wallets']
  183. return evm_wallets, spl_wallets,
  184. def table_format_wallets(chain):
  185. """Format wallets to table"""
  186. # Chains available 'evm','spl'
  187. # evm shows all the forks
  188. i = _chain_index(chain)
  189. dict = get_wallets()[i]
  190. print(f"\n\n{chain.upper()} WALLETS:")
  191. line_0, line_ = tc._table_headers()
  192. table = [line_0, line_,]
  193. for i, (key, value) in enumerate(dict.items()):
  194. index = i + 1
  195. user = key
  196. info = value['info']
  197. twitter = value['twitter']
  198. ens = value['ens']
  199. wallets = {}
  200. wlts = value['wallets']
  201. for x, y in wlts.items():
  202. wallets[x] = y
  203. line = [index, user, ens, twitter, info]
  204. x = 1
  205. for wlt,inf in wallets.items():
  206. address = inf['address']
  207. networks = inf['networks']
  208. networks_str = ', '.join(networks)
  209. if x == 1:
  210. line.append(address)
  211. line.append(networks_str)
  212. x += 1
  213. else:
  214. line = [' ',' ',' ',' ',' ',address,networks_str]
  215. x += 1
  216. table.append(line)
  217. return table
  218. def csv_export():
  219. """Exports the wallets to csv files"""
  220. # Need to add SPL & BTC wallet option when relevant
  221. table = table_format_wallets('evm')
  222. del table[1]
  223. file = 'data/whallets.csv'
  224. with open(file, 'w') as output:
  225. new_writer = csv.writer(output)
  226. for row in table:
  227. new_writer.writerow(row)
  228. print(tc._csv_exported())
  229. def _chain_index(chain):
  230. """Asigns an index based on given parameter of the chain"""
  231. if chain.lower() == 'evm':
  232. i = 0
  233. elif chain.lower() == 'spl':
  234. i = 1
  235. return i
  236. def display_wallets(chain):
  237. """Displays the wallets according the given chain"""
  238. table = table_format_wallets(chain)
  239. print(tabulate(table))
  240. def display_all_wallets():
  241. display_wallets('evm')
  242. display_wallets('spl')
  243. # Run the program
  244. if __name__ == '__main__':
  245. print(tc._welcome_message())
  246. cli_main()