whallets_cli.py 8.0 KB

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