whallets_cli.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. # 4) Prints options for the user (-h)
  9. import json
  10. from tabulate import tabulate
  11. import texts_cli as tc
  12. print(tc._welcome_message())
  13. def cli_main():
  14. """Main program for the cli"""
  15. print(tabulate(tc._main_menu()))
  16. main_menu_choice()
  17. def main_menu_choice():
  18. """main cli operations choice"""
  19. choice = input(tc._menu_choice())
  20. if choice == '1':
  21. add_wallet()
  22. elif choice == '2':
  23. print(tc._missing_operation())
  24. elif choice == '3':
  25. display_all_wallets()
  26. elif choice == '4':
  27. print(tc._missing_operation())
  28. _new_choice()
  29. elif choice == '5':
  30. print(tc._missing_operation())
  31. _new_choice()
  32. elif choice.lower() == "q":
  33. quit()
  34. else:
  35. _new_choice()
  36. _new_choice()
  37. def _new_choice():
  38. """Offer user a new choice"""
  39. x = 1
  40. while x < 3:
  41. chc = input(tc._ask_new_choice())
  42. if int(chc) == 1:
  43. cli_main()
  44. elif int(chc) == 2:
  45. quit()
  46. else:
  47. x += 1
  48. else:
  49. quit()
  50. def add_wallet():
  51. """A function to add wallet to the wallets_dict.json"""
  52. chain, name, addr, inf, twtr, ens = _get_inputs()
  53. msg = tc._check_wallet_result(chain, name, addr, twtr, ens)
  54. print(msg)
  55. def remove_wallet():
  56. """Removes wallet from the wallet dictionary"""
  57. # This function needs to be developped
  58. def _get_inputs():
  59. """Get infor to add a new wallet"""
  60. chain = int(input(tc._choose_chain()))
  61. name = input(tc._prompt_name())
  62. addr = input(tc._prompt_address())
  63. inf = input(tc._prompt_info())
  64. twtr = input(tc._prompt_twitter())
  65. ens = input(tc._prompt_ens())
  66. return chain, name, addr, inf, twtr, ens
  67. def check_wallet(chain, name, addr, twtr, ens):
  68. """
  69. Scans through wallets to check if a new wallet is not already in the
  70. database
  71. """
  72. i = chain - 1
  73. dict = get_wallets()[i]
  74. for key, value in dict.items():
  75. user = key
  76. wallets = value['wallets']
  77. info = value['info']
  78. twitter = value['twitter']
  79. ens_saved = value['ens']
  80. if user == name:
  81. return 0
  82. elif addr in wallets.values():
  83. return 1
  84. elif twtr == twitter:
  85. return 2
  86. elif ens == ens_saved:
  87. return 3
  88. else:
  89. return 4
  90. def get_wallets():
  91. """ Gets the info from the dictionary """
  92. filename = 'wallets_dict.json'
  93. with open(filename) as f:
  94. all_wallets = json.load(f)
  95. evm_wallets = all_wallets['evm_wallets']
  96. spl_wallets = all_wallets['spl_wallets']
  97. return evm_wallets, spl_wallets
  98. def display_wallets(chain):
  99. """Displays the wallets according the given chain"""
  100. # Chains available 'evm','spl'
  101. # evm shows all the forks
  102. i = _chain_index(chain)
  103. dict = get_wallets()[i]
  104. print(f"\n\n{chain.upper()} WALLETS:")
  105. for key, value in dict.items():
  106. user = key
  107. info = value['info']
  108. twitter = value['twitter']
  109. wallets = {}
  110. wlts = value['wallets']
  111. for x, y in wlts.items():
  112. wallets[x] = y
  113. print(f"\nUser: {user}")
  114. print(f"Info: {info}")
  115. print(f"Twitter: {twitter}")
  116. for wlt,inf in wallets.items():
  117. print(f"Wallets: {wlt}")
  118. print(f"Address: {inf['address']}")
  119. print(f"Active networks:")
  120. networks = inf['networks']
  121. networks_str = ', '.join(networks)
  122. print(networks_str)
  123. def _chain_index(chain):
  124. """Asigns an index based on given parameter of the chain"""
  125. if chain.lower() == 'evm':
  126. i = 0
  127. elif chain.lower() == 'spl':
  128. i = 1
  129. return i
  130. def display_all_wallets():
  131. display_wallets('evm')
  132. display_wallets('spl')
  133. # Run the program
  134. cli_main()