-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.py
131 lines (105 loc) · 3.6 KB
/
worker.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
124
125
126
127
128
129
130
131
import time
import os
import json
import requests
from datetime import datetime
from settings import INDEXER_TIMEOUT, WORKER_ROOT, NETWORK, SHERLOCK, ETHERSCAN, TG_BOT, TG_RECEIVER
from data import pool, tokens, price, protocols, sherlock, sherx, state
INDENT = None
SORT_KEYS = False
if os.environ.get("FLASK_ENV") == "development":
INDENT = 4
SORT_KEYS = True
COOLDOWN_PERIOD = os.path.join(WORKER_ROOT, "cooldown.json")
UNSTAKE_WINDOW = os.path.join(WORKER_ROOT, "unstake.json")
POOL = os.path.join(WORKER_ROOT, "pool.json")
TOKENS = os.path.join(WORKER_ROOT, "tokens.json")
PREMIUM = os.path.join(WORKER_ROOT, "premium.json")
COVERED = os.path.join(WORKER_ROOT, "covered.json")
PRICES = os.path.join(WORKER_ROOT, "prices.json")
SHERX = os.path.join(WORKER_ROOT, "sherx.json")
STATE = os.path.join(WORKER_ROOT, "state.json")
def run():
data = sherlock.get_cooldown_period()
with open(COOLDOWN_PERIOD, "w") as f:
json.dump(data, f, indent=INDENT, sort_keys=SORT_KEYS)
data = sherlock.get_unstake_window()
with open(UNSTAKE_WINDOW, "w") as f:
json.dump(data, f, indent=INDENT, sort_keys=SORT_KEYS)
data = pool.get_staking_pool_data()
with open(POOL, "w") as f:
json.dump(data, f, indent=INDENT, sort_keys=SORT_KEYS)
data = tokens.get_tokens()
with open(TOKENS, "w") as f:
json.dump(data, f, indent=INDENT, sort_keys=SORT_KEYS)
data = protocols.get_protocols_premium()
with open(PREMIUM, "w") as f:
json.dump(data, f, indent=INDENT, sort_keys=SORT_KEYS)
# data = protocols.get_protocols_covered()
# with open(COVERED, "w") as f:
# json.dump(data, f, indent=INDENT, sort_keys=SORT_KEYS)
data = price.get_prices()
with open(PRICES, "w") as f:
json.dump(data, f, indent=INDENT, sort_keys=SORT_KEYS)
data = sherx.get_underlying()
with open(SHERX, "w") as f:
json.dump(data, f, indent=INDENT, sort_keys=SORT_KEYS)
data = state.get_state()
with open(STATE, "w") as f:
json.dump(data, f, indent=INDENT, sort_keys=SORT_KEYS)
_prev_number = 0
_prev_time = datetime.min
def verify_run():
global _prev_number
global _prev_time
if NETWORK == "LOCALHOST":
return True
if NETWORK == "KOVAN":
url = "https://api-kovan.etherscan.io/api"
if NETWORK == "MAINNET":
url = "https://api.etherscan.io/api"
payload = {
"module": "account",
"action": "txlist",
"address": SHERLOCK,
"startblock": 0,
"endblock":99999999,
"page": 1,
"offset": 1,
"sort": "desc",
"apikey": ETHERSCAN
}
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"}
try:
r = requests.get(url, params=payload, headers=headers)
b = int(r.json()["result"][0]["blockNumber"])
if b > _prev_number or (datetime.now()-_prev_time).seconds > 900:
_prev_number = b
_prev_time = datetime.now()
return True
return False
except Exception as e:
print(e)
return True
def tg_msg(msg):
url = "https://api.telegram.org/bot%s/sendMessage" % TG_BOT
data = {
"chat_id": TG_RECEIVER,
"text": msg
}
requests.post(url, json=data)
def loop():
while True:
if verify_run():
run()
time.sleep(INDEXER_TIMEOUT)
def loop_catcher():
while True:
try:
loop()
except Exception as e:
print(e)
tg_msg(str(e))
time.sleep(300)
if __name__ == "__main__":
loop_catcher()