You are not logged in.
I would like to submit, for your appreciation, some *Bash* functions to get crypto
and central bank currency rates on the terminal.
Just test and add them to your ~/.bashrc if you like.
You need the package "jq" for them to work.
Hopefully, some examples before each function is presented can give an
idea of how to use them and their syntax.
I am sorry if the code is not that great, but it has been working here.
Hope they are useful.
===> I did some bug fixes and I have more recent code,
if you want it, just let me know.
## Binance Rates
Examples:
# Get USDT equivalent of a cryptocurrency
binance eth
# To convert one currency to another, use a "valid" Binance currency pair
binance xrpusdt
binance 20 bnb usdc
binance 20 eostusd
binance() {
if ! [[ ${1} =~ [0-9] ]]; then
set -- 1 "${@:1:2}"
fi
if [[ -z ${3} ]] && [[ ${#2} -le 4 ]]; then
set -- "${@:1:2}" "USDT"
fi
local BRATE=$(curl -s https://api.binance.com/api/v1/ticker/price?symbol="${2^^}""${3^^}" | jq -r ".price")
printf "%s*%s\n" "${1}" "${BRATE}" | bc -l
}
## CoinGecko Rates
Coin Gecko also has a public API.
CoinGecko is a little harder to use because you need to specify
the id of the FROM_CURRENCY (not the usual "codes"),
whereas VS_CURRENCY does use common codes for crypto and central bank currencies.
You can see Lists of these currencies running the function with the argument "-l"
Examples:
cgk ethereum lkr
cgk bitcoin xau
cgk true-usd usd
cgk tether usd
cgk 100 litecoin xrp
EXTRA FUNCTIONS:
CURRENCY LISTS
cgk -l
MARKET CAPITAL
cgk -mc
cgk() {
# printf "cgk [amount] [currency_id] [vs_currency]"
## Get Lists of suported FROM and VS currencies with "-l"
if [[ ${*} = "-l" ]]; then
printf "\nList of supported FROM_CURRENCY IDs\n\n"
curl -s -X GET "https://api.coingecko.com/api/v3/coins/list" -H "accept: application/json" | jq -r '.[] | "\(.name) = \(.id)"' | column -s '=' -c 80 -T 1 -e -t -o '|' -N '-------FROM_CURRENCY NAME-------,---------------ID---------------'
printf "\n\n"
printf "List of supported VS_CURRENCY Codes\n\n"
curl -s -X GET "https://api.coingecko.com/api/v3/simple/supported_vs_currencies" -H "accept: application/json" | jq -r '.[]' | column -c 100
printf "\n"
return
## Get market cap dominance of the top 10 cryptos with "-mc"
elif [[ ${*} = "-mc" ]]; then
local CGKGLOBAL=$(curl -sX GET "https://api.coingecko.com/api/v3/global" -H "accept: application/json")
CGKTIME=$(printf "%s" "${CGKGLOBAL}" | jq -r '.data.updated_at')
date -d@"$CGKTIME" "+%n## %d/%b/%Y%n## %T (%Z)%n"
local LC_NUMERIC="en_US.utf8"
printf "CRYPTO MARKET CAPITAL\n"
printf "usd %s\n" "$(printf "%'.2f\n" "$(printf "%s" "${CGKGLOBAL}" | jq -r '.data.total_market_cap.usd')")"
printf "brl %s\n\n" "$(printf "%'.2f\n" "$(printf "%s" "${CGKGLOBAL}" | jq -r '.data.total_market_cap.brl')")"
printf "DOMINANCE\n"
printf "%s\n" "${CGKGLOBAL}" | jq -r '.data.market_cap_percentage | keys_unsorted[] as $k | "\($k) = \(.[$k])"' | column -s '=' -t -o "=" | awk -F"=" '{ printf "%s", $1; printf("%.4f %%\n", $2); }'
printf "\n"
return
fi
if ! [[ ${1} =~ [0-9] ]]; then
set -- 1 "${@:1:2}"
fi
if [[ -z ${3} ]]; then
set -- "${@:1:2}" "usd"
fi
local CGKRATE=$(curl -s -X GET "https://api.coingecko.com/api/v3/simple/price?ids=${2,,}&vs_currencies=${3,,}" -H "accept: application/json" | jq ".\"${2,,}\".\"${3,,}\"")
printf "%s*%s\n" "${1}" "${CGKRATE}" | bc -l
}
## Coinmarketcap Rates
Examples:
# Prices in US Dollars
cmc btc
cmc dash
cmc 1000 dash
# CMC does not convert from a central bank currency to another,
# but it does convert from crypto to ~93 central bank currencies
cmc btc cny
cmc 50 xmr brl
cmc usdt usd
I ask you to kindly sign-up for a free API key at
https://pro.coinmarketcap.com/signup/
and update the key after "X-CMC_PRO_API_KEY".
If there are too many requests beyond allowance to this test API key below,
api requests will stop working consistently or altogether.
cmc() {
if ! [[ ${1} =~ [0-9] ]]; then
set -- 1 "${@:1:2}"
fi
if [[ -z ${3} ]]; then
set -- "${@:1:2}" "USD"
fi
local CMCRATE=$(curl -s -H "X-CMC_PRO_API_KEY: f70ef502-0d91-496b-bd5b-5c0f20334720" -H "Accept: application/json" -d "&symbol=${2^^}&convert=${3^^}" -G [url]https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest[/url] | jq -c ".data[] | .quote.${3^^}.price")
# LC_NUMERIC="en_US.utf8"
printf "%s*%s\n" "${1}" "${CMCRATE}" | bc -l
}
## Rate.sx Cryptocurrency Table
Get a clean chart of top 10 crypto table & BTC market cap dominance.
Call it with "rates" in Bash.
rates() {
if [[ ${*} = :help ]]
then
curl -s "rate.sx/:help"
return
fi
local CCHARTORIG=$(curl -s rate.sx/?FT)
local CCHART=$(printf "%s\n" "$CCHARTORIG" | tail -28 | tr -d '=─│┼├┤┴└┘┌┬┐' | cut -c-70)
printf "%s\n" "${CCHART}"
printf "%s\n" "${CCHART}"
date --date "$(printf "%s\n" "${CCHART}" | tail -1)" "+#%d/%b/%y %T (%Z)"
}
If you want a colorful table, try "curl -s curl -s rate.sx/?F"
and change the function accordingly (i.e. get the sed command out)
## Rate.sx Graphs & Conversion Tool
Examples:
rate [from_currency] [to_currency] [@date]
# Check Graph of Ethereum in US Dollars
rate eth
rate eth usd
## From one crypto to another
rate ada bnb
# At a specific time (resolution is about 10 minutes)
rate tusd usdt @January
rate xmr eth @2018-10-01
(Max backwards date is 2018-01-07)
# Also check:
rate :help
or even "curl rate.sx/:help"
rate() {
if [[ -z ${*} ]]
then
set -- btc usd
elif [[ -z ${2} ]] || [[ ${2} =~ [0-9] ]]; then
set -- ${1} usd ${2}
elif [[ ${*} = :help ]]
then
curl -s "rate.sx/:help"
return
fi
printf "Rate [from_currency] [to_currency] [@date|?T|..]\n"
curl -s "${2}.rate.sx/${1}${3}"
}
## Central Bank Currencies, OpenExchange.org Rates
OpenExchangeRates is one of the best websites for central bank currency rates.
It also supports conversion from and to *some* crypto and black-market currencies.
This became a too big bash function that I turned into a proper script.
There is a somewhat long manual, you can call it running the script with the flag "--help".
Basically:
# Get USD equivalent of a given currency
openx.sh brl
openx.sh xmr
# Convert
openx.sh 100 brl jpy
openx.sh btc usd
Download latest version of script at:
https://drive.google.com/file/d/1_PMrIR … sp=sharing
I ask you to kindly sign-up for a free API key at
https://openexchangerates.org/signup
and update the key in variable "APPID" in the script source code.
If there are too many requests beyond allowance to this test API key
in the script source code, api requests will stop working consistently or altogether.
## Central Bank Currencies, Currencylayer.com Rates
Free plans should get currency updates every hour during weekdays.
It supports very few cyrpto currencies.
To get a List of all currencies it supports, run it with "-l" flag
I ask you to kindly sign-up for a free API key at
https://currencylayer.com/product
and update the key after "access_key=" parameter in the source code of this function.
If there are too many requests beyond allowance to this test API key
in the script source code, api requests will stop working consistently or altogether.
Syntax is similar to previous examples.
# Basically, to show one-US-dollar equivalent of a given currency
clay cad
clay czk
# Convert
clay 100 brl jpy
clay btc usd
clay 0.5 djf cny
# Options
# Show List of supported currencies
clay -l
# Define new scale value for decimal plates with "-s" such as
clay 0.5 djf cny -s3
#OBS: Trailing zeroes are trimmed by default.
##OBS: Coinlayer does support a few currencies, but you should be aware
that bitcoin price uses an exponential notation for ex "e-05", so bash calculator
will not recognise this number
clay() {
## List available currencies with option "-l"
if [[ ${*} = -l ]]; then
curl -s https://currencylayer.com/site_downloads/cl-currencies-table.txt | sed -e 's/<[^>]*>//g'
return
fi
if [[ -z ${2} ]]; then
set -- USD ${1^^}
fi
if ! [[ ${1} =~ [0-9] ]]; then
set -- 1 "${@:1:2}"
fi
if [[ -z ${3} ]]; then
set -- "${@:1:2}" "USD"
fi
## Decimal plates
## You can set a new value with a flag such as "-s2" for two decimal plates
## E.g.: clay 0.1 cny jpy -s2
local SCLDEFAULTS=8
if printf "%s\n" "${*}" | grep "\-\-scale=" &> /dev/null ||
printf "%s\n" "${*}" | grep "\-s" &> /dev/null; then
SCL=$(printf "%s\n" "${*}" | sed -n -e 's/.*\(-s[0-9]*\).*/\1/p' | grep -o "[0-9]*")
set -- $(printf "%s\n" "${*}" | sed -e 's/-s[0-9]*//g')
fi
if [[ -z ${SCL} ]]; then
SCL=$(printf "%s\n" "${SCLDEFAULTS}")
fi
local CLJSON=$(curl -s http://www.apilayer.net/api/live?access_key=35324a150b81290d9fb15e434ed3d264&callback=CALLBACK_FUNCTION)
if ! [[ ${2^^} = USD && ${3^^} = USD ]]; then
local FROMCURRENCY=$(printf "%s\n" "${CLJSON}" | jq ".quotes.USD${2^^}")
local TOCURRENCY=$(printf "%s\n" "${CLJSON}" | jq ".quotes.USD${3^^}")
elif [[ ${2^^} = USD ]]; then
local FROMCURRENCY=1
local TOCURRENCY=$(printf "%s\n" "${CLJSON}" | jq ".quotes.USD${3^^}")
elif [[ ${3^^} = USD ]]; then
local FROMCURRENCY=$(printf "%s\n" "${CLJSON}" | jq ".quotes.USD${2^^}")
local TOCURRENCY=1
fi
local JSONTIME=$(printf "%s\n" "${CLJSON}" | jq ".timestamp")
date -d@"$JSONTIME" "+## %d/%b/%Y%n## %T (%Z)"
## Make equation and print result
printf "define trunc(x){auto os;os=scale;for(scale=0;scale<=os;scale++)if(x==x/1){x/=1;scale=os;return x}}; scale=%s; trunc((%s*%s)/%s)\n" "${SCL}" "${1}" "${TOCURRENCY}" "${FROMCURRENCY}" | bc -lq
}
Last edited by mountaineerbr (2019-07-10 21:47:33)
Offline