-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
90 lines (78 loc) · 3.04 KB
/
main.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
87
88
89
90
function modifyUpdateButton(seconds) {
const button = document.getElementById('update');
const timeoutSeconds = 180;
button.disabled = seconds < timeoutSeconds;
button.innerHTML = button.disabled ?
"Can update in " + (timeoutSeconds-seconds) + " seconds" :
'Update';
}
function showDuration(last_updated) {
let duration = luxon.Duration.fromMillis(luxon.DateTime.now() - last_updated);
let tempObject = duration.shiftTo('seconds').toObject();
tempObject.seconds = Math.round(tempObject.seconds);
modifyUpdateButton(tempObject.seconds);
duration = luxon.Duration.fromObject(tempObject);
if (duration >= luxon.Duration.fromObject({ minutes: 1 })) {
duration = duration.shiftTo('minutes', 'seconds');
}
return duration.toHuman();
}
let timeoutID = 0;
let usd_per_btc = 40000;
function updateAgo(last_updated) {
document.getElementById('date').innerHTML = showDuration(last_updated);
timeoutID = setTimeout(updateAgo, 1000, last_updated);
}
function modifyInput(id_and_store_key) {
localStorage.setItem(id_and_store_key, document.getElementById(id_and_store_key).value);
calculateView();
}
function calculateView() {
const sat_per_btc = 100000000;
const sat_per_usd = sat_per_btc / usd_per_btc;
const sat_per_hour = getInput('usd_per_hr') * sat_per_usd;
document.getElementById('sat-rate').innerHTML = Math.round(sat_per_hour / 60);
const boost_sat = getInput('boost_usd') * sat_per_usd;
document.getElementById('boost-sat').innerHTML = Math.round(boost_sat);
}
function modifyUsdRate() {
modifyInput('usd_per_hr');
}
function modifyBoostUsd() {
modifyInput('boost_usd');
}
function main() {
doFetch();
document.getElementById('update').addEventListener('click', doFetch);
document.getElementById('usd_per_hr').addEventListener('change', modifyUsdRate);
document.getElementById('boost_usd').addEventListener('change', modifyBoostUsd);
}
function getInput(id_and_store_key) {
let rval = localStorage.getItem(id_and_store_key);
if (rval == null) {
localStorage.setItem(id_and_store_key, '1.00');
rval = localStorage.getItem(id_and_store_key);
}
document.getElementById(id_and_store_key).value = rval;
return parseFloat(rval);
}
async function doFetch() {
let request = new Request('https://api.coinbase.com/v2/exchange-rates?currency=BTC');
let response = await fetch(request);
if (response.ok) {
let jsonValue = await response.json();
let last_updated = luxon.DateTime.now();
clearTimeout(timeoutID);
updateAgo(last_updated);
rate_string = jsonValue.data.rates.USD;
document.querySelector('#usd-per-btc').innerHTML = rate_string;
usd_per_btc = parseFloat(rate_string);
const sat_per_btc = 100000000;
sat_per_usd = sat_per_btc / usd_per_btc;
document.querySelector('#sat-per-usd').innerHTML = Math.round(sat_per_usd) + " SAT"
calculateView();
} else {
throw new Error(`HTTP error! status: ${response.status}`);
}
}
main();