whallets_cli.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. """CLI managing a wallet database and run research functions"""
  2. #TODO:
  3. # 1) Clean and simplify the code
  4. # - comment off all the SPL and BTC options
  5. # - remove dics and use csv & pandas only
  6. # 2) Add Blockscan.io (etherscan) API commands
  7. # 3) Program functions pulling APIs data based on users interaction
  8. # 4) DONE - CLI function to easy add new wallets and other data to the wallets_dict.json
  9. # DONE - loop through each info separately
  10. # DONE - ask to re-add info if matches
  11. # DONE - ask for additional wallets for the same user
  12. # DONE - add and print all the added wallets
  13. # DONE - csv export (get away the line row)
  14. # - Network choice on exporting csv option
  15. # 5) Change the custom menu for argparser
  16. # 6) Remove wallet option
  17. # 7) Edit wallet option
  18. import json
  19. from tabulate import tabulate
  20. import texts_cli as tc
  21. import csv
  22. def cli_main():
  23. """Main program for the cli"""
  24. print(tabulate(tc._main_menu()))
  25. main_menu_choice()
  26. def main_menu_choice():
  27. """main cli operations choice"""
  28. choice = input(tc._menu_choice())
  29. if choice == '1':
  30. add_wallet()
  31. elif choice == '2':
  32. display_all_wallets()
  33. elif choice == '3':
  34. print(tc._missing_operation())
  35. _new_choice()
  36. elif choice == '4':
  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 = 'data/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. ntw_i = 0
  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. def _correct_item(x,key):
  160. """Allows user to rewrite an exisitng item in the wallet"""
  161. choice = input(tc._display_wallet_check_result(key)[0])
  162. if choice == '1':
  163. new_item = x
  164. elif choice == '2':
  165. new_item = input(tc._enter_new_info(key))
  166. return new_item
  167. # Commenting out the network option, keeping ETH only - simplify!
  168. # def _check_network():
  169. # """Check if the existing network"""
  170. # print(tabulate(tc._choose_network()))
  171. # ntw = int(input(tc._menu_choice()))
  172. # i = ntw - 1
  173. # if i != 0:
  174. # print(tc._missing_operation())
  175. # _new_choice()
  176. # else:
  177. # return i
  178. def get_wallets():
  179. """ Gets the info from the dictionary """
  180. filename = 'data/whallets_dict.json'
  181. with open(filename) as f:
  182. all_wallets = json.load(f)
  183. evm_wallets = all_wallets['evm_wallets']
  184. spl_wallets = all_wallets['spl_wallets']
  185. return evm_wallets, spl_wallets,
  186. def table_format_wallets(chain):
  187. """Format wallets to table"""
  188. # Chains available 'evm','spl'
  189. # evm shows all the forks
  190. i = _chain_index(chain)
  191. dict = get_wallets()[i]
  192. print(f"\n\n{chain.upper()} WALLETS:")
  193. line_0, line_ = tc._table_headers()
  194. table = [line_0, line_,]
  195. for i, (key, value) in enumerate(dict.items()):
  196. index = i + 1
  197. user = key
  198. info = value['info']
  199. twitter = value['twitter']
  200. ens = value['ens']
  201. wallets = {}
  202. wlts = value['wallets']
  203. for x, y in wlts.items():
  204. wallets[x] = y
  205. line = [index, user, ens, twitter, info]
  206. x = 1
  207. for wlt,inf in wallets.items():
  208. address = inf['address']
  209. networks = inf['networks']
  210. networks_str = ', '.join(networks)
  211. if x == 1:
  212. line.append(address)
  213. line.append(networks_str)
  214. x += 1
  215. else:
  216. line = [' ',' ',' ',' ',' ',address,networks_str]
  217. x += 1
  218. table.append(line)
  219. return table
  220. def csv_export():
  221. """Exports the wallets to csv files"""
  222. # Need to add SPL & BTC wallet option when relevant
  223. table = table_format_wallets('evm')
  224. del table[1]
  225. file = 'data/whallets.csv'
  226. with open(file, 'w') as output:
  227. new_writer = csv.writer(output)
  228. for row in table:
  229. new_writer.writerow(row)
  230. print(tc._csv_exported())
  231. def _chain_index(chain):
  232. """Asigns an index based on given parameter of the chain"""
  233. if chain.lower() == 'evm':
  234. i = 0
  235. elif chain.lower() == 'spl':
  236. i = 1
  237. return i
  238. def display_wallets(chain):
  239. """Displays the wallets according the given chain"""
  240. table = table_format_wallets(chain)
  241. print(tabulate(table))
  242. def display_all_wallets():
  243. display_wallets('evm')
  244. display_wallets('spl')
  245. # Run the program
  246. if __name__ == '__main__':
  247. print(tc._welcome_message())
  248. cli_main()