-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
116 lines (92 loc) · 4.03 KB
/
main.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
import os
import logging
import argparse
import bots.http.app
from bots.services.multiprocessing import service_manager
from bots.services.scenario import services_from_config
from bots.http.traders_handler import from_config as traders_from_config
from bots.services.vega_wallet import from_config as wallet_from_config
from bots.vega_sim.scenario_wallet import from_config as scenario_wallet_from_config
from bots.config.environment import check_env_variables
from bots.api.datanode import check_market_exists, get_statistics, get_healthy_endpoints
from bots.tools.github import download_and_unzip_github_asset
from bots.config.types import load_network_config, read_bots_config
from bots.wallet.cli import VegaWalletCli
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", default="./research-bots-config-stagnet1.toml")
args = parser.parse_args()
tokens = os.getenv("TOKENS", "")
tokens_list = [token.strip() for token in tokens.split(",") if len(token.strip()) > 1]
config = read_bots_config(args.config)
logging.basicConfig(level=logging.DEBUG if config.debug else logging.INFO)
config.update_network_config(
load_network_config(config.network_config_file, config.devops_network_name, config.work_dir)
)
bots.http.app.configure_flask(config.debug)
scenarios_config = config.scenarios
rest_api_endpoints = config.network_config.api.rest.hosts
healthy_rest_endpoints = get_healthy_endpoints(rest_api_endpoints)
if len(healthy_rest_endpoints) < 1:
raise Exception("There is no healthy rest data-node in the network")
required_market_names = [scenarios_config[scenario_name].market_name for scenario_name in scenarios_config]
statistics = get_statistics(healthy_rest_endpoints)
wallet_mutex = None
try:
if config.wallet.download_wallet_binary:
binary_path = download_and_unzip_github_asset(
config.wallet.artifact_name,
statistics["appVersion"] if config.wallet.auto_version else config.wallet.version,
config.work_dir,
config.wallet.repository,
)
# update custom branch
config.wallet.update_binary(binary_path)
except Exception as e:
logging.error(str(e))
return
# before the wallet http server is started we need to use the CLI
cli_wallet = VegaWalletCli(config.wallet)
if not cli_wallet.is_initialized():
cli_wallet.init()
cli_wallet.import_internal_networks()
# before the wallet http server is started we need to use the CLI
cli_wallet = VegaWalletCli(config.wallet)
if not cli_wallet.is_initialized():
cli_wallet.init()
cli_wallet.import_internal_networks()
scenario_wallets = dict()
try:
check_env_variables()
check_market_exists(healthy_rest_endpoints, required_market_names)
scenario_wallets = scenario_wallet_from_config(config.scenarios, cli_wallet)
traders_svc = traders_from_config(config, cli_wallet, scenario_wallets, tokens_list)
bots.http.app.handler(path="/traders", handler_func=lambda: traders_svc.serve())
except Exception as e:
logging.error(str(e))
return
services = [
wallet_from_config(config.wallet),
]
services += services_from_config(
config.vega_market_sim_network_name,
scenario_wallets,
scenarios_config,
os.path.dirname(config.network_config.file_path),
config.wallet,
wallet_mutex,
)
processes = service_manager(services)
try:
bots.http.app.run(config.debug, config.http_server)
except Exception as e:
logging.error("HTTP Server failed with error: " + str(e))
# for process in processes:
# process.kill()
if __name__ == "__main__":
with_pprof = os.getenv("WITH_PPROF", "")
if with_pprof != "":
logging.info("Starting pprof server on port 8081")
from pypprof.net_http import start_pprof_server
start_pprof_server(port=8081)
main()