-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_tzstats.py
73 lines (67 loc) · 2.63 KB
/
data_tzstats.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
import json
import pandas as pd
import requests
from tqdm import tqdm
import time
def smart_fetch(uri):
while True:
start = time.time()
try:
result = requests.get(uri)
if (result.status_code // 100 == 4): # 4xx we treat as permanent
print("Error " + str(result.status_code) +
" while fetching " + uri)
return None
return result
except Exception as e:
print("Error while fetching " + uri +
"; Retrying indefinitely...\n" + str(e))
# let's not get throttled lol :PPPP
remaining = 30 - (time.time() - start)
if (remaining > 0):
time.sleep(remaining)
def get_tzstats_block(level):
url = f"https://api.tzstats.com/explorer/block/{level}"
r = smart_fetch(url)
return r.json()
def cycle_start_and_end_level(cycle_index):
url = f"https://api.tzkt.io/v1/cycles/{cycle_index}"
r = smart_fetch(url)
json_obj = r.json()
return json_obj['firstLevel'], json_obj['lastLevel']
def get_block_data(start_level, stop_level):
lst_cycle = []
lst_level = []
lst_priority = []
lst_validations = []
lst_fees = []
lst_n_tx = []
lst_n_ops_applied = []
lst_n_ops_failed = []
lst_gas_used = []
lst_timestamp = []
for level in tqdm(range(start_level, stop_level + 1)):
tzstats_block = get_tzstats_block(level)
lst_cycle.append(tzstats_block['cycle'])
assert level == tzstats_block['height']
lst_level.append(tzstats_block['height'])
lst_priority.append(tzstats_block['round'])
lst_validations.append(tzstats_block['n_endorsed_slots'])
lst_fees.append(tzstats_block['fee'])
lst_n_tx.append(tzstats_block['n_tx'])
lst_n_ops_applied.append(tzstats_block['n_ops_applied'])
lst_n_ops_failed.append(tzstats_block['n_ops_failed'])
lst_gas_used.append(tzstats_block['gas_used'])
lst_timestamp.append(tzstats_block['time'])
data = {'Cycle': lst_cycle, 'Level': lst_level,
'Priority': lst_priority, 'Validations': lst_validations,
'Fees': lst_fees, 'Transactions': lst_n_tx,
'Ops_Applied': lst_n_ops_applied, 'Ops_Failed': lst_n_ops_failed,
'Gas_Used': lst_gas_used, 'Block_Timestamp': lst_timestamp}
return data
if __name__ == '__main__':
start_level, stop_level = cycle_start_and_end_level(cycle_index=0)
block_data = get_block_data(start_level, stop_level)
data_df = pd.DataFrame(block_data)
path = f"tzstats_block_{start_level}_{stop_level}.csv"
data_df.to_csv(path, index=False)