Using Web API to fetch recommended transaction fee

Bitcoin Core provides recommendations as well, but I’m doing this as a “proof of concept” for an improvement in Counterwallet or other Web applications.

NOTE 1: in Oct 2017, 21.co changed their name so please use the new domain name and API endpoint.
NOTE 2: Bitcoin Core fee estimates may differ (be higher or lower)

There are several APIs such as https://bitcoinfees.21.co/api.

API

Endpoint: https://bitcoinfees.21.co/api/v1/fees/recommended

Example response: { "fastestFee": 40, "halfHourFee": 20, "hourFee": 10 }

  • fastestFee: The lowest fee (in satoshis per byte) that will currently result in the fastest transaction confirmations (usually 0 to 1 block delay).
  • halfHourFee: The lowest fee (in satoshis per byte) that will confirm transactions within half an hour (with 90% probability).
  • hourFee: The lowest fee (in satoshis per byte) that will confirm transactions within an hour (with 90% probability).

Example in Bash

This is ugly, but it works. Bash floats suck!

#!/bin/bash
echo "Sample of recent transaction sizes in Counterparty:"
echo " source: blockchain.info, for Counterparty v9.55"
echo "Send: 264 bytes"
echo "Issuance: 424 bytes"
echo "DEx order: 404 bytes"
echo "Broadcast: 283 bytes (size dependent)"

read -p "What is your transaction size in BYTES (e.g. 430 for asset issuance)? " size
read -p "What's your target time (ASAP (1 block)=1, 30min=2, 60min=3)? " target
echo ""
echo "Fetching information from HTTPS endpoint..."
echo ""
if test $target -eq 1; then
  asapFee=`curl -s https://bitcoinfees.21.co/api/v1/fees/recommended | jq '.fastestFee'`
  reco=$((asapFee*size))
  recoBTC=`bc -l <<< $reco/100000000`
  echo "Reco for 10 min confirmation time for "$size" bytes is " $reco " satoshi or in BTC:"
  echo $recoBTC
  exit 0
elif test $target -eq 2; then
  halfHourFee=`curl -s https://bitcoinfees.21.co/api/v1/fees/recommended | jq '.halfHourFee'`
  reco=$((halfHourFee*size))
  recoBTC=`bc -l <<< $reco/100000000`
  echo "Reco for 30 min confirmation time for "$size" bytes is " $reco " satoshi or in BTC:"
  echo $recoBTC
else test $target -eq 3
  hourFee=`curl -s https://bitcoinfees.21.co/api/v1/fees/recommended | jq '.hourFee'`
  reco=$((hourFee*size))
  recoBTC=`bc -l <<< $reco/100000000`
  echo "Reco for 60 min confirmation time for "$size" bytes is " $reco " satoshi or in BTC:"
  echo $recoBTC
fi

Example Output

$ ./fee-test.sh
Sample of recent transaction sizes in Counterparty:
 source: blockchain.info, for Counterparty v9.55
Send: 264 bytes
Issuance: 424 bytes
DEx order: 404 bytes
Broadcast: 283 bytes (size dependent)
What is your transaction size in BYTES (e.g. 430 for asset issuance)? 444
What's your target time (ASAP (1 block)=1, 30min=2, 60min=3)? 1

Fetching information from HTTPS endpoint...

Reco for 10 min confirmation time for 444 bytes is  31080  satoshi or in BTC:
.00031080000000000000

$ ./fee-test.sh
<snip> 
What is your transaction size in BYTES (e.g. 430 for asset issuance)? 444
What's your target time (ASAP (1 block)=1, 30min=2, 60min=3)? 2

Fetching information from HTTPS endpoint...

Reco for 30 min confirmation time for 444 bytes is  31080  satoshi or in BTC:
.00031080000000000000

$ ./fee-test.sh
<snip> 
What is your transaction size in BYTES (e.g. 430 for asset issuance)? 444
What's your target time (ASAP (1 block)=1, 30min=2, 60min=3)? 3

Fetching information from HTTPS endpoint...

Reco for 60 min confirmation time for 444 bytes is  26640  satoshi or in BTC:
.00026640000000000000

Use with counterparty-client

Create a wrapper command counterparty-client.sh for counterparty-client send with case to different tx sizes for different counterparty-client actions), in the command calculate fee and pass it to counterparty-client as --fee $FEE

Incidentally, I don’t know if`counterparty-client uses recommendations from Bitcoin Core (could be checked in the source code). If it already employs Bitcoin Core recommendations or if those are less accurate (for example, now in 0.12.1 which we use they might be, compared to the latest & greatest 0.13.1).