Skip to content

Instantly share code, notes, and snippets.

@locnnil
Created April 14, 2024 21:16
Show Gist options
  • Save locnnil/f73057c3ea1833dd9de60c2a708c81e7 to your computer and use it in GitHub Desktop.
Save locnnil/f73057c3ea1833dd9de60c2a708c81e7 to your computer and use it in GitHub Desktop.
BTC Halving monitor - A simple bash script to tell how many blocks rest for the next halving
#!/usr/bin/env bash
declare -A COLOR
COLOR['END']='\033[0m'
COLOR['RED']='\033[0;31m'
COLOR['GREEN']='\033[0;32m'
COLOR['YELLOW']='\033[0;33m'
COLOR['BLUE']='\033[0;34m'
COLOR['WHITE']='\033[0;37m'
function json_extract() {
local key=$1
local json=$2
local string_regex='"([^"\]|\\.)*"'
local number_regex='-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?'
local value_regex="${string_regex}|${number_regex}|true|false|null"
local pair_regex="\"${key}\"[[:space:]]*:[[:space:]]*(${value_regex})"
if [[ ${json} =~ ${pair_regex} ]]; then
echo $(sed 's/^"\|"$//g' <<< "${BASH_REMATCH[1]}")
else
return 1
fi
}
readonly period=210000
api="https://mempool.space/api/v1/mining/blocks/timestamp/"
timestamp=$(date +%s)
remain=0
next_halving=0
while true
do
clear
body=$(curl -sSL $api$timestamp)
timestamp=$(date +%s)
curret_block=$(json_extract "height" "$body")
if (( $(($curret_block%$period)) == 0 )); then
echo -e "${COLOR['GREEN']}IT's HALVING BLOCK BABY!!! ${COLOR['END']}"
remain=0
else
next_halving=$(( ($curret_block/$period)*$period + $period ))
remain=$(($next_halving-$curret_block))
fi
echo -ne "\033[H\033[J"
echo -e "The timestamp is $timestamp"
echo -e "${COLOR['YELLOW']}[INFO] The current block is ${COLOR['GREEN']}$curret_block ${COLOR['END']}"
echo -e "${COLOR['GREEN']}Halving on block $next_halving ${COLOR['END']}"
echo -e "${COLOR['BLUE']}Rest ${COLOR['RED']}$remain ${COLOR['BLUE']}blocks to Halving${COLOR['END']}"
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment