-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcalculator.js
116 lines (96 loc) · 2.96 KB
/
calculator.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
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
const ApiUrl = 'https://api.coinmore.io';
function statsApiCall(action) {
return fetch(`${ApiUrl}${action}`).then((response) => response.json());
}
function fetchPoolProfit() {
return statsApiCall('/profit?coin=alephium');
}
function fetchRate() {
return statsApiCall(`/rate?coin=alephium`);
}
function getPoolProfitUSD(rate, profit) {
return profit * rate;
}
function perHour(value) {
return value / 24;
}
function costsPerTime(powerConsumption, electricityCosts, multiplier = 1) {
return ((powerConsumption * multiplier) / 1000) * electricityCosts;
}
function perWeek(value) {
return value * 7;
}
function addCell(td) {
return td.insertCell();
}
function addValue(tr, value, currencyValue = '', sign = '') {
tr.innerHTML =
`${sign}` + ` ${parseFloat(value).toFixed(4)}` + ` ${currencyValue}`;
}
function addRow(tbody, period, reward, income, costs, profit, currencyValue) {
let td = tbody.insertRow();
let trPeriod = td.insertCell();
trPeriod.innerHTML = period;
addValue(addCell(td), reward, 'ALPH');
addValue(addCell(td), income, currencyValue);
addValue(addCell(td), costs, currencyValue, '-');
addValue(addCell(td), profit, currencyValue);
}
function generateTable(calculatorForm) {
let button = document.getElementById('calculator_button');
button.disabled = true;
button.classList.add('disabled');
const hashrateValue = calculatorForm.hashrate.value;
const powerConsumptionValue = calculatorForm.power_consumption.value;
const currencyValue = 'USD';
const electricityCostsValue = calculatorForm.electricity_costs.value;
Promise.all([fetchRate(), fetchPoolProfit()]).then(function ([
{ rate },
{ profit },
]) {
let tbody = document.getElementsByTagName('tbody')[0];
tbody.innerHTML = '';
let reward = profit * hashrateValue;
let income = getPoolProfitUSD(rate.value, reward);
addRow(
tbody,
'1 hour',
perHour(reward),
perHour(income),
costsPerTime(powerConsumptionValue, electricityCostsValue),
perHour(income) -
costsPerTime(powerConsumptionValue, electricityCostsValue),
currencyValue,
);
addRow(
tbody,
'24 hours',
reward,
income,
costsPerTime(powerConsumptionValue, electricityCostsValue, 24),
income - costsPerTime(powerConsumptionValue, electricityCostsValue, 24),
currencyValue,
);
addRow(
tbody,
'7 days',
perWeek(reward),
perWeek(income),
costsPerTime(powerConsumptionValue, electricityCostsValue, 168),
perWeek(income) -
costsPerTime(powerConsumptionValue, electricityCostsValue, 168),
currencyValue,
);
});
button.disabled = false;
button.classList.remove('disabled');
}
const calculatorForm = document.forms.calculator_form;
calculatorForm.addEventListener('submit', function (event) {
event.preventDefault();
generateTable(calculatorForm);
});
function init(calculatorForm) {
generateTable(calculatorForm);
}
init(calculatorForm);