-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheth_base_fee.py
300 lines (234 loc) · 8.59 KB
/
eth_base_fee.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python
import time
import traceback
import requests
import unicornhathd
from digits import digits
import os
from dotenv import load_dotenv
load_dotenv()
url = os.getenv('ETH_EXECUTION_LAYER_URL')
headers = {'content-type': 'application/json'}
black = (0, 0, 0) # "off"
white = (255, 255, 255)
red = (255, 0, 0)
yellow = (255, 255, 0)
green = (0, 255, 0)
def get_block_number():
payload = {
"method": "eth_blockNumber",
"params": [],
"jsonrpc": "2.0",
"id": 1,
}
response = requests.post(url, json=payload, headers=headers).json()
return int(response['result'], 16)
def get_block_by_number(block_number):
payload = {
"method": "eth_getBlockByNumber",
"params": [hex(block_number), True],
"jsonrpc": "2.0",
"id": 1,
}
response = requests.post(url, json=payload, headers=headers).json()
return response['result']
def get_base_fee_for_block(block_number):
return int(get_block_by_number(block_number)['baseFeePerGas'], 16)
def display_digit(digit, offset_x):
pattern = digits[str(digit)]
for y in range(5):
for x in range(3):
if pattern[y][x]:
unicornhathd.set_pixel(x + offset_x, 15 - y, *white)
def display_block_number(block_number):
# Convert block_number to a 32-bit binary number
binary_str = format(block_number, '032b')
# Split binary string into two 16-bit parts
binary_parts = [binary_str[16:], binary_str[:16]]
for y in range(2):
for x in range(16):
if binary_parts[y][x] == '1':
unicornhathd.set_pixel(x, y, *white)
else:
unicornhathd.set_pixel(x, y, *black)
def calculate_rgb(y):
if y == 0:
r = 0
g = 0
b = 1
elif y <= 3:
r = 0
g = y / 3
b = 1 - y / 3
else:
r = 1
g = max(0, 1 - ((y - 4)/3))
b = 0
return int(r * 255), int(g * 255), int(b * 255)
def set_bar_chart_pixels(my_list):
most_recent_bar_y_vals = []
for x in range(len(my_list)):
strength = min(10, int(my_list[x]) // 10)
for y in range(10):
r, g, b = calculate_rgb(y)
if y < strength:
if x == 0:
most_recent_bar_y_vals.append((r, g, b))
unicornhathd.set_pixel(x, y, r, g, b)
for _ in range(4):
for i in range(len(most_recent_bar_y_vals)):
unicornhathd.set_pixel(0, i, *black)
unicornhathd.show()
time.sleep(0.25)
for i, color in enumerate(most_recent_bar_y_vals):
unicornhathd.set_pixel(0, i, *color)
unicornhathd.show()
time.sleep(0.25)
unicornhathd.rotation(0)
unicornhathd.brightness(0.5)
def get_most_recent_base_fees(block_number):
return [get_base_fee_for_block(block_number - i) for i in range(16)]
# h/t https://m.minecraft.novaskin.me/skin/4942713520/Fire
def display_fireball():
fireball = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 3, 0, 0],
[0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 3, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 3, 3, 0, 3, 3, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 0, 3, 0, 0, 0],
[0, 0, 0, 0, 3, 3, 3, 2, 2, 2, 3, 3, 3, 0, 0, 0],
[0, 0, 0, 0, 3, 3, 3, 2, 2, 2, 2, 3, 3, 0, 0, 0],
[0, 0, 0, 0, 3, 3, 2, 2, 2, 2, 2, 2, 3, 0, 0, 0],
[0, 0, 0, 3, 3, 2, 2, 2, 1, 2, 2, 2, 2, 3, 0, 0],
[0, 0, 3, 3, 2, 2, 2, 1, 1, 2, 2, 2, 2, 3, 0, 0],
[0, 0, 3, 3, 2, 2, 1, 1, 1, 1, 2, 2, 2, 3, 3, 0],
[0, 0, 3, 3, 2, 2, 1, 1, 1, 1, 2, 2, 2, 3, 3, 0],
[0, 0, 0, 3, 2, 2, 1, 1, 1, 1, 2, 2, 3, 3, 3, 0],
[0, 0, 0, 3, 3, 2, 2, 1, 1, 1, 2, 2, 3, 0, 0, 0],
[0, 0, 0, 0, 3, 3, 2, 1, 1, 2, 2, 3, 0, 0, 0, 0]
]
colors = {
0: (0, 0, 0), # Black (off)
1: (255, 255, 255), # Yellow
2: (255, 165, 0), # Orange
3: (255, 0, 0) # Red
}
for y in range(16):
for x in range(16):
unicornhathd.set_pixel(x, 15 - y, *colors[fireball[y][x]])
unicornhathd.show()
def display_countdown_timer():
countdown_pixel_args = [
{
0: green,
1: green,
2: green,
3: green,
4: green,
},
{
0: black
},
{
1: black
},
{
2: black,
3: yellow,
4: yellow,
},
{
3: black,
4: red,
},
]
countdown_indicator_x_pos = 0
countdown_indicator_y_max = 15
for countdown_position in countdown_pixel_args:
for y_offset, color in countdown_position.items():
y_pos = countdown_indicator_y_max - y_offset
unicornhathd.set_pixel(countdown_indicator_x_pos, y_pos, *color)
unicornhathd.show()
time.sleep(1)
if __name__ == '__main__':
display_fireball()
try:
print('Initializing base fee values...')
current_block_number = get_block_number()
print(f'Current block number: {current_block_number}')
most_recent_base_fees = get_most_recent_base_fees(current_block_number)
print(f'Most recent base fees: {most_recent_base_fees}')
base_fees_dec = [x / 10**9 for x in most_recent_base_fees]
print(f'Base fees as dec: {base_fees_dec}')
prev_block_number = None
prev_time = time.time()
while True:
block_number = get_block_number()
print(f'\nBlock number: {block_number}')
current_time = time.time()
print(f'Current time: {current_time}')
elapsed_time = current_time - prev_time
prev_time = current_time
print(f'Elapsed time between updates: {elapsed_time}')
# if we don't have a new block number (e.g. the proposer missed the slot)
if block_number == prev_block_number:
print('⚠️ No new block! ⚠️')
for i in range(5):
unicornhathd.set_pixel(0, 11 + i, 255, 0, 255)
unicornhathd.show()
time.sleep(7)
display_countdown_timer()
continue
if prev_block_number is not None and block_number != prev_block_number + 1:
print('⚠️ BLOCK SKIPPED DUE TO MISTIMING OF UPDATES ⚠️')
prev_block_number = block_number
base_fee = get_base_fee_for_block(block_number)
# base_fee = 2345 * 10**9 # <- for testing digit display
print(f'Base fee: {base_fee}')
base_fee_decimal = base_fee / 10**9
print(f'Base fee decimal: {base_fee_decimal}')
base_fees_dec = [base_fee_decimal] + base_fees_dec[:15]
# base_fees_dec = [10 * i for i in range(16)] # <- for testing the color scale
print(f'Base fees list: {base_fees_dec}')
base_fee_str = str(base_fee_decimal).split('.')[0]
print(f'Base fee string: {base_fee_str}')
unicornhathd.clear()
if len(base_fee_str) > 4:
print("!! Base fee too high for display !!")
# TODO: we probably need to clear out the area allocated for digits if this happens
else:
for i in range(len(base_fee_str)):
display_digit(int(base_fee_str[i]), 13 - (len(base_fee_str) - 1 - i) * 4)
set_bar_chart_pixels(base_fees_dec)
unicornhathd.show()
time.sleep(5)
display_countdown_timer()
# "erase" the remaining pixel of the countdown timer so that if on the next loop
# there is not yet a new block, then the digits and chart can remain pending a new block
unicornhathd.set_pixel(0, 11, *black)
unicornhathd.show()
except KeyboardInterrupt:
unicornhathd.clear()
pixels = [
(1, 15), (2, 15),
(0, 14), (1, 14), (2, 14), (3, 14),
(0, 13), (1, 13), (2, 13), (3, 13),
(1, 12), (2, 12)
]
for pixel in pixels:
unicornhathd.set_pixel(*pixel, *red)
unicornhathd.show()
except Exception:
traceback.print_exc()
unicornhathd.clear()
pixels = [
(0, 15), (4, 15),
(1, 14), (3, 14),
(2, 13),
(1, 12), (3, 12),
(0, 11), (4, 11)
]
for pixel in pixels:
unicornhathd.set_pixel(*pixel, *red)
unicornhathd.show()