-
Notifications
You must be signed in to change notification settings - Fork 493
/
Copy pathcryptoprice.js
62 lines (51 loc) · 1.38 KB
/
cryptoprice.js
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
const fetch = require('node-fetch');
//cryptocurrencies array, prices of these cryptocurrencies will be shown
//Add more names whose price you to fetch
var cryptocurrencies=[
"ALGO",
"ATOM",
"COMP",
"DASH",
"LINK",
"LOOM",
"MANA",
"USDC",
"BAT",
"BTC",
"BCH",
"BSV",
"CVC",
"DAI",
"DNT",
"EOS",
"ETH",
"ETC",
"GNT",
"KNC",
"LTC",
"MKR",
"OMG",
"OXT",
"REP",
"XTZ",
"XLM",
"ZEC",
"ZRX"
]
var baseCurrency="INR";//Its the currency in which you want price of crypto, if you want in usd change to "USD", similarly for others
var url="https://api.coinbase.com/v2/exchange-rates?currency="+baseCurrency;
//Api get call to coinBase api using fetch in javascript
fetch(url).then(res => res.json())
.then((json) => {
var cryptoPrice=json.data.rates;//storing cryptocurrencies rate as key value json data in cryptoPrice object
for(var key in cryptoPrice ){
cryptoPrice[key]=1/cryptoPrice[key];//as the value of crytoPrice is for 1 rs we need price of cryptocurrency in terms of inr we are doing 1/(crypto price for 1INR)
}
console.log("Crypto "+" Price");
//printing name of cryptocurrency and its price
for(var key in cryptoPrice){
if(cryptocurrencies.indexOf(key)!==-1){
console.log(key+" "+" "+cryptoPrice[key]);
}
}
}).catch(err=>console.log(err));