Skip to content

Instantly share code, notes, and snippets.

@knbknb
Last active May 25, 2022 12:31
Show Gist options
  • Save knbknb/bfaddb3db3e17e49df4f7908b0df67fe to your computer and use it in GitHub Desktop.
Save knbknb/bfaddb3db3e17e49df4f7908b0df67fe to your computer and use it in GitHub Desktop.
virtual_currencies.sh - bash script to get financial metrics of 10 most popular cryptocurrencies
#!/bin/sh
# https://stackoverflow.com/questions/48100901/convert-a-string-to-a-number-using-jq
#infile=cryptocur_top10.json
infile=$(mktemp /tmp/cryptocurr-$(date +"%Y-%m-%d_%T_XXXXXX")).json
export COINMARKETCAP_API_KEY=$([ -f ~/.env ] && cat ~/.env | grep COINMARKETCAP_API_KEY | cut -d '=' -f2)
# get current top-10 data from coinmarketcap
curl -sLo "$infile" \
-H "X-CMC_PRO_API_KEY: $COINMARKETCAP_API_KEY" \
-H "Accept: application/json" \
"https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?convert=EUR&limit=10" \
# jq: parse JSON,
# awk: format floats,
# xargs: lpad,
# perl: add header
< "$infile" jq -r '.data[]| [.cmc_rank, .symbol, .quote.EUR.percent_change_1h, .quote.EUR.percent_change_1h, .quote.EUR.percent_change_7d, .quote.EUR.price, .quote.EUR.market_cap] | @tsv' \
| awk '{ $3=sprintf("%.2f",$3); $4=sprintf("%.2f",$4); $5=sprintf("%.2f",$5); $6=sprintf("%.3f",$6); $7=sprintf("%.3f",$7/(1*10^9)) }1' \
| xargs printf '%-2s %-5s %7s %7s %7s %10s %8s\n' \
| perl -pE 'BEGIN {say qq(rk sym pctch_1h pctch_24h pctch_7d price_eur mt_cap_eur);} $_'
## optional
## grab symbols and currency-names from json file
< $infile jq -r '.data[]| [.symbol + " ", .name] | @tsv'
exit
## same as
cat <<END
BTC Bitcoin
ETH Ethereum
USDT Tether
BNB BNB
USDC USD Coin
XRP XRP
SOL Solana
ADA Cardano
LUNA Terra
AVAX Avalanche
END
rm $infile
@knbknb
Copy link
Author

knbknb commented Aug 16, 2018

Creates text output that will look like this:

rk sym pctch_1h pctch_24h pctch_7d price_eur mt_cap_eur
1  BTC      0.79   -1.04   -0.34   5651.116   97.274
2  ETH      0.55   -1.08  -19.21    257.864   26.138
3  XRP      0.61   -0.55  -14.61      0.258   10.177
4  BCH      1.42    0.39  -11.89    467.796    8.091
5  EOS      0.33   -1.08  -18.14      4.146    3.758
6  XLM      1.78   -3.01    3.42      0.193    3.620
7  LTC     -0.61   -3.05   -12.0     49.122    2.843
8  ADA      0.96   -1.94   -20.7      0.085    2.202
9  USDT     0.15    0.01   -0.12      0.879    2.116
10 XMR      0.34   -1.25    -8.5     80.026    1.306
"BTC": "Bitcoin"
"ETH": "Ethereum"
"XRP": "Ripple"
"BCH": "Bitcoin Cash"
"ADA": "Cardano"
"XLM": "Stellar"
"LTC": "Litecoin"
"XEM": "NEM"
"NEO": "NEO"
"EOS": "EOS"
"USDT": "???"

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