-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgk.sh
More file actions
executable file
·1200 lines (1016 loc) · 33.3 KB
/
Copy pathcgk.sh
File metadata and controls
executable file
·1200 lines (1016 loc) · 33.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# cgk.sh -- coingecko.com api access
# v0.18.6 jun/2024 by mountaineerbr
# requires jq and curl/wget
#defaults
#from currency, defaults=bitcoin
DEFFROMCUR=bitcoin
#vs currency, defaults=usd
DEFTOCUR=usd
#scale, defaults=16
SCLDEFAULTS=16
# cUrl/Wget
# Timeout (seconds)
TOUT=18
# User agent
UAG='user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
#troy ounce to gram ratio
TOZ=31.1034768
#set number format
export LC_NUMERIC=en_US.UTF-8
#script name
SN="${0##*/}"
#cache directory
#defaults=/tmp/cgk.sh.cache
CACHEDIR="${TMPDIR:-/tmp}/$SN".cache
## Manual and help
HELP_LINES="NAME
Cgk.sh -- Currency Converter and Market Stats
Coingecko.com API Access
SYNOPSIS
cgk.sh [-gox] [-sNUM] [AMOUNT] FROM_CURRENCY [VS_CURRENCY]
cgk.sh -d CRYPTO
cgk.sh -ee [-pNUM]
cgk.sh -HH CRYPTO [VS_CURRENCY]
cgk.sh -t CRYPTO [VS_CURRENCY]
cgk.sh -tt [-pNUM] [CRYPTO]
cgk.sh -mm [VS_CURRENCY]
cgk.sh [-hluv]
DESCRIPTION
This programme fetches updated crypto and bank currency rates
from CoinGecko.com and can convert any amount of one supported
currency into another.
FROM_CURRENCY is a (crypto)currency or metal symbol. For crypto-
currencies, coingecko IDs can be used, too. VS_CURRENCY is a
crypto, fiat or metal symbol and defaults to ${DEFTOCUR,,}.
List supported currency symbols and IDs with option -l. About
53 bank currencies (fiat) are supporterd, as well as gold and
silver. If FROM or VS_CURRENCY is set to any of .=- , it will
mean the same as bitcoin (and sets scale to 8 in some cases).
Central bank currency conversions are supported indirectly by
locally calculating them with the cost of one extra call to the
API. As CoinGecko updates rates frequently (about once a minute),
it is one of the best APIs for bank currency rates, too.
Use option -g to convert precious metal rates using grams instead
of troy ounces (${TOZ} grams / troy ounce). Use option -x to
use satoshi unit instead of a bitcoin unit (with BTC pairs only).
Default precision is ${SCLDEFAULTS} and can be adjusted with option -s .
A shortcut is -NUM in which NUM is an integer. Option -o adds a
thousands separator (a comma) in result.
Option -t fetches general of a CRYPTO currency ticker, user may
set a VS CURRENCY (check available VS_CURRENCIES for this function
with -mm).
Option -tt retrieves tickers with CRYPTO from all supported ex-
changes (note that vs_currency is usd only). *Tip: pipe output
to grep and fetch specific market pairs.
Option -b is originally called the bank currency function. It is
now invoked automatically when needed, however setting it explic-
itly when appropriate may improve speed. It was first implemented
because of coingecko api restrictions. For example, it does not
offer rates for conversion between bank currencies and metals,
neither for some cryptocurrency pairs not officially supported
by any exchange, so we calculate those rate slocally.
Set option -a to disable automatic conversion of symbols to
coingecko IDs. Some coins have got same symbols, for example
Peercoin and Phillips Pay Coin, both PPC. Use ''peercoin\`\`
or \`\`philips-pay-coin\`\`, check IDs with option -l.
By defaults, the script will keep a local cache of the available
currency symbols and ids from CoinGecko. That will avoid flooding
the server with requests of mostly static data and will improve
script speed. To update cache files, set -c. Note that directory
/tmp is cleared on every boot, cache directory=$CACHEDIR .
Coingecko.com api rate limit is currently 100 requests/minute.
ABBREVIATIONS
Results of some functions may have abbreviations.
ATH All time high
ATHDelta Difference between ATH and current price
ATL All time low
ATLDelta Difference between ATL and current price
CapTotal Total market cap
CapCh24H Market cap change_in the last 24h
Ch, chg Change
Delta Difference
EX Exchange name
EXID Exchange identifier
FDilutdV Fully diluted valuation
INC? Incentives for trading?
MCapRank Market capital rank
Mkt, mk Market
NORM Normalised
R Rate
Rnk Rank, trust rank
ROI Return on investiment
SCORE Trust score
Vol Volume
For more information, such as normal and normalized volume,
check <https://blog.coingecko.com/trust-score/>.
MARKET CAP FUNCTION 24-H ROLLING STATS -m
Some currency convertion data is available for use with the Market
Cap Function -m. You can choose which currency to display data,
when available.
Check a list of vs_currencies with option -mm. Otherwise, the
market capitulation table will display data in various currencies
in some fields by defaults.
WARRANTY
Licensed under the GNU Public License v3 or better. It is dis-
tributed without support or bug corrections. This programme needs
Bash, cURL or Wget, JQ, Coreutils and Gzip to work properly.
CoinGecko updates rates at about once per minute.
It is _not_ advisable to depend solely on CoinGecko rates for
serious trading.
If you found this useful, consider sending feedback! =)
USAGE EXAMPLES
(1) One Bitcoin in US Dollar:
$ cgk.sh bitcoin
$ cgk.sh 1 bitcoin usd
(2) 100 ZCash in Digibyte with 8 decimal plates:
$ cgk.sh -b -s8 100 zcash digibyte
$ cgk.sh -b -8 100 zec dgb
(3) One thousand Brazilian Real in US Dollar with
3 decimal plates and using math expression in
AMOUNT:
$ cgk.sh -b -4 '101+(2*24.5)+850' brl usd
(4) One gram of gold in US Dollars:
$ cgk.sh -g xau usd
(5) Simple ticker for Ethereum against Japanese Yen:
$ cgk.sh -t eth jpy
(6) Tickers of any Ethereum pair from all exchanges:
$ cgk.sh -tt eth
Tip: pipe to less with opion -S (--chop-long-lines)
or 'most' pager for scrolling horizontally:
$ cgk.sh -tt eth | less -S
(7) Market cap function, show data for Chinese CNY:
$ cgk.sh -m cny
(8) Ten thousand satoshis to N. America USD;
use satoshi unit, no decimal plates:
$ cgk.sh -x 10000 bitcoin usd
same as:
$ cgk.sh -0 .00010000 bitcoin usd
OPTIONS
Formatting
-NUM Same as -sNUM.
-g Use grams instead of troy ounces (precious metals only).
-o Print thousands separator in results (comma).
-s NUM Scale setting (decimal plates); defaults=${SCLDEFAULTS}.
-x Use Satoshi instead of Bitcoin (sat = 1/100,000,000 btc);
this affects input AMOUNT and output.
Miscellaneous
-a Disable automatic conversion of symbols to CoinGecko ids.
-c Update cache data from CoinGecko (currecy symbols and ids).
-h Show this help.
-j Debug; print raw data, usually json.
-l List supported currencies.
-mm List supported VS_CURRENCIES for options -m and -t .
-p NUM Pages to retrieve (max 100 results/page); use with
options -ett; defaults=auto.
-v Print script version.
Functions
-b Bank currency function, force convertions between
unofficially supported currency pairs; defaults=auto.
-d CRYPTO Dominance of cryptos; a single crypto is optional
(amongst top 10 only); also check option -m.
-e Exchange information; set number of pages with -p.
-ee Print a list of exchange names and IDs only.
-H CRYPTO [VS_CURRENCY]
Historical prices (time series); twice to print CSV.
-m [VS_CURRENCY]
Market ticker; a vs_currency may be supplied; check
option -mm; defaults=USD+others.
-t CRYPTO [VS_CURRENCY]
Simple ticker; general informatioin of CRYPTO.
-tt [CRYPTO]
All tickers with CRYPTO from all exchanges."
#some urls
COINLISTURL1=https://api.coingecko.com/api/v3/coins/list
COINLISTURL2=https://api.coingecko.com/api/v3/simple/supported_vs_currencies
#fiat codes (bank fiat + metals)
FIATCODES=(
btc eth ltc bch bnb eos xrp xlm link dot yfi
usd aed ars aud bdt bhd bmd brl cad chf clp
cny czk dkk eur gbp hkd huf idr ils inr jpy
krw kwd lkr mmk mxn myr ngn nok nzd php pkr
pln rub sar sek sgd thb try twd uah vef vnd
zar xdr xag xau bits sats
) #61
#removed crypto: btc eth ltc bch bnb eos xrp xlm, link dot bits sats
FIATCODESSTRICT=(
usd aed ars aud bdt bhd bmd brl cad chf clp
cny czk dkk eur gbp hkd huf idr ils inr jpy
krw kwd lkr mmk mxn myr ngn nok nzd php pkr
pln rub sar sek sgd thb try twd uah vef vnd
zar xdr xag xau
) #48
## Functions
#cache files
#usage cachef [URL] [TMPFILE]
cachef()
{
local url tmpfile
url="$1" tmpfile="$2"
if [[ ! -s "$tmpfile" || "$OPTC" -gt 0 ]]
then "${YOURAPP[@]}" "$url" >"$tmpfile" #create or update cache
fi
}
## -m Market Cap function
## -d dominance opt
mcapf()
{
#scale
SCL="${SCL:-$SCLDEFAULTS}"
#discard AMOUNT in $1
(( $1 )) 2>/dev/null && shift
#we don't use $2
if [[ -n "$2" ]] && [[ "${2,,}" != usd ]]
then echo "Warning: discarding -- $2" >&2
fi
# Check if input has a defined vs_currency
if [[ -z "$1" ]]
then NOARG=1 ;set -- "${DEFTOCUR,,}"
fi
# Get Data
CGKGLOBAL="$CACHEDIR/cgkglobal.json"
"${YOURAPP3[@]}" "https://api.coingecko.com/api/v3/global" -H "accept: application/json" >"$CGKGLOBAL"
VS_CUR=( $( jq -r '.data.total_market_cap|keys[]' "$CGKGLOBAL" ) )
# Print JSON?
if (( PJSON && (DOMOPT || MCAP > 1) ))
then cat -- "$CGKGLOBAL" ;exit
fi
#option -mm
if (( MCAP > 1 ))
then
echo 'Vs_currencies for option -m'
echo "${VS_CUR[@]^^}"
exit
fi
# Check if input is a valid vs_currency for this function
if [[ \ "${VS_CUR[*],,}"\ != *\ "${1,,}"\ * ]]
then
printf "Not available -- %s\n" "${1^^}" 1>&2
NOARG=1
set -- usd
fi
#-d only dominance?
if (( DOMOPT ))
then
if [[ -n "$1" ]] &&
DOM="$( jq -e ".data.market_cap_percentage.${1,,}//empty" "$CGKGLOBAL" )"
then
printf "%.${SCL}f %%\n" "${DOM}"
else
{
jq -r '.data.market_cap_percentage|to_entries[] | [.key, .value] | @tsv' "$CGKGLOBAL" |
awk '{ printf "%s\t%.4f%%\n", toupper($1) , $2 }'
totale=( $( jq -r '.data.market_cap_percentage[]' "$CGKGLOBAL" ) )
printf 'Sum:\t%.4f%%\n' "$( bc <<< "scale=16; ( ${totale[@]/%/+} 0 ) / 1" )"
jq -r '(100-(.data.market_cap_percentage|add))' "$CGKGLOBAL" |
awk '{ printf "Oth:\t%.4f%%\n", $1 }'
} | column -t -N'Symbol,Dominance' -R'Dominance'
fi
exit
fi
#get more data
MARKETGLOBAL="$CACHEDIR/mktglobal.json"
DEFIGLOBAL="$CACHEDIR/defiglobal.json"
"${YOURAPP3[@]}" "https://api.coingecko.com/api/v3/coins/markets?vs_currency=${1,,}&order=market_cap_desc&per_page=10&page=1&sparkline=false" >"$MARKETGLOBAL"
"${YOURAPP3[@]}" "https://api.coingecko.com/api/v3/global/decentralized_finance_defi" >"$DEFIGLOBAL"
# Print JSON?
if (( PJSON ))
then cat -- "$CGKGLOBAL" "$MARKETGLOBAL" "$DEFIGLOBAL" ;exit
fi
#timestamp
CGKTIME="$( jq -r '.data.updated_at' "$CGKGLOBAL" )"
# Avoid erros being printed
{
cat <<-!
## CRYPTO MARKET STATS
$( date -d@"$CGKTIME" "+## %FT%T%Z" )
## Markets_: $( jq -r '.data.markets' "$CGKGLOBAL" )
## Cryptos_: $( jq -r '.data.active_cryptocurrencies' "$CGKGLOBAL" )
## ICOs Stats
# Upcoming: $( jq -r '.data.upcoming_icos' "$CGKGLOBAL" )
# Ongoing_: $( jq -r '.data.ongoing_icos' "$CGKGLOBAL" )
# Ended___: $( jq -r '.data.ended_icos' "$CGKGLOBAL" )
!
echo -e "\n## Dominance (Top 10)"
{
jq -r '.data.market_cap_percentage | keys_unsorted[] as $k | "\($k) \(.[$k])"' "$CGKGLOBAL" | awk '{ printf " # %s____:=%.4f%%\n", toupper($1), $2 }'
jq -r '(100-(.data.market_cap_percentage|add))' "$CGKGLOBAL" | awk '{ printf " # Others_:=%.4f%%\n", $1 }'
} | column -t -d -s= -NA,B -RB
echo -e "\n## Total Market Cap"
echo -e " # Equivalent in"
printf " %s____: %'22.2f\n" "${1^^}" "$( jq -r ".data.total_market_cap.${1,,}" "$CGKGLOBAL" )"
if [[ -n "${NOARG}" ]]
then
for c in eur gbp brl jpy cny xau btc eth xrp # bitcoin ethereum ripple
do printf " %s____: %'22.2f\n" "${c^^}" "$( jq -r ".data.total_market_cap.${c,,}" "$CGKGLOBAL" )"
done
fi
printf " # Change(%%USD/24h): %.4f %%\n" "$( jq -r '.data.market_cap_change_percentage_24h_usd' "$CGKGLOBAL" )"
echo -e "\n## Market Cap per Coin"
jq -r '.[]|"\(.symbol) \(.market_cap) \(.market_cap_change_percentage_24h)"' "$MARKETGLOBAL" |
awk '{ printf " # %s=%'"'"'.2f=%.4f%%\n", toupper($1) , $2 , $3 }' |
column -t -s'=' -N"# SYMBOL,CAP(${1^^}),CHANGE(24h)" -R"CAP(${1^^}),CHANGE(24h)"
echo -e "\n## Market Volume (Last 24H)"
echo " # Equivalent in"
printf " %s_____ %'22.2f\n" "${1^^}" "$( jq -r ".data.total_volume.${1,,}" "$CGKGLOBAL" )"
if [[ -n "${NOARG}" ]]
then
for c in eur gbp brl jpy cny xau btc eth xrp # bitcoin ethereum ripple
do printf " %s____: %'22.2f\n" "${c^^}" "$( jq -r ".data.total_volume.${c,,}" "$CGKGLOBAL" )"
done
fi
echo -e "\n## Market Volume per Coin (Last 24H)"
jq -r '.[]|"\(.symbol) \(.total_volume) '${1^^}' \(.market_cap_change_percentage_24h)"' "$MARKETGLOBAL" |
awk '{ printf " # %s=%'"'"'.2f %s=%.4f%%\n", toupper($1) , $2 , $3 , $4 }' |
column -t -s"=" -N'# SYMBOL,VOLUME,CHANGE' -R'VOLUME,CHANGE'
echo -e "\n## Supply and All Time High"
jq -r '.[]|"\(.symbol) \(.circulating_supply) \(.total_supply)"' "$MARKETGLOBAL" |
awk '{ printf " # %s=%'"'"'.2f=%'"'"'.2f\n", toupper($1) , $2 , $3 }' |
column -t -s"=" -N' # SYMBOL,CIRCULATING_SUPPLY,TOTAL_SUPPLY' -R'CIRCULATING_SUPPLY,TOTAL_SUPPLY'
echo -e "\n## Price Stats (${1^^})"
jq -r '.[]|"\(.symbol) \(.high_24h) \(.low_24h) \(.price_change_24h) \(.price_change_percentage_24h)"' "$MARKETGLOBAL" |
awk '{ printf " # %s=%s=%s=%s=%.4f%%\n", toupper($1) , $2 , $3 , $4 , $5 }' |
column -t -s"=" -N" # SYMBOL,HIGH(24h),LOW(24h),CHANGE,CHANGE%" -R"HIGH(24h),LOW(24h),CHANGE,CHANGE%"
echo -e "\n## All Time Highs (${1^^})"
jq -r '.[]|"\(.symbol) \(.ath) \(.ath_change_percentage) \(.ath_date)"' "$MARKETGLOBAL" |
awk '{ printf " # %s=%s=%.4f%%= %s\n", toupper($1) , $2 , $3 , $4 }' |
column -t -s'=' -N' # SYMBOL,PRICE,CHANGE,DATE' -R'PRICE,CHANGE,DATE'
echo -e "\n## Defi Market"
#defi markets
jq -r '.data |
"## MktCap__: \(.defi_market_cap | .[0:18])",
"## EthMktCp: \(.eth_market_cap | .[0:18])",
" # Defi/Eth: \(.defi_to_eth_ratio | .[0:10])",
" # Dominanc: \(.defi_dominance | .[0:6])",
" # Vol24h__: \(.trading_volume_24h | .[0:18])",
" # TopCoin_: \(.top_coin_name)",
" # TopCDom_: \(.top_coin_defi_dominance | tostring | .[0:6])"' "$DEFIGLOBAL"
# Avoid erros being printed
} 2>/dev/null
}
#warning message for more columns
warnf()
{
echo 'OBS: more columns optionally required' >&2
}
## -e Show Exchange info function
exf()
{
# -ee Show Exchange list
if ((EXOPT > 1))
then
ELIST="$CACHEDIR/elist.json"
"${YOURAPP[@]}" "https://api.coingecko.com/api/v3/exchanges/list" >"$ELIST"
# Print JSON?
if [[ -n ${PJSON} ]]
then cat -- "${ELIST}" ;exit
fi
jq -r '.[]|"\(.id)=\(.name)"' "$ELIST" | column -et -s'=' -N"EXCHANGE_ID,EXCHANGE_NAME"
printf 'Exchanges: %s\n' "$(jq -r '.[] | .id? //empty' "$ELIST" | wc -l)"
exit
fi
# Test screen width
# if stdout is redirected; skip this
if [[ -t 1 ]]
then
if [[ "$(tput cols)" -lt 85 ]]
then COLCONF=( -HINC?,COUNTRY,EX -TEXID ) ;warnf
elif [[ "$(tput cols)" -lt 115 ]]
then COLCONF=( -HINC?,EX -TCOUNTRY,EXID ) ;warnf
else COLCONF=( -TCOUNTRY,EXID,EX )
fi
fi
#Get pages with exchange info
# Print JSON?
if (( PJSON ))
then "${YOURAPP[@]}" "https://api.coingecko.com/api/v3/exchanges?page=1" ;exit
fi
echo 'Table of Exchanges'
head="$( "${YOURAPP2[@]}" "https://api.coingecko.com/api/v3/exchanges" 2>&1 | grep -Fie "total:" -e "per-page:" | sort -r )"
total="$( grep -Fi total <<< "$head"| grep -o '[0-9]*' )"
ppage="$( grep -Fi page <<< "$head"| grep -o '[0-9]*' )"
(( TPAGES )) || {
TPAGES="$(( (total / ppage) + 1 ))"
(( total % ppage )) || (( TPAGES-- ))
}
echo "$head"
echo 'Columns: Rank,Score,ExchangeID,Vol24hBTC,NormalisedVol,Incentives?,Year,Country,ExchangeName' >&2
for ((i=TPAGES;i>0;i--))
do
#feedback to stderr
printf '>>p%s/%s\r' "${i}" "${TPAGES}" >&2
#get data and make table
"${YOURAPP[@]}" "https://api.coingecko.com/api/v3/exchanges?page=${i}" |
jq -r 'reverse[] | "\(if .trust_score_rank == null then "??" else .trust_score_rank end)=\(if .trust_score == null then "??" else .trust_score end)=\(.id)=[\(.trade_volume_24h_btc)]=\(.trade_volume_24h_btc_normalized)=\(if .has_trading_incentive == true then "yes" else "no" end)=\(if .year_established == null then "??" else .year_established end)=\(if .country != null then .country else "??" end)=\(.name)"' |
column -et -s'=' -N"RNK,SCORE,EXID,VOL24H_BTC,NORM_VOL,INC?,YEAR,COUNTRY,EX" ${COLCONF[@]}
done
# Check if CoinEgg still has a weird "en_US" in its name that havocks table
}
## Bank currency rate function
bankf()
{
local n; unset BANK FMET TMET;
#download currency lists
if ((OPTA==0))
then
#get data to check currency names and symbols
local keys=( $( jq -r ".[] | .id,.symbol" "$CGKTEMPLIST1" ) )
#check and change currency ids
if [[ \ "${keys[*]}"\ = *\ "${2,,}"\ * ]] &&
MAYBE1="$(changecf vs "$2")"
then
set -- "$1" "$MAYBE1" "$3"
elif [[ \ "${keys[*]}"\ = *\ "${3,,}"\ * ]] &&
MAYBE2="$(changecf vs "$3")"
then
set -- "$1" "$2" "$MAYBE2"
fi
fi
if [[ "${2,,}" = xa[ug] ]]
then URI="price?ids=bitcoin,${3,,},${MAYBE2}&vs_currencies=btc,${2,,},${3,,},${MAYBE2}"
else URI="price?ids=bitcoin,${2,,},${3,,},${MAYBE1},${MAYBE2}&vs_currencies=btc,${2,,},${3,,},${MAYBE1},${MAYBE2}"
fi
# Get CoinGecko JSON
CGKRATERAW="$CACHEDIR/cgkrateraw.json"
"${YOURAPP[@]}" "https://api.coingecko.com/api/v3/simple/$URI" >"$CGKRATERAW"
# Print JSON?
if (( PJSON ))
then cat -- "${CGKRATERAW}" ;exit
fi
# Get rates to from_currency anyways
if [[ "${2,,}" = btc || "${2,,}" = bitcoin ]]
then BTCBANK=1
elif ! BTCBANK="$( mainf 1 "${2,,}" btc 2>/dev/null )"
then BTCBANK="( 1 / $( mainf 1 bitcoin "${2,,}" ) )" || return 1
fi
# Get rates to vs_currency anyways
if [[ "${3,,}" = btc || "${3,,}" = bitcoin ]]
then BTCTOCUR=1
elif ! BTCTOCUR="$( mainf 1 "${3,,}" btc 2>/dev/null )"
then BTCTOCUR="( 1 / $( mainf 1 bitcoin "${3,,}" ) )" || return 1
fi
# Timestamp? No timestamp for this API
# Calculate result
# Precious metals in grams?
ozgramf "$2" "$3"
#satoshi units?
satoshif "$@" && set -- "${args[@]}"
while [[ ${RESULT:0:SCL+n+1} != *[1-9]* ]]
do RESULT=$( bc <<< "scale=16; ( ( ($1) * ${BTCBANK}) / ${BTCTOCUR}) ${GRAM} ${TOZ}" );
((++n)); ((n>16)) && break;
done; ((n>1)) && ((SCL+=n+2));
printf "%${OOPT}.*f\n" "${SCL}" "${RESULT}"
}
## -H historical prices/time series -- backup func
## NOT IN USE
#help:-H [PERIOD] [SYMBOL] [VS_CURRENCY]
# Historical prices (time series); period may be 24h
# (1d), 7d, 14d, 30d, 90d,180d, 1y and max; pass -H
# twice to print historical volumes instead of prices.
#histf() {
# #period
# case "${1:-x}" in
# 7*d*) period=7_days ;;
# 14*d*) period=14_days ;;
# 30*d*) period=30_days ;;
# 90*d*) period=90_days ;;
# 180*d*) period=180_days ;;
# *y*|365*) period=365_days ;;
# 24*h*|1*d*) period=24_hours ;;
# *) period=max ;;
# esac ;shift
# #volume or price?
# if (( OPTHIST == 1 ))
# then
# #prices
# #jq -r '.stats[] | "\(.[1])\t\( (.[0]/1000)|strflocaltime("%Y-%m-%dT%H:%M:%S%Z") )"' <<<"$DATA"
#
# to="$( date +%s )"
# if [[ "$period" = max ]]
# then from=1367107200 #2013-04-28
# else from="$( date -d "${period/_/ } ago" +%s )"
# fi
#
# DATA="$( "${YOURAPP3[@]}" "https://api.coingecko.com/api/v3/coins/${1}/market_chart/range?vs_currency=${2,,}&from=${from}&to=${to}" )"
# # Print JSON?
# if (( PJSON ))
# then echo "$DATA" ;exit
# fi
# [[ -n "$DATA" ]] || exit 1
#
# jq -r '.prices[] | "\(.[1])\t\( (.[0]/1000)|strflocaltime("%Y-%m-%dT%H:%M:%S%Z") )"' <<<"$DATA"
# else
# #volumes
# ID=( $( "${YOURAPP3[@]}" "https://www.coingecko.com/en/coins/${1}" | sed -nE '/\/[0-9]+\/.*json/ s/.*\/([0-9]+)\/.*/\1/p' ) )
# #get data
# DATA="$( "${YOURAPP3[@]}" "https://www.coingecko.com/price_charts/${ID[0]}/${2,,}/${period}.json" )"
# # Print JSON?
# if (( PJSON ))
# then echo "$DATA" ;exit
# fi
# [[ -n "$DATA" ]] || exit 1
#
# jq -r '.total_volumes[] | "\(.[1])\t\( (.[0]/1000)|strflocaltime("%Y-%m-%dT%H:%M:%S%Z") )"' <<<"$DATA"
# fi
#}
## -H historical prices/time series
histf()
{
#get data
data="$CACHEDIR/data.json"
"${YOURAPP3[@]}" "https://www.coingecko.com/price_charts/export/${2,,}/${3,,}.csv" >"$data"
#print raw CSV data?
if (( PJSON )) || (( OPTHIST > 1 ))
then cat -- "$data" ;exit
fi
#make a table
#header (columns)
COLS="$( read <"$data" ;echo "$REPLY" )"
tail -n+2 "$data" | column -ets, -N"${COLS^^}" -TSNAPPED_AT
}
#trap function
trapf()
{
#unset trap
trap \ EXIT INT TERM
if [[ -d "$CACHEDIR" ]]
then rm -f "$CACHEDIR"/*.json
fi &>/dev/null
#rmdir --ignore-fail-on-non-empty "$CACHEDIR"
}
#-t ticker simple backup func
tickersimplefb()
{
[[ "${3,,}" != usd ]] && echo "Warning: rates against USD only" >&2
#maybe#https://www.coingecko.com/en/overall_stats
#downlaod ticker data
data="$("${YOURAPP3[@]}" "https://www.coingecko.com/en/coins/${2,,}")"
# Print JSON?
if (( PJSON ))
then echo "$data" ;exit
fi
#process
sed -n '/Price and Market Stats/,/Popular coins right/ p'<<<"$data" |
if command -v w3m &>/dev/null
then w3m -dump -T text/html
elif command -v elinks &>/dev/null
then elinks -dump
elif command -v lynx &>/dev/null
then lynx -force_html -stdin -dump
else sed 's/<[^>]*>//g' | grep -v '^/' | awk NF
fi
}
#-t default simple ticker function
tickersimplef()
{
local perpage total i marketglobal data ok
perpage=600
total=10
#get data
for i in {1..${total}}
do
if ! marketglobal="$( "${YOURAPP3[@]}" "https://api.coingecko.com/api/v3/coins/markets?vs_currency=${3,,}&order=market_cap_desc&per_page=${perpage}&page=${i}&sparkline=false" )"
then break
fi
# Print JSON?
(( PJSON )) && echo "$marketglobal"
if data="$( jq -er ".[] |
select(.id == \"${2,,}\") //
select(.symbol == \"${2,,}\") //
select(.name | ascii_downcase == \"${2,,}\") //
empty" <<< "$marketglobal" 2>/dev/null )" &&
[[ -n "$data" ]]
then
ok=1 ;break
fi
done
#return on error
(( ok )) || return 1
#print ticker
<<<"$data" jq -er '. |
"Updated_: \(.last_updated)",
"MCapRank: \(.market_cap_rank)",
"ID______: \(.id)",
"Symbol__: \(.symbol)",
"Name____: \(.name)",
"CapTotal: \(.market_cap)",
"CapCh24H: \(.market_cap_change_24h // empty) \(.market_cap_change_percentage_24h)%",
"Supply__: \(.circulating_supply // empty)",
"SupTotal: \(.total_supply // empty)",
"Sup__Max: \(.max_supply // empty)",
"ATH_____: \(.ath // empty)",
"ATHDelta: \(.ath_change_percentage // empty)%",
"ATH_Date: \(.ath_date // empty)",
"ATL_____: \(.atl // empty)",
"ATLDelta: \(.atl_change_percentage // empty)",
"ATL_Date: \(.atl_date // empty)",
"ROI_____: times: \(.roi|.times // empty) cur: \(.roi|.currency) perc: \(.roi|.percentage)",
"FDilutdV: \(.fully_diluted_valuation // empty)",
"PriceNow: \(.current_price)",
"PriCh24h: \(.price_change_24h // empty) \(.price_change_percentage_24h)%",
"High_24H: \(.high_24h // empty)",
"Low__24H: \(.low_24h // empty)",
"VolTotal: \(.total_volume // empty)"' 2>/dev/null
}
## -tt Ticker Function from all exchanges
tickerf()
{
#we don't use $3
if [[ "${3,,}" != usd ]]
then echo "Warning: discarding -- $3" >&2
fi
## Trap temp cleaning functions
trap trapf EXIT
trap 'exit 2' INT TERM
# Test screen width
# if stdout is redirected; skip this
if [[ -t 1 ]]
then
if [[ "$( tput cols )" -lt 110 ]]
then COLCONF=( -HEX,L_TRADE -TVOL,MARKET,SPD% ) ;warnf
else COLCONF=( -TL_TRADE,EX,MARKET,VOL,SPD%,P_USD )
fi
fi
# Start print Heading
printf "Tickers for %s\n" "${2^^}"
#calc how many result pages
head="$( "${YOURAPP2[@]}" "https://api.coingecko.com/api/v3/coins/${2,,}/tickers" 2>&1 | grep -Fie "total:" -e "per-page:" | sort -r )"
total="$( grep -Fi total <<<"$head"| grep -o '[0-9]*' )"
ppage="$( grep -Fi page <<<"$head"| grep -o '[0-9]*' )"
(( TPAGES )) || {
TPAGES="$(( (total / ppage) + 1 ))"
(( total % ppage )) || (( TPAGES-- ))
}
echo "$head"
printf 'Columns: Market,Price,Spread%%,PriceBTC,PriceUSD,Vol,ExchangeID,ExchangeName,LastTrade\n\n' >&2
# Print JSON?
if (( PJSON ))
then
"${YOURAPP2[@]}" "https://api.coingecko.com/api/v3/coins/${2,,}/tickers"
"${YOURAPP[@]}" "https://api.coingecko.com/api/v3/coins/${2,,}/tickers?page=${i:-1}"
exit
fi
for (( i=TPAGES ;i>0 ;i--))
do
#feedback to stderr
printf '>>p%s/%s\r' "${i}" "${TPAGES}" 1>&2
#get data and make table
"${YOURAPP[@]}" "https://api.coingecko.com/api/v3/coins/${2,,}/tickers?page=${i}" |
jq -r '.tickers[]|"\(.base)/\(.target)=\(.last)=\(if .bid_ask_spread_percentage == null then "??" else .bid_ask_spread_percentage end)=\(.converted_last.btc)=\(.converted_last.usd)=\(.volume)=\(.market.identifier)=\(.market.name)=\(.last_traded_at)"' |
column -s= -et -N"MARKET,PRICE,SPD%,P_BTC,P_USD,VOL,EXID,EX,L_TRADE" ${COLCONF[@]}
#don't clog the server
((i%2)) && sleep 0.8 || sleep 1
done
}
## -l Print currency lists
listsf()
{
local COLCONF
#load currency lists
cachef "$COINLISTURL1" "$CGKTEMPLIST1" || exit
cachef "$COINLISTURL2" "$CGKTEMPLIST2" || exit
# Print JSON?
if (( PJSON ))
then cat -- "$CGKTEMPLIST1" "$CGKTEMPLIST2" ;exit
fi
#check if stdout is free
[[ -t 1 ]] && COLCONF=( -TSYMBOL,ID,NAME )
printf "List of supported FROM_CURRENCIES (cryptos)\n"
jq -r '.[]|"\(.symbol)=\(.id)=\(.name)"' "$CGKTEMPLIST1" | column -s'=' -et -N'SYMBOL,ID,NAME' ${COLCONF[@]}
printf "\nList of (officially) supported VS_CURRENCY\n"
jq -r '.[]' "$CGKTEMPLIST2" | tr "[:lower:]" "[:upper:]" | sort | column -c 60
printf '\nCriptos: %5d\n' "$( jq -r '.[].id' "$CGKTEMPLIST1" | wc -l )"
printf 'VsCurre: %5d\n' "$( jq -r '.[]' "$CGKTEMPLIST2" | wc -l )"
}
# Change currency code to ID in FROM_CURRENCY and VS_CURRENCY
changecf()
{
local vsto symbol grepid selection REPLY
symbol="${2,,}"
vsto="${1,,}"
#is a know fiat?
if [[ -n "$OPTA" || \ "${FIATCODESSTRICT[*]}"\ = *\ "$symbol"\ * ]]
then
echo "$symbol"
#try to match in currency list
elif
if [[ "$vsto" = vs ]]
then [[ ${symbol} != bitcoin ]] &&
! jq -er ".[]|select(.id == \"${symbol}\")" "$CGKTEMPLIST1" &>/dev/null &&
grepid=( $(jq -er ".[]|select(.symbol == \"${symbol}\")|.id" "$CGKTEMPLIST1") )
elif [[ \ "${FIATCODES[*]}"\ != *\ "$symbol"\ * ]]
then grepid=( $(jq -er ".[]|select(.id == \"${symbol}\")|.symbol" "$CGKTEMPLIST1") )
[[ \ "${FIATCODES[*]}"\ != *\ "${grepid[0]}"\ * ]] && unset grepid
fi
((${#grepid[@]}>1))
then
echo "Multiple ids found for \`$symbol':" >&2
select selection in "${grepid[@]}"
do [[ \ "${grepid[*]}"\ = *\ "$selection"\ * ]] && break
done
echo "${selection:-${grepid[0]}}"
elif ((${#grepid[@]}))
then
echo "${grepid[0]}"
else
echo "$symbol"
return 1
fi
return 0
}
# Precious metals in grams?
ozgramf()
{
# Precious metals - troy ounce to gram
#CGK does not support Platinum(xpt) and Palladium(xpd)
if (( GRAMOPT ))
then
[[ "${2^^}" = XA[GU] ]] && TMET=1
[[ "${1^^}" = XA[GU] ]] && FMET=1
if (( TMET - FMET ))
then
if (( TMET ))
then GRAM='*'
else GRAM='/'
fi
return 0
fi
fi
unset TOZ GRAM
return 1
}
#satoshi unit instead of bitcoin unit?
satoshif()
{
#satoshi opt?
if (( SATOPT ))
then
[[ "${3,,}" =~ ^(bitcoin|btc|bitcoin-cash|bch|bitcoin-cash-sv|bsv)$ ]] && SATTO=1
[[ "${2,,}" =~ ^(bitcoin|btc|bitcoin-cash|bch|bitcoin-cash-sv|bsv)$ ]] && SATFROM=1
if (( SATTO - SATFROM ))
then
if (( SATTO ))
then args=( "( $1 ) * 100000000" "$2" "$3" )
else args=( "( $1 ) / 100000000" "$2" "$3" )
fi
return 0
fi
fi
unset args
return 1
}
#check currencies (defaults)
curcheckf()
{
((OPTA)) && return 0
## Check VS_CURRENCY
# Make sure "XAG Silver" does not get translated to "XAG Xrpalike Gene"
#auto try the bank function, if needed
if [[ -z "$BANKSKIP$TOPT" && \ "${FIATCODESSTRICT[*]}"\ = *\ "${2,,}"\ * ]] &&
bankf "${@}"
then
exit 0
elif ! jq -er ".[] | select(.id == \"${2,,}\" or .symbol == \"${2,,}\")" "$CGKTEMPLIST1" &>/dev/null
then
printf "ERR: currency -- %s\n" "${2^^}" >&2
exit 1
fi
## Check TO_CURRENCY
if [[ \ "${FIATCODES[*]}"\ != *\ "${3,,}"\ * ]] && {
((TOPT)) || ! grep -qi "^${3}$" "$CGKTEMPLIST2"
}
then
# Bank opt needs this anyways
#auto try the bank function
if [[ -z "$BANKSKIP$TOPT" ]] &&
jq -er ".[] | select(.id == \"${2,,}\" or .symbol == \"${3,,}\")" "$CGKTEMPLIST1" &>/dev/null &&
bankf "${@}"
then
exit 0
else
printf "ERR: currency -- %s\n" "${3^^}" >&2
exit 1
fi
fi
return 0
}
#default option - cryptocurrency converter
mainf()
{
local rate n;
#if $CGKRATERAW is set, it is from bank function
if [[ -s "$CGKRATERAW" ]]
then
# Result for bank subfunction -b
if rate=$( jq -er '."'${2,,}'"."'${3,,}'" // empty' "$CGKRATERAW" | sed 's/e/*10^/g' ) &&
rate="$( bc <<< "scale=16 ; ( $1 ) * $rate / 1 " )" &&
[[ -n "${rate//[^0-9,.+-]}" ]]
then echo "$rate"
else return 1
fi
else
# Make equation and print result
rate="$( "${YOURAPP[@]}" "https://api.coingecko.com/api/v3/simple/price?ids=${2,,}&vs_currencies=${3,,}" )"
#print json?
if (( PJSON ))
then echo "$rate" ;exit
fi
rate="$( jq -r ".\"${2,,}\".\"${3,,}\"" <<< "${rate}" | sed 's/e/*10^/g' )"
# Precious metals in grams?
ozgramf "$2" "$3"
#satoshi units?
satoshif "$@" && set -- "${args[@]}"
while [[ ${RESULT:0:SCL+n+1} != *[1-9]* ]]
do RESULT=$( bc <<< "scale=16; ( ($1) * ${rate} / 1 ) ${GRAM} ${TOZ}" );
((++n)); ((n>16)) && break;
done; ((n>1)) && ((SCL+=n+2));
printf "%${OOPT}.*f\n" "${SCL}" "${RESULT}"
fi
return 0
}