-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_menu.py
330 lines (293 loc) · 10.6 KB
/
run_menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import os
from loguru import logger
from dotenv import load_dotenv
load_dotenv('.env')
from conf import settings
from src.wallet.WalletController import WalletController
from src.MarketController import MarketController
from src.market.util.custom_exceptions import NoMarketBuyersExceptions
from src.api.exception.APIException import (
NoMarketSessionException,
MarketSessionException,
MarketWalletAddressException,
MarketAccountException
)
# logger:
format = "{time:YYYY-MM-DD HH:mm:ss} | {level:<5} | {message}"
logger.add("files/logfile.log", format=format, level='DEBUG', backtrace=True)
logger.info("-" * 79)
def main_no_installation():
while True:
_clear_console()
print(" MAIN MENU - No Wallet Detected")
print("1 - New Installation")
_sep()
print("0 - Exit")
_empty()
choice = input("Please make a choice: ")
if choice == "1":
installation_menu()
return
elif choice == "0":
exit("Exit.")
else:
print("Invalid option.")
def main():
while True:
_clear_console()
print(" MARKET MAIN MENU - Wallet Detected")
print("1 - Market Operations")
print("2 - Market Configuration")
print("3 - Wallet Operations")
_sep()
print("0 - Exit")
_empty()
choice = input("Please make a choice: ")
if choice == "1":
market_menu()
elif choice == "2":
market_configuration()
elif choice == "3":
wallet_menu()
elif choice == "0":
exit("Exit.")
else:
print("Invalid option.")
def installation_menu():
_clear_console()
print("This will create a new market wallet & account.")
choice = input("Proceed? (Y/n)")
if choice.lower() == "y":
wallet = WalletController()
wallet.create_wallet(store_mnemonic=True)
wallet.create_account()
address = wallet.get_address()
print("Market Wallet address (use it to transfer tokens):")
print(address)
input("Press any key to pass.")
return
def market_configuration():
try:
market = MarketController()
except Exception as e:
logger.exception(f"Unable to login to the platform: {e}")
input("Press any key to return to main menu.")
return
while True:
_clear_console()
print(" Market Config MENU")
print("1 - Register market wallet address")
print("2 - Get current market wallet address")
print("3 - Update market wallet address")
_sep()
print("\\ - Return to previous menu.")
print("0 - Exit")
_empty()
choice = input("Please make a choice: ")
if choice == "1":
try:
address = input("Enter market wallet address: ")
market.register_market_wallet_address(address=address)
except MarketWalletAddressException:
pass
elif choice == "2":
try:
market.get_market_wallet_address()
except MarketWalletAddressException:
logger.error("Failed to get wallet address.")
elif choice == "3":
try:
new_address = input("New market wallet address: ")
market.update_market_wallet_address(
new_address=new_address
)
except MarketWalletAddressException:
pass
elif choice == "\\":
return
elif choice == "0":
exit("Exit.")
else:
print("Invalid option.")
input("Press any key to pass.")
def market_menu():
try:
market = MarketController()
except Exception:
logger.exception("Unable to login to the platform")
input("Press any key to return to main menu.")
return
while True:
_clear_console()
print(" Market OPS MENU")
print("1 - Open market session")
print("2 - Get bids for latest market session")
print("3 - Approve market bids")
print("4 - Close market session")
print("5 - Run market session")
print("6 - Get users market balance")
print("7 - Transfer token balance back to agents")
print("8 - Validate token transfers")
print("9 - List last session available.")
print("10 - Change session status.")
_sep()
print("\\ - Return to previous menu.")
print("0 - Exit")
_empty()
choice = input("Please make a choice: ")
if choice == "1":
try:
# Create first market session:
market.open_market_session()
except (NoMarketSessionException, MarketSessionException) as ex:
logger.error(ex)
except Exception:
logger.exception("Failed to open session.")
elif choice == "2":
try:
# Create first market session:
market.get_buyers_bids()
except (NoMarketSessionException, MarketSessionException) as ex:
logger.error(ex)
except Exception:
logger.exception("Failed to list bids.")
elif choice == "3":
try:
# Approve buyers bids:
market.approve_buyers_bids()
except (NoMarketSessionException, MarketSessionException) as ex:
logger.error(ex)
except Exception:
logger.exception("Failed to approve bids.")
elif choice == "4":
try:
# Close market session (no more bids):
market.close_market_session()
except (NoMarketSessionException, MarketSessionException) as ex:
logger.error(ex)
except Exception:
logger.exception("Failed to close session.")
elif choice == "5":
try:
# Run market session:
if settings.RUN_REAL_MARKET:
market.run_market_session()
else:
market.run_fake_market_session()
except NoMarketBuyersExceptions:
logger.error("Insuficient market bids (buyers) to create a new session.")
except BaseException:
logger.exception("Failed to run market session.")
elif choice == "6":
try:
# List users market balance:
market.list_user_market_balance()
except MarketAccountException as ex:
logger.error(ex)
except BaseException:
logger.exception("Failed to list user market balance.")
elif choice == "7":
try:
# Transfer tokens back to clients:
market.transfer_tokens_out()
except MarketSessionException as ex:
logger.error(ex)
except Exception:
logger.exception("Failed to transfer tokens out")
elif choice == "8":
try:
# Validate final token balance transfers:
market.validate_tokens_transfer()
except MarketSessionException as ex:
logger.error(ex)
except Exception:
logger.exception("Failed to validate tokens transfer")
elif choice == "9":
try:
# Close market session (no more bids):
market.list_last_session()
except NoMarketSessionException as ex:
logger.error(ex)
except Exception:
logger.exception("Failed to list session.")
elif choice == "10":
try:
# Close market session (no more bids):
session_id = int(input("Session ID: "))
new_status = input("New session status [options: staged,open,closed,running,finished]: ")
market.set_session_status(session_id, new_status)
except (NoMarketSessionException, MarketSessionException) as ex:
logger.error(ex)
except Exception:
logger.exception("Failed to list session.")
elif choice == "\\":
return
elif choice == "0":
exit("Exit.")
else:
print("Invalid option.")
input("Press any key to pass.")
def wallet_menu():
wallet = WalletController()
while True:
_clear_console()
print(" Wallet OPS MENU")
print("1 - Get wallet address")
print("2 - Get wallet balance")
print("3 - Transfer balance to address")
_sep()
print("\\ - Return to previous menu.")
print("0 - Exit")
_empty()
choice = input("Please make a choice: ")
if choice == "1":
try:
address = wallet.get_address()
print(f"Wallet Address: {address}")
except Exception as ex:
logger.exception(repr(ex))
elif choice == "2":
# Approve buyers bids:
try:
balance = wallet.get_balance()
print(f"Wallet Balance: {balance}i")
except Exception as ex:
logger.exception(repr(ex))
elif choice == "3":
# Close market session (no more bids):
try:
amount = input("Enter transfer amount "
"(use 'FB' keyword for full balance "
"transfer): ")
if amount.lower() == "fb":
amount = wallet.get_balance()['baseCoin']['available']
else:
amount = int(amount)
out_address = input("Enter output address: ")
# -- initialize WALLET controller:
node_response = wallet.transfer_tokens(
amount=amount,
address=out_address
)
print("Node Response:", node_response)
except Exception as ex:
logger.exception(repr(ex))
elif choice == "\\":
return
elif choice == "0":
exit("Exit.")
else:
print("Invalid option.")
input("Press any key to pass.")
def _clear_console():
import os
os.system('cls' if os.name == 'nt' else 'clear')
def _empty():
print("")
def _sep():
print("===========================================")
if __name__ == '__main__':
wallet_path = os.path.join(settings.WALLET_STORAGE_PATH)
if not os.path.exists(wallet_path):
main_no_installation()
main()