-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmc_calc.js
64 lines (48 loc) · 2.1 KB
/
cmc_calc.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
63
const Wreck = require('wreck');
var BTC = {};
// maths and formatting
function formatNumber (num) {
return +(Math.round(num + "e+2") + "e-2");
}
var formatThousand = function(x) { return formatNumber(x / 1e3) + "k"; };
var formatMillion = function(x) { return formatNumber(x / 1e6) + "M"; };
var formatBillion = function(x) { return formatNumber(x / 1e9) + "B"; };
var formatTrillion = function(x) { return formatNumber(x / 1e12) + "T"; };
var formatQuadrillion = function(x) { return formatNumber(x / 1e15) + "Q"; };
function formatAbbreviation(x) {
var v = Math.abs(x);
return (v >= .9995e15 ? formatQuadrillion
: v >= .9995e12 ? formatTrillion
: v >= .9995e9 ? formatBillion
: v >= .9995e6 ? formatMillion
: formatThousand)(x);
}
const getcmcdata = async function () {
const { res, payload } = await Wreck.get('https://api.coinmarketcap.com/v1/ticker/?limit=50');
var coins = JSON.parse(payload);
for(coin in coins) {
var c = coins[coin];
if(c.id == 'bitcoin') {
BTC = c;
}
// how many coins you would need to buy in order to have one one-millionth of the available coin supply
c.micro_count = c.available_supply/1000000;
// how much that would cost
c.micro_cost = c.micro_count*c.price_usd;
// the market cap this coin would have if it suddenly matched BTC's price
c.cap_if_match_btc_raw = c.market_cap_usd * BTC.price_usd/c.price_usd;
// human readable format - Trillion, Billion, Million, or k (thousand)
c.cap_if_match_btc_nice = formatAbbreviation(c.market_cap_usd * BTC.price_usd/c.price_usd);
// how much bigger this coin would be than bitcoin if one coin suddenly matched BTC's price
c.cap_if_match_btc_multiplier = (parseFloat(c.cap_if_match_btc_raw) / parseFloat(BTC.market_cap_usd)).toFixed(2) + 'x';
// how much one coin would have to be for this coin to be equal to BTC's market cap
c.flippening_price = BTC.price_usd * BTC.available_supply/c.available_supply;
}
console.dir(coins);
};
try {
getcmcdata();
}
catch (err) {
console.error(err);
}