-
Notifications
You must be signed in to change notification settings - Fork 12
/
tmp_eta_relay_top_arbs.py
62 lines (50 loc) · 1.69 KB
/
tmp_eta_relay_top_arbs.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
import datetime
import time
from backtest.utils import connect_db
db = connect_db()
curr = db.cursor()
curr.execute(
'''
SELECT COUNT(*) FROM large_candidate_arbitrages
''',
)
(n_arbitrages,) = curr.fetchone()
print(f'Have {n_arbitrages:,} large arbitrages in total to relay')
curr.execute(
'''
CREATE TEMP TABLE tmp_count_by_block AS
SELECT block_number, count(*)::integer as cnt
FROM large_candidate_arbitrages
GROUP BY block_number;
CREATE INDEX tmp_cbb_bn_idx ON tmp_count_by_block (block_number);
'''
)
# print(f'Have {curr.rowcount:,} unique blocks to process')
last_marks = []
last_ts = []
while True:
time.sleep(5)
db.commit()
curr.execute(
'''
SELECT SUM(cnt)::integer
FROM (SELECT * FROM top_candidate_arbitrage_reservations WHERE claimed_on IS NOT NULL) tcar
JOIN tmp_count_by_block tcb ON tcar.start_block <= tcb.block_number AND tcb.block_number <= LEAST(tcar.end_block, tcar.progress)
'''
)
(n_processed,) = curr.fetchone()
last_marks.append(n_processed)
last_ts.append(time.time())
last_marks = last_marks[-500:]
last_ts = last_ts[-500:]
if len(last_marks) >= 2:
period_processed = last_marks[-1] - last_marks[0]
elapsed = last_ts[-1] - last_ts[0]
nps = period_processed / elapsed
if nps == 0:
print('n_processed', n_processed)
continue
remain = n_arbitrages - n_processed
eta_to_target = remain / nps
eta_to_target_td = datetime.timedelta(seconds=eta_to_target)
print(f'{n_processed:,} of {n_arbitrages:,} top arbs relayed ({n_processed / n_arbitrages * 100:.2f}%) ETA {eta_to_target_td}')