forked from ccyanxyz/cycle-arb-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_router_frontrun.py
69 lines (65 loc) · 1.89 KB
/
get_router_frontrun.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
import json
from bisect import bisect
import linecache
bnidx = json.load(open('data/block_line_index.json'))
bns = [int(i) for i in bnidx.keys()]
def get_line(i):
return linecache.getline('data/cycle_include_router_with_tx_from1.json', i).strip()
def search_frontrun(start, end, txinfo):
frontrun_tx = None
s = bisect(bns, start) - 1
e = bisect(bns, end) + 1
if s >= len(bns):
sb = bns[-1]
else:
sb = bns[s]
if e >= len(bns):
eb = bns[-1]
else:
eb = bns[e]
start_line = bnidx[str(sb)]
end_line = bnidx[str(eb)]
data = txinfo['input']
for i in range(start_line, end_line):
line = get_line(i)
info = json.loads(line)
bn = int(info['receipt']['blockNumber'], 16)
if info['tx']['hash'] == txinfo['hash']:
break
if bn > end:
break
if info['tx']['input'] == data:
frontrun_tx = info
break
return frontrun_tx
'''
def search_frontrun(start, end, data):
frontrun_tx = None
with open('data/cycle_include_router_with_tx_from1.json') as f:
for line in f:
info = json.loads(line)
bn = int(info['receipt']['blockNumber'], 16)
if bn < start:
continue
if bn > end:
break
if info['tx']['input'] == data:
frontrun_tx = info
return frontrun_tx
'''
stats = {'frontrun': 0}
idx = 0
with open('data/failed_router_arbs.json') as f:
for line in f:
print(idx)
idx += 1
info = json.loads(line)
bn = int(info['blockNumber'])
f_tx = search_frontrun(bn - 20, bn, info)
if not f_tx:
continue
with open('data/frontruned_arb_info.json', 'a') as f1:
i = {'frontrun': f_tx, 'victim': info}
f1.write(json.dumps(i)+'\n')
stats['frontrun'] += 1
print(stats)