-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonitor.py
123 lines (113 loc) · 4.65 KB
/
monitor.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
import asyncio
import web3dex
import datetime
import calendar
import sys
import os
import json
import logging
class Config:
def __init__(self, chain, input, output, routing, indecimals, outdecimals, min_gap, min_base_liquidity, timeout, daemon, dexes):
self.chain = chain
self.input = input
self.output = output
self.routing = routing
self.indecimals = indecimals
self.outdecimals = outdecimals
self.min_gap = min_gap
self.min_base_liquidity = min_base_liquidity
self.timeout = timeout
self.daemon = daemon
self.dexes = dexes
@staticmethod
def read(conf_file):
file = open(conf_file)
conf = file.read()
file.close()
res = json.loads(conf, object_hook=Config.from_json)
filename = os.path.basename(conf_file)
res.log_file = f"{filename}.log"
res.csv_file = f"{filename}.csv"
return res
@staticmethod
def from_json(json):
return Config(json['chain'],
json['input'], json['output'], json['routing'],
json['indecimals'], json['outdecimals'], json['min_gap'], json['min_base_liquidity'],
json['timeout'], json['daemon'], json['dexes'])
async def looper():
config_file = sys.argv[1]
conf = Config.read(config_file)
file_handler = logging.FileHandler(filename=conf.log_file)
stdout_handler = logging.StreamHandler(sys.stdout)
handlers = [file_handler, stdout_handler]
while True:
try:
await main(conf)
except Exception as err:
print("ERROR", err)
finally:
if not conf.daemon:
return
await asyncio.sleep(conf.timeout)
async def main(conf):
values = {}
block_number = web3dex.all[conf.chain.lower()][0].block_number()
print ("block_number: {}".format(block_number))
for dex in web3dex.all[conf.chain.lower()]:
if dex.platform in conf.dexes.split(','):
input = conf.input if conf.input is not None else dex.base_address
output = conf.output if conf.output is not None else dex.base_address
if dex.exist(input, output):
value = dex_read(dex, input, output)
if value['liquidity_in'] > conf.min_base_liquidity and value['price'] != 0 and value['liquidity_out'] > conf.min_base_liquidity * value['price']:
values[dex.platform] = value
intermediate = None
if conf.routing == "*":
intermediate = dex.token
elif conf.routing is not None:
intermediate = conf.routing
if intermediate is not None and dex.exist(input, output, intermediate):
value = dex_read(dex, input, output, intermediate)
if value['liquidity_in'] > conf.min_base_liquidity and value['price'] != 0 and value['liquidity_out'] > conf.min_base_liquidity * value['price']:
values[dex.platform + "_dexcoin"] = value
res = []
for k, v in values.items():
for kk, vv in values.items():
gap = (v['reserve_ratio'] - vv['reserve_ratio']) / v['reserve_ratio']
if gap !=0 and gap > conf.min_gap:
now = datetime.datetime.now()
out = {
"timestamp": calendar.timegm(now.utctimetuple()),
'gap': gap
}
dex0 = dict(map(lambda kv: ('dex0_' + kv[0], kv[1]), v.items()))
dex1 = dict(map(lambda kv: ('dex1_' + kv[0], kv[1]), vv.items()))
out = {**out, **dex0}
out = {**out, **dex1}
list_ = out.values()
text = ', '.join([str(x) for x in list_])
print(text)
appendcsv(conf.csv_file, out)
res.append(out)
return res
def dex_read(dex, input, output, intermediate = None):
return {
'platform': dex.platform + ('' if intermediate is None else '_dexcoin'),
'price': dex.price(input, output, intermediate),
'reserve_ratio': dex.reserve_ratio(input, output, intermediate, refresh = True),
'liquidity_in': dex.liquidity_in(input, output, intermediate),
'liquidity_out': dex.liquidity_out(input, output, intermediate)
}
def appendcsv(file, dict):
exist = os.path.exists(file)
file = open(file, 'a')
if not exist:
header = ', '.join([str(x) for x in dict.keys()])
file.write(header + '\r\n')
item = ', '.join([str(x) for x in dict.values()])
file.write(item + '\r\n')
file.close()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(looper())