Last active
March 1, 2024 11:35
-
-
Save bh2smith/d651a29ceb8d30d5b77827a9bc72867c to your computer and use it in GitHub Desktop.
BlockTimes.sh
This file contains hidden or 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
#!/bin/bash | |
# Space separated Array of Ethereum block numbers to fetch timestamps for | |
blockNumbers=(18562256 18562257) | |
ethRpc="https://rpc.ankr.com/eth" | |
for blockNumber in "${blockNumbers[@]}" | |
do | |
data="{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBlockByNumber\",\"params\":[\"0x$(printf '%x\n' $blockNumber)\", false],\"id\":1}" | |
response=$(curl -s -X POST -H "Content-Type: application/json" --data "$data" $ethRpc) | |
# convert from from hex to decimal | |
timestampHex=$(echo $response | jq -r '.result.timestamp') | |
# Remove the '0x' prefix and timestamp from hex to decimal | |
timestampDec=$((16#${timestampHex#0x})) | |
# Convert from seconds since epoch to hooman format | |
dateReadable=$(TZ=UTC date -r $timestampDec '+%Y-%m-%d %H:%M:%S') | |
echo "($blockNumber, '$dateReadable')," | |
# Sleep so the node doesn't complain. | |
sleep 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment