forked from Gusarich/meridian-givers-monitoring
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
147 lines (128 loc) · 5.59 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
async function main() {
const client = new TonClient({
endpoint: await getHttpEndpoint(),
});
// Function to update a single giver's balance
async function updateGiverBalance(giver, index) {
try {
// Parse the address from the current giver
const address = Address.parse(giver);
// Fetch the balance using the parsed address
const balanceResponse = await client.callGetMethod(
address,
'get_mining_status',
[]
);
// Read the number from the balance response
let stack = balanceResponse.stack;
stack.skip(6);
const leftSolutons = stack.readNumber();
const balance =
leftSolutons * window.solutionRewards[Math.floor(index / 10)];
if (window.giversInitialBalances[index] === null) {
window.giversInitialBalances[index] = balance;
}
// Format the number with commas
const formattedBalance = Math.floor(balance / 1e9).toLocaleString();
// Update the progress bar for the current giver
const progress = document.getElementById(`giver${index + 1}`);
// Calculate the expected time to drain the balance
const timePassed = Date.now() - window.loadTime;
const drained = window.giversInitialBalances[index] - balance;
const drainingSpeed = drained / (timePassed / 1000);
const timeToDrain = balance / drainingSpeed;
function formatTimeToDrain(timeToDrain) {
if (timeToDrain === Infinity) {
return '...';
}
const seconds = Math.floor(timeToDrain % 60);
const minutes = Math.floor((timeToDrain / 60) % 60);
const hours = Math.floor((timeToDrain / (60 * 60)) % 24);
const days = Math.floor(timeToDrain / (60 * 60 * 24));
let formattedTime = '';
if (days > 0) {
formattedTime += `${days} day${days > 1 ? 's' : ''} `;
} else if (hours > 0) {
formattedTime += `${hours} hour${hours > 1 ? 's' : ''} `;
} else if (minutes > 0) {
formattedTime += `${minutes} minute${
minutes > 1 ? 's' : ''
} `;
} else if (seconds > 0) {
formattedTime += `${seconds} second${
seconds > 1 ? 's' : ''
} `;
}
return formattedTime.trim();
}
const formattedTimeToDrain = formatTimeToDrain(timeToDrain);
if (progress) {
progress.value = balance; // You may need to adjust based on your scale
const label = progress.previousElementSibling; // Assuming label is right before progress bar
if (balance >= 100000000000) {
label.textContent =
label.textContent.split('#')[0] +
'#' +
((index % 10) + 1) +
' - Balance: ' +
formattedBalance +
' MRDN, expected to drain in ' +
formatTimeToDrain(timeToDrain);
} else {
label.textContent =
label.textContent.split('#')[0] +
'#' +
((index % 10) + 1) +
' - DRAINED';
}
}
return balance; // Return the balance for use in the total
} catch (error) {
console.error('Error fetching balance for giver:', giver, error);
return 0; // Return 0 if there was an error
}
}
// Function to update the total mining progress
function updateTotalMiningProgress(total) {
const totalProgress = document.getElementById('totalMiningProgress');
if (totalProgress) {
totalProgress.value = total; // Assuming total is within the min-max range of the progress bar
{
const label =
totalProgress.previousElementSibling.previousElementSibling;
label.textContent =
'Total Givers Balance: ' +
Math.floor(total / 1e9).toLocaleString() +
' / ' +
Number(420000000).toLocaleString() +
' MRDN';
}
{
const label = totalProgress.previousElementSibling;
label.textContent =
'Available for Mining: ' +
((total / 420000000000000000) * 100).toFixed('2') +
'%';
}
}
}
// Infinite loop to update balances
while (true) {
// Create a promise for each giver's balance update
const updatePromises = givers.map(updateGiverBalance);
// Wait for all updates to complete and sum the balances
const balances = await Promise.all(updatePromises);
const totalBalance = balances.reduce(
(acc, balance) => acc + balance,
0
);
// Update the total mining progress bar
updateTotalMiningProgress(totalBalance);
// Wait for 10 seconds before starting the next iteration
await new Promise((resolve) => setTimeout(resolve, 5000));
}
}
// Call the function to start the update process
document.addEventListener('DOMContentLoaded', (event) => {
main();
});