-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
86 lines (75 loc) · 3.43 KB
/
popup.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//change coins list in this array
//coin's name - entry_target - quantity
const coinsList = [
'BTC',
'ETH',
'C98-3.8-27.47',
'ATOM-37.9-8.28',
'NEAR-7.5-41.4',
'CELO-5.7-14.59',
'SLRS-0.4308_2-241.92',
'LINK-47_50.9-2.14',
];
const fetchCoinAPI = async () => {
let totalInterestAmount = 0;
for (const coinNameString of coinsList) {
const coinNameComponents = coinNameString.split('-');
let [coinName, range, quantity] = coinNameComponents;
let fetchResult = await fetch(`https://min-api.cryptocompare.com/data/price?fsym=${coinName}&tsyms=USDT`);
let jsonResult = await fetchResult.json();
let currentPrice = jsonResult.USDT;
if (!currentPrice) {
fetchResult = await fetch(`https://min-api.cryptocompare.com/data/price?fsym=${coinName}&tsyms=USD`);
jsonResult = await fetchResult.json();
currentPrice = jsonResult.USD;
}
let interest = 0;
let capital = 0;
let interestElement = `<span></span>`;
if (range) {
let [entry, tp] = range.split('_');
if (!isNaN(entry)) {
capital = Number(entry) * Number(quantity);
interest = 100 * ( Number(currentPrice) - Number(entry) ) / entry;
}
range = `(${entry}${tp ? '_' + tp : ''})`
} else {
range = ''
}
if (interest != 0) {
const interestAmount = (interest * Number(capital)/100).toFixed(2);
totalInterestAmount += Number(interestAmount);
interestElement = interest > 0 ?
`<span style="color: green;">+${interest.toFixed(2)}% |${interestAmount}$</span>` :
`<span style="color: red;">${interest.toFixed(2)}% |${interestAmount}$</span>`
}
document.getElementById(`currenciesList`).innerHTML += `
<h3>
<a target="blank" href="https://www.cryptocompare.com/coins/${coinName.toLowerCase()}/overview/USDT">${coinName}${range}:</a>
<span style="color: blue;">$${currentPrice}</span> ${interestElement}
</h3>`
}
// Exchange USD to VNĐ
document.getElementById('totalInterest').innerHTML = `${totalInterestAmount.toFixed(2)}$`;
let usdtFetchResult = await fetch(`https://min-api.cryptocompare.com/data/price?fsym=USDT&tsyms=USD`);
let usdtJsonResult = await usdtFetchResult.json();
document.getElementById('USDT').innerHTML = `${usdtJsonResult.USD}$`;
let VNDcurrency = await fetchExchange();
document.getElementById('VNDPrice').innerHTML = formatMoney(VNDcurrency);
}
const fetchExchange = async () => {
let result = await fetch(`https://free.currconv.com/api/v7/convert?q=USD_VND&compact=ultra&apiKey=adeffb2a37fa8b7a98aa`); //add this to your url &compact=ultra&apiKey=${yourkey}
result = await result.json();
return result.USD_VND;
}
function formatMoney(n, c, d, t) {
var c = isNaN(c = Math.abs(c)) ? 2 : c,
d = d == undefined ? "." : d,
t = t == undefined ? "," : t,
s = n < 0 ? "-" : "",
i = String(parseInt(n = Math.abs(Number(n) || 0).toFixed(c))),
j = (j = i.length) > 3 ? j % 3 : 0;
var ret = s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "") + ' VNĐ';
return ret.replace('.00', '');
}
fetchCoinAPI();