Skip to content

Instantly share code, notes, and snippets.

@bh2smith
Last active March 1, 2024 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bh2smith/d651a29ceb8d30d5b77827a9bc72867c to your computer and use it in GitHub Desktop.
Save bh2smith/d651a29ceb8d30d5b77827a9bc72867c to your computer and use it in GitHub Desktop.
BlockTimes.sh
#!/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