whallets_cli.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. # 4) Prints options for the user (-h)
  11. import json
  12. from tabulate import tabulate
  13. import texts_cli as tc
  14. print(tc._welcome_message())
  15. def cli_main():
  16. """Main program for the cli"""
  17. print(tabulate(tc._main_menu()))
  18. main_menu_choice()
  19. def main_menu_choice():
  20. """main cli operations choice"""
  21. choice = input(tc._menu_choice())
  22. if choice == '1':
  23. add_wallet()
  24. elif choice == '2':
  25. print(tc._missing_operation())
  26. _new_choice()
  27. elif choice == '3':
  28. display_all_wallets()
  29. elif choice == '4':
  30. print(tc._missing_operation())
  31. _new_choice()
  32. elif choice == '5':
  33. print(tc._missing_operation())
  34. _new_choice()
  35. elif choice.lower() == "q":
  36. quit()
  37. else:
  38. _new_choice()
  39. _new_choice()
  40. def _new_choice():
  41. """Offer user a new choice"""
  42. x = 1
  43. while x < 3:
  44. chc = input(tc._ask_new_choice())
  45. if chc == '1':
  46. cli_main()
  47. elif chc == '2':
  48. quit()
  49. else:
  50. x += 1
  51. else:
  52. quit()
  53. def add_wallet():
  54. """A function to add wallet to the wallets_dict.json"""
  55. new_wallet, addresses = get_inputs()
  56. print(tabulate(tc._save_wallet_confirm(addresses, new_wallet)))
  57. confirm_entry(addresses, new_wallet)
  58. def confirm_entry(addresses, new_wallet):
  59. """Preview the new wallet and confirm saving it"""
  60. x = input(tc._confirm_entry()[0])
  61. if x == '1':
  62. # new_wallet_dict = refactor_wallet(addresses, **new_wallet)
  63. # save_wallet(**new_wallet_dict)
  64. print(tc._confirm_entry()[1])
  65. x = input(tc._confirm_entry()[2])
  66. if x == '1':
  67. add_wallet()
  68. def save_wallet(**new_wallet_dict):
  69. """Saves the wallet into the database/dictionary and informs the user"""
  70. def refactor_wallet(addresses, new_wallet):
  71. """Refactor the wallet items to the wallet_dict format"""
  72. new_wallet_dictionary = {new_wallet["username"]: {
  73. "twitter": new_wallet["twitter address"]
  74. "info": new_wallet["info/note"]
  75. "ens":new_wallet["ENS"]
  76. "wallets": {
  77. }
  78. }
  79. # This is the result aiming for:
  80. # {
  81. # "evm_wallets": {
  82. # "@vitalik.eth": {
  83. # "twitter": "https://twitter.com/VitalikButerin",
  84. # "info": "ETH foundation, influencer, researcher, cult",
  85. # "ens": "vitalik.eth",
  86. # "wallets": {
  87. # "wallet_0": {
  88. # "address": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
  89. # "networks": [
  90. # "erc",
  91. # "bsc",
  92. # "poly",
  93. # "heco",
  94. # "ftm",
  95. # "avax",
  96. # "optm",
  97. # "arb"
  98. # ]
  99. # },
  100. def remove_wallet():
  101. """Removes wallet from the wallet dictionary"""
  102. # This function needs to be developed
  103. def get_inputs():
  104. """Get infor to add a new wallet"""
  105. ntw_i = _check_network()
  106. network = tc._return_network(ntw_i)
  107. addresses = []
  108. new_wallet = {
  109. "username":" ",
  110. "twitter address":" ",
  111. "ENS":" ",
  112. "info/note":" ",
  113. "address": addresses
  114. }
  115. for item, value in new_wallet.items():
  116. x = input(tc._prompt_new_info(item))
  117. new_item = check_wallet_item(ntw_i,x)
  118. if item == 'address':
  119. addresses.append(new_item)
  120. y = input(tc._ask_more_wallets()[0])
  121. while y == '1':
  122. new_item = input(tc._ask_more_wallets()[1])
  123. addresses.append(new_item)
  124. y = input(tc._ask_more_wallets()[0])
  125. new_wallet[item] = new_item
  126. new_wallet["Network"] = network
  127. return new_wallet, addresses
  128. def _check_network():
  129. """Check if the existing network"""
  130. print(tabulate(tc._choose_network()))
  131. ntw = int(input(tc._menu_choice()))
  132. i = ntw - 1
  133. if i != 0:
  134. print(tc._missing_operation())
  135. _new_choice()
  136. else:
  137. return i
  138. def check_wallet_item(ntw_i,x):
  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. for key, value in dict.items():
  145. username = key
  146. wallets = value['wallets']
  147. info = value['info']
  148. twitter = value['twitter']
  149. ens_saved = value['ens']
  150. if x == username:
  151. new_item = _correct_item(x,'username')
  152. elif x in wallets.values():
  153. new_item = _correct_item(x,'wallet address')
  154. elif x == twitter:
  155. new_item = _correct_item(x,'twitter address')
  156. elif x == ens_saved:
  157. new_item = _correct_item(x, 'ENS')
  158. else:
  159. new_item = x
  160. return new_item
  161. def _correct_item(x,z):
  162. """Allows user to rewrite an exisitng item in the wallet"""
  163. a = '2'
  164. while a == '2':
  165. a = (input(tc._display_wallet_check_result(z)[0]))
  166. if a == '1':
  167. new_item = x
  168. elif a == '2':
  169. new_item = input(tc._enter_new_info(z))
  170. if new_item == x:
  171. a = '2'
  172. else:
  173. new_item = x
  174. break
  175. return new_item
  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 display_wallets(chain):
  185. """Displays the wallets according the given chain"""
  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. for key, value in dict.items():
  192. user = key
  193. info = value['info']
  194. twitter = value['twitter']
  195. wallets = {}
  196. wlts = value['wallets']
  197. for x, y in wlts.items():
  198. wallets[x] = y
  199. print(f"\nUser: {user}")
  200. print(f"Info: {info}")
  201. print(f"Twitter: {twitter}")
  202. for wlt,inf in wallets.items():
  203. print(f"Wallets: {wlt}")
  204. print(f"Address: {inf['address']}")
  205. print(f"Active networks:")
  206. networks = inf['networks']
  207. networks_str = ', '.join(networks)
  208. print(networks_str)
  209. def _chain_index(chain):
  210. """Asigns an index based on given parameter of the chain"""
  211. if chain.lower() == 'evm':
  212. i = 0
  213. elif chain.lower() == 'spl':
  214. i = 1
  215. return i
  216. def display_all_wallets():
  217. display_wallets('evm')
  218. display_wallets('spl')
  219. # Run the program
  220. cli_main()