-
-
Save bitstein/545c289a12316732e269a01bdc0d7566 to your computer and use it in GitHub Desktop.
Get estimated halving date from a bitcoin node
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 | |
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