whallets_cli.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. print(tc._welcome_message())
  17. def cli_main():
  18. """Main program for the cli"""
  19. print(tabulate(tc._main_menu()))
  20. main_menu_choice()
  21. def main_menu_choice():
  22. """main cli operations choice"""
  23. choice = input(tc._menu_choice())
  24. if choice == '1':
  25. add_wallet()
  26. elif choice == '2':
  27. print(tc._missing_operation())
  28. _new_choice()
  29. elif choice == '3':
  30. display_all_wallets()
  31. elif choice == '4':
  32. print(tc._missing_operation())
  33. _new_choice()
  34. elif choice == '5':
  35. print(tc._missing_operation())
  36. _new_choice()
  37. elif choice == '6':
  38. print(tc._missing_operation())
  39. _new_choice()
  40. elif choice.lower() == "q":
  41. quit()
  42. else:
  43. _new_choice()
  44. _new_choice()
  45. def _new_choice():
  46. """Offer user a new choice"""
  47. x = 1
  48. while x < 3:
  49. chc = input(tc._ask_new_choice())
  50. if chc == '1':
  51. cli_main()
  52. elif chc == '2':
  53. quit()
  54. else:
  55. x += 1
  56. else:
  57. quit()
  58. def add_wallet():
  59. """A function to add wallet to the wallets_dict.json"""
  60. new_wallet, addresses, ntw_i = get_inputs()
  61. print(tabulate(tc._save_wallet_confirm(addresses, new_wallet)))
  62. confirm_entry(ntw_i, addresses, new_wallet)
  63. def confirm_entry(ntw_i, addresses, new_wallet):
  64. """Preview the new wallet and confirm saving it"""
  65. x = input(tc._confirm_entry()[0])
  66. if x == '1':
  67. # new_wallet_dict = refactor_wallet(addresses, **new_wallet)
  68. # save_wallet(**new_wallet_dict)
  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
  107. ]
  108. }
  109. }
  110. new_wallet_dictionary[username]["wallets"].update(wallet)
  111. # print(f"\n\n\n{new_wallet_dictionary}")
  112. return new_wallet_dictionary
  113. def remove_wallet():
  114. """Removes wallet from the wallet dictionary"""
  115. # This function needs to be developed
  116. def get_inputs():
  117. """Get infor to add a new wallet"""
  118. ntw_i = _check_network()
  119. network = tc._return_network(ntw_i)
  120. addresses = []
  121. new_wallet = {
  122. "username":" ",
  123. "twitter address":" ",
  124. "ENS":" ",
  125. "info/note":" ",
  126. "address": addresses
  127. }
  128. for key, value in new_wallet.items():
  129. x = input(tc._prompt_new_info(key))
  130. new_item = check_wallet_item(ntw_i, x, key)
  131. if key == 'address':
  132. addresses.append(new_item)
  133. y = input(tc._ask_more_wallets()[0])
  134. while y == '1':
  135. new_item = input(tc._ask_more_wallets()[1])
  136. addresses.append(new_item)
  137. y = input(tc._ask_more_wallets()[0])
  138. new_wallet[key] = new_item
  139. new_wallet["Network"] = network
  140. return new_wallet, addresses, ntw_i
  141. def check_wallet_item(ntw_i,x,key,):
  142. """
  143. Scans through wallets to check if a new wallet is not already in the
  144. database
  145. """
  146. dict = get_wallets()[ntw_i]
  147. if x == "" or x == " ":
  148. new_item = x
  149. else:
  150. for a,b in dict.items():
  151. if x == a or x == b:
  152. # print("\n\na or b\n\n")
  153. new_item = _correct_item(x, key)
  154. for v in b.values():
  155. if x == v:
  156. # print("\n\nv\n\n")
  157. new_item = _correct_item(x, key)
  158. return new_item
  159. else:
  160. new_item = x
  161. return new_item
  162. # username = key
  163. # wallets = value['wallets']
  164. # info = value['info']
  165. # twitter = value['twitter']
  166. # ens_saved = value['ens']
  167. #
  168. # if x == username:
  169. # new_item = _correct_item(x,'username')
  170. # elif x in wallets.values():
  171. # new_item = _correct_item(x,'wallet address')
  172. # elif x == twitter:
  173. # new_item = _correct_item(x,'twitter address')
  174. # elif x == ens_saved:
  175. # new_item = _correct_item(x, 'ENS')
  176. # else:
  177. # new_item = x
  178. def _correct_item(x,key):
  179. """Allows user to rewrite an exisitng item in the wallet"""
  180. choice = input(tc._display_wallet_check_result(key)[0])
  181. if choice == '1':
  182. new_item = x
  183. elif choice == '2':
  184. new_item = input(tc._enter_new_info(key))
  185. return new_item
  186. def _check_network():
  187. """Check if the existing network"""
  188. print(tabulate(tc._choose_network()))
  189. ntw = int(input(tc._menu_choice()))
  190. i = ntw - 1
  191. if i != 0:
  192. print(tc._missing_operation())
  193. _new_choice()
  194. else:
  195. return i
  196. def get_wallets():
  197. """ Gets the info from the dictionary """
  198. filename = 'wallets_dict.json'
  199. with open(filename) as f:
  200. all_wallets = json.load(f)
  201. evm_wallets = all_wallets['evm_wallets']
  202. spl_wallets = all_wallets['spl_wallets']
  203. return evm_wallets, spl_wallets,
  204. def table_format_wallets(chain):
  205. """Format wallets to table"""
  206. # Chains available 'evm','spl'
  207. # evm shows all the forks
  208. i = _chain_index(chain)
  209. dict = get_wallets()[i]
  210. print(f"\n\n{chain.upper()} WALLETS:")
  211. # for key, value in dict.items():
  212. # user = key
  213. # info = value['info']
  214. # twitter = value['twitter']
  215. # ens = value['ens']
  216. # wallets = {}
  217. # wlts = value['wallets']
  218. # for x, y in wlts.items():
  219. # wallets[x] = y
  220. #
  221. #
  222. # print(f"\nUser: {user}")
  223. # print(f"Info: {info}")
  224. # print(f"Twitter: {twitter}")
  225. # for wlt,inf in wallets.items():
  226. # print(f"Wallets: {wlt}")
  227. # print(f"Address: {inf['address']}")
  228. # print(f"Active networks:")
  229. # networks = inf['networks']
  230. # networks_str = ', '.join(networks)
  231. # print(networks_str)
  232. # table = []
  233. line_0 = [
  234. "#", "USER", "ENS", "TWITTER", "INFO", "ADDRESSES", "NETWORKS"]
  235. line_ = ["==", "=========", "===========", "===========",
  236. "===========", "===========", "==========="]
  237. table = [line_0, line_,]
  238. for i, (key, value) in enumerate(dict.items()):
  239. index = i + 1
  240. user = key
  241. info = value['info']
  242. twitter = value['twitter']
  243. ens = value['ens']
  244. wallets = {}
  245. wlts = value['wallets']
  246. for x, y in wlts.items():
  247. wallets[x] = y
  248. line = [index, user, ens, twitter, info]
  249. x = 1
  250. for wlt,inf in wallets.items():
  251. address = inf['address']
  252. networks = inf['networks']
  253. networks_str = ', '.join(networks)
  254. if x == 1:
  255. line.append(address)
  256. line.append(networks_str)
  257. x += 1
  258. else:
  259. line = [' ',' ',' ',' ',' ',address,networks_str]
  260. x += 1
  261. table.append(line)
  262. return table
  263. def csv_export():
  264. """Exports the wallets to csv files"""
  265. # Need to add SPL wallets when they are relevant
  266. table = table_format_wallets('evm')
  267. file = 'data/whallets.csv'
  268. with open(file, 'w') as output:
  269. new_writer = csv.writer(output)
  270. for row in table:
  271. new_writer.writerow(row)
  272. print(tc._csv_exported())
  273. def _chain_index(chain):
  274. """Asigns an index based on given parameter of the chain"""
  275. if chain.lower() == 'evm':
  276. i = 0
  277. elif chain.lower() == 'spl':
  278. i = 1
  279. return i
  280. def display_wallets(chain):
  281. """Displays the wallets according the given chain"""
  282. table = table_format_wallets(chain)
  283. print(tabulate(table))
  284. def display_all_wallets():
  285. display_wallets('evm')
  286. display_wallets('spl')
  287. # Run the program
  288. if __name__ == '__main__':
  289. cli_main()