whallets_cli.py 9.3 KB

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