Last active
September 10, 2021 14:17
-
-
Save pietjepuk2/5ac6a9954d57ce8ffb1e509c0b43909f to your computer and use it in GitHub Desktop.
Read base fee from Geth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
FILE_NAME = "base_fee.csv" | |
LONDON_FORK_BLOCK = 12_965_000 | |
session = requests.Session() | |
headers = {"Content-type": "application/json"} | |
# Get latest block number from Geth | |
response = session.post( | |
"http://localhost:8545", | |
json={"jsonrpc": "2.0", "method": "eth_blockNumber", "params": [], "id": 1}, | |
headers=headers, | |
) | |
end_block = int(response.json()["result"], 16) + 1 | |
# Get latest block number in output | |
rw_mode = "a" | |
try: | |
lines = open(FILE_NAME, "r").readlines() | |
start_block = int(lines[-1].split(",")[0]) + 1 | |
except (OSError, IndexError): | |
rw_mode = "w" | |
start_block = LONDON_FORK_BLOCK | |
# Get the base fee for the desired blocks, and write to file | |
params = [None, False] | |
payload = {"jsonrpc": "2.0", "method": "eth_getBlockByNumber", "params": params, "id": 1} | |
print(f"Reading blocks in the range [{start_block}, {end_block})") | |
with open(FILE_NAME, rw_mode) as o: | |
for b in range(start_block, end_block): | |
# Print progress every hundred blocks | |
if b % 100 == 0: | |
print(b, end="\r") | |
params[0] = f"0x{b:x}" | |
response = session.post("http://localhost:8545", json=payload, headers=headers) | |
base_fee = int(response.json()["result"]["baseFeePerGas"], 16) | |
o.write(f"{b},{base_fee}\n") | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment