Skip to content

Instantly share code, notes, and snippets.

@lorepozo
Created July 18, 2017 03:44
Show Gist options
  • Save lorepozo/06fb89cdf4b2f79adc8bbf40d7d7a55c to your computer and use it in GitHub Desktop.
Save lorepozo/06fb89cdf4b2f79adc8bbf40d7d7a55c to your computer and use it in GitHub Desktop.
check your current profitability from nicehash
#!/bin/sh
# REQUIRES jq (https://stedolan.github.io/jq)
#
# This program, nh-profit, checks your current profitability from nicehash.
#
# Copyright 2017 Lucas Morales <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# defaults
addr= # bitcoin address of nicehash provider
currency=USD # of BTC, USD, GBP, EUR
pretty=1 # appends " $currency/day"
# constants
nhapi="https://api.nicehash.com/api?method=stats.provider.ex"
btcapi="http://api.coindesk.com/v1/bpi/currentprice.json"
while [ $# -gt 0 ]
do case "$1" in
-h|--help)
cat <<EOF
Usage: `basename $0` [options]
check your current daily profitability from nicehash
options:
-a, --addr <ADDR> Bitcoin address of nicehash provider.
(default: $addr)
-c, --currency <CURRENCY> Currency for output: BTC, USD, GBP, EUR.
(default: $currency)
-n, --no-pretty Displays profitability in less pretty format.
EOF
exit
;;
-a|--addr)
shift
addr=$1
shift
;;
-c|--currency)
shift
currency="$1"
shift
;;
-n|--no-pretty)
shift
pretty=0
;;
*)
>&2 echo "invalid argument: $1"
exit 1
esac
done
if ! command -v jq >/dev/null
then >&2 echo "This program requires jq: https://stedolan.github.io/jq"; exit 1
fi
# validate the currency
case "$currency" in
BTC)
format="%.8f"
btc_conv=1.0
;;
USD|GBP|EUR)
format="%.2f"
btc_conv=$(curl -LSs $btcapi | jq ".bpi.$currency.rate_float")
;;
*)
>&2 echo "invalid currency: $currency"
exit 1
esac
# get nicehash info
response=$(curl -LSs "$nhapi&addr=$addr" | jq -c '.result | {error, current}')
# nicehash error check
if echo "$response" | jq -e '.error != null' >/dev/null
then echo "$response" | jq -r .error >&2 ; exit 1
fi
# compute profitability
profitability_btc=$(echo "$response" | jq -r '
reduce (
.current | .[] |
if .data[1] != "0" and (.data[0] | has("a"))
then (.data[0].a | tonumber) * (.profitability | tonumber)
else empty
end
) as $i (0;.+$i)' )
profitability=$(jq -n "$profitability_btc * $btc_conv")
# display
if [ $pretty -eq 0 ]
then printf "$format\n" $profitability
else printf "$format $currency/day\n" $profitability
fi
@lorepozo
Copy link
Author

See lucasem/cryptocurrency-things for an up-to-date version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment