-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontract_operations_7.py
More file actions
161 lines (118 loc) · 4.27 KB
/
Copy pathcontract_operations_7.py
File metadata and controls
161 lines (118 loc) · 4.27 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import json
import requests
import threading
import tornado.ioloop
import tornado.web
import time
decimals = 1000000000000000000
contract = "KT1PxkrCckgh5fA5v2cZEE2bX5q2RV1rv8dj"
# origination = 1734719
origination = 1734757
# level = 1735011
def fetch_url(url):
while True:
try:
fetched = json.loads(requests.get(url).text)
return fetched
except Exception as e:
print(f"{url} unreachable due to {e}, retrying")
def get_last_block():
url = "https://api.tzkt.io/v1/head"
result = fetch_url(url)
return result["level"]
def load_database():
try:
with open("database.json", "r") as infile:
previous = json.loads(infile.read())
except Exception as e:
previous = get_clear_dict()
return previous
def save_data(data):
with open("database.json", "w") as outfile:
outfile.write(json.dumps(data))
print("file saved")
def merge_save(output_dict):
print("Saving...")
input_dict = load_database()
merged_data = {**input_dict["data"], **output_dict["data"]}
merged_stats = output_dict["stats"]
merged = {"data": merged_data, "stats": merged_stats}
print(f"Total of {len(merged_data)} data entries")
save_data(merged)
def get_clear_dict(dict_in={}):
dict_in.clear()
dict_out = {"data": {},
"stats": {}
}
return dict_out
def get_last_saved_block():
previous_data = load_database()
try:
last_saved = previous_data["stats"]["last_block"]
except Exception as e:
last_saved = origination
return last_saved
def run():
start = get_last_saved_block()
end = get_last_block()
for block in range(start, end + 1):
print(f"Processing block {block}")
url = f"https://api.tzkt.io/v1/operations/transactions?sender={contract}" \
f"&entrypoint.in=burn,mint" \
f"&status=applied" \
f"&level={block}"
operations = fetch_url(url)
for_output = get_clear_dict()
for operation in operations:
print(f"Processing operation {operation}")
value_readable = int(operation["parameter"]["value"]["value"]) / decimals
print(f'{operation["initiator"]["address"]} applied '
f'{operation["parameter"]["entrypoint"]} of '
f'{operation["parameter"]["value"]["value"]}'
f', which is {value_readable}')
for_output["stats"]["last_block"] = operation["level"]
for_output["data"][operation["hash"]] = {"initiator": operation["initiator"]["address"],
"operation": operation["parameter"]["entrypoint"],
"value": operation["parameter"]["value"]["value"],
"level": operation["level"],
"timestamp": operation["timestamp"],
}
if operations:
merge_save(for_output)
class RawHandler(tornado.web.RequestHandler):
def get(self):
self.write(load_database())
def make_app():
return tornado.web.Application([
(r"/raw", RawHandler),
(r"/", ReadHandler),
(r"/static/(.*)", tornado.web.StaticFileHandler, {"path": "static"}),
])
class ReadHandler(tornado.web.RequestHandler):
def get(self):
database = load_database()
self.render("dashboard.html",
title="Mints and Burns readable API",
data=database["data"],
decimals=decimals)
class ThreadedClient(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while True:
try:
run()
run_interval = 360
print(f"Sleeping for {run_interval / 60} minutes")
time.sleep(run_interval)
except Exception as e:
print(f"Error: {e}")
raise
if __name__ == "__main__":
background = ThreadedClient()
background.start()
print("Background process started")
app = make_app()
app.listen(9878)
print("Main process starting")
tornado.ioloop.IOLoop.current().start()