Skip to content

Instantly share code, notes, and snippets.

@mutatrum
Last active June 29, 2021 11:27
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 mutatrum/2a71c8e852b37a549c69c7027ac6ac98 to your computer and use it in GitHub Desktop.
Save mutatrum/2a71c8e852b37a549c69c7027ac6ac98 to your computer and use it in GitHub Desktop.
#!/bin/bash
BLOCKCHAININFO=$(bitcoin-cli getblockchaininfo)
CURRENTHEIGHT=$(echo $BLOCKCHAININFO | jq -r .blocks)
CURRENTTIME=$(echo $BLOCKCHAININFO | jq -r .mediantime)
BLOCKS=$((CURRENTHEIGHT%2016))
STARTHEIGHT=$((CURRENTHEIGHT-BLOCKS))
STARTBLOCK=$(bitcoin-cli getblock $(bitcoin-cli getblockhash $STARTHEIGHT))
STARTTIME=$(echo $STARTBLOCK | jq -r .mediantime)
AVERAGETIME=$(((CURRENTTIME-STARTTIME)/BLOCKS))
PERCENT=$(awk "BEGIN {print -100+(60000/$AVERAGETIME)}")
REMAININGBLOCKS=$((2016-BLOCKS))
ENDTIME=$((CURRENTTIME+(AVERAGETIME*REMAININGBLOCKS)))
DATE=$(date -d @$ENDTIME +'%Y-%m-%d %H:%M')
echo Difficulty adjustment: $PERCENT% on $DATE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment