Skip to content

Instantly share code, notes, and snippets.

@mutatrum
Last active January 26, 2021 00:02
Show Gist options
  • Save mutatrum/ad19dbce6c2bec29570176d768a595f1 to your computer and use it in GitHub Desktop.
Save mutatrum/ad19dbce6c2bec29570176d768a595f1 to your computer and use it in GitHub Desktop.
Get estimated halving date from a bitcoin node
#!/bin/bash
CURRENT_BLOCK="$(bitcoin-cli getblockcount)"
CURRENT_BLOCK_HASH="$(bitcoin-cli getblockhash $CURRENT_BLOCK)"
CURRENT_BLOCK_TIME="$(bitcoin-cli getblock $CURRENT_BLOCK_HASH | jq '.mediantime')"
HALVING_BLOCK="$(((($CURRENT_BLOCK/210000)+1)*210000))"
BLOCK_DELTA="$(($HALVING_BLOCK-$CURRENT_BLOCK))"
PREVIOUS_BLOCK="$(($CURRENT_BLOCK-$BLOCK_DELTA))"
PREVIOUS_BLOCK_HASH="$(bitcoin-cli getblockhash $PREVIOUS_BLOCK)"
PREVIOUS_BLOCK_TIME="$(bitcoin-cli getblock $PREVIOUS_BLOCK_HASH | jq '.mediantime')"
DELTA_TIME="$(($CURRENT_BLOCK_TIME-$PREVIOUS_BLOCK_TIME))"
HALVING_TIME="$(($CURRENT_BLOCK_TIME+$DELTA_TIME))"
echo "$(date -d @$HALVING_TIME +%F)"
@mutatrum
Copy link
Author

Change last line to
echo "$(date -d @$HALVING_TIME --iso-8601=seconds)"
to get the time in ISO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment