-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
92 lines (82 loc) · 2.79 KB
/
bot.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
import monitor
import trade
import web3dex
import os
import logging
import sys
import asyncio
import json
config_dir = 'configs'
wallet_dir = 'wallets'
log_file = 'bot.log'
file_handler = logging.FileHandler(filename=log_file)
stdout_handler = logging.StreamHandler(sys.stdout)
handlers = [file_handler, stdout_handler]
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S',
handlers=handlers)
async def looper():
while True:
try:
await main()
except Exception as err:
print("ERROR", err)
finally:
await asyncio.sleep(30)
async def main():
for filename in os.listdir(config_dir):
f = os.path.join(config_dir, filename)
if os.path.isfile(f) and filename.startswith('config_') and filename.endswith('.json'):
try:
conf = monitor.Config.read(f)
gap = await monitorize(conf, filename)
await trading(conf, gap)
except Exception as err:
print("ERROR", err)
async def monitorize(conf: monitor.Config, filename: str):
gaps = await monitor.main(conf)
logging.info("> {} {} gaps".format(filename, len(gaps)))
if len(gaps) == 0:
return None
max = gaps[0]
for gap in gaps:
if gap['gap'] > max['gap']:
max = gap
logging.info("> {} gap: {} vs {}".format(max['gap'], max['dex0_platform'], max['dex1_platform']))
logging.info("> {}".format(max))
return max
async def trading(conf: monitor.Config, gap: dict):
if gap is None:
return
filename = conf.chain.lower() + ".json"
f = os.path.join(wallet_dir, filename)
if not os.path.isfile(f):
logging.info("> {} not found".format(filename))
return
file = open(f)
walletconf = json.loads(file.read())
file.close()
for dex in web3dex.all[conf.chain.lower()]:
if dex.platform == gap['dex0_platform'].replace("_dexcoin", ""):
dex0 = dex
if dex.platform == gap['dex1_platform'].replace("_dexcoin", ""):
dex1 = dex
await trade.main(
wallet_address = walletconf['wallet_address'],
private_key = walletconf['private_key'],
dex0 = dex0,
dex1 = dex1,
amount = walletconf['amount'],
min_gap = walletconf['min_gap'],
path0_inToken = conf.input,
path0_outToken = conf.output,
path0_middleToken = dex0.token if gap['dex0_platform'].endswith('_dexcoin') else None,
path1_inToken = conf.input,
path1_outToken = conf.output,
path1_middleToken = dex1.token if gap['dex1_platform'].endswith('_dexcoin') else None,
)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(looper())