citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

btc-blockfees (1516B)


      1 #!/usr/bin/env bash
      2 
      3 # required:
      4 
      5 set -e
      6 
      7 BITCOIN_RPCUSER=${BITCOIN_RPCUSER:-rpcuser}
      8 BITCOIN_RPCPASS=${BITCOIN_RPCPASS:-rpcpass}
      9 BITCOIN_RPCPORT=${BITCOIN_RPCPORT:-8332}
     10 
     11 calc_block_subsidy() {
     12     local halvings=$(($1 / 210000))
     13     if [ $halvings -gt 63 ]; then
     14         printf "0\n"
     15     fi
     16 
     17     local subsidy=$((50 * 100000000))
     18     printf "%s\n" $((subsidy >> halvings));
     19 }
     20 
     21 mkreq () {
     22   (
     23     printf '['
     24     echo ${1#","}
     25     printf ']'
     26   ) > /tmp/req.txt
     27 }
     28 
     29 doreq() {
     30   mkreq "$1"
     31   curl -s -u $BITCOIN_RPCUSER:$BITCOIN_RPCPASS \
     32     --data-binary @/tmp/req.txt -H 'content-type: text/plain;' "http://127.0.0.1:${BITCOIN_RPCPORT}"
     33 }
     34 
     35 nblocks=${1:-100}
     36 count=$(btc blocks)
     37 
     38 heights=$(seq $((count - $nblocks + 1)) $count)
     39 # heights=$(seq 10 20)
     40 
     41 blockhash_reqs=$(
     42     <<<"$heights" xargs printf ',{"jsonrpc": "1.0", "id":"blockfees", "method": "getblockhash", "params": [%d] }\n'
     43 )
     44 
     45 txid_reqs=$(
     46   doreq "$blockhash_reqs" \
     47       | jq -rc '.[].result' \
     48       | xargs printf ',{"jsonrpc": "1.0", "id":"blockfees", "method": "getblock", "params": ["%s"] }\n'
     49 )
     50 
     51 tx_reqs=$(
     52     doreq "$txid_reqs" \
     53         | jq -rc '.[].result.tx[0]' \
     54         | xargs printf ',{"jsonrpc": "1.0", "id":"blockfees", "method": "getrawtransaction", "params": ["%s", 1] }\n'
     55 )
     56 
     57 vals=$(doreq "$tx_reqs" | jq -rc '.[].result.vout[0].value')
     58 
     59 paste -d, <(cat <<<"$heights") <(cat <<<"$vals") | \
     60 while IFS=, read -r height val
     61 do
     62     subsidy=$(calc_block_subsidy $height)
     63     printf '%s-(%s/100000000)\n' "$val" "$subsidy"
     64 done | bc -l