Skip to content

Instantly share code, notes, and snippets.

@bitstein
Forked from mutatrum/halving.sh
Last active May 11, 2020 15:05
Show Gist options
  • Save bitstein/545c289a12316732e269a01bdc0d7566 to your computer and use it in GitHub Desktop.
Save bitstein/545c289a12316732e269a01bdc0d7566 to your computer and use it in GitHub Desktop.
Get estimated halving date from a bitcoin node
#!/bin/bash
CURRENT_BLOCK="$(bitcoin-cli getblockcount)"
if [ $CURRENT_BLOCK -ge 630000 ]
then
SUBSIDY=$(bitcoin-cli getblockstats $CURRENT_BLOCK | jq '.subsidy')
SATS_PER_BTC=100000000
SUBSIDY_BTC=$(bc -l <<< "scale=2; $SUBSIDY / $SATS_PER_BTC")
echo "HALVING COMPLETE!"
echo "Current block: $CURRENT_BLOCK"
echo "Current subsidy: $SUBSIDY_BTC"
else
echo "Current block: $CURRENT_BLOCK"
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))"
echo "Blocks until halving: $BLOCK_DELTA"
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 "Estimated halving time: $(date -r $HALVING_TIME)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment