-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
382 lines (340 loc) · 12 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Constants
var FALCONER_URL = "https://falconer.haqq.sh";
var API_DOMAIN = "https://old-haqq-stuff.vercel.app"; // Base domain for the API
// var API_DOMAIN = "http://localhost:3000"; // Base domain for the API
var REQUEST_HEADERS = {
"Content-Type": "application/json",
};
var DEFAULT_LOCALE = "en-US";
// Matches language tags as defined in RFC 5646
var regex =
/^((?:(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)|(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang))|((?:([A-Za-z]{2,3}(-(?:[A-Za-z]{3}(-[A-Za-z]{3}){0,2}))?)|[A-Za-z]{4}|[A-Za-z]{5,8})(-(?:[A-Za-z]{4}))?(-(?:[A-Za-z]{2}|[0-9]{3}))?(-(?:[A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3}))*(-(?:[0-9A-WY-Za-wy-z](-[A-Za-z0-9]{2,8})+))*(-(?:x(-[A-Za-z0-9]{1,8})+))?)|(?:x(-[A-Za-z0-9]{1,8})+))$/;
function formatNumber(
numberToFormat,
minimumFractionDigits,
maximumFractionDigits,
locale
) {
minimumFractionDigits =
minimumFractionDigits !== undefined ? minimumFractionDigits : 0; // Default value set to 0
maximumFractionDigits =
maximumFractionDigits !== undefined ? maximumFractionDigits : 3; // Default value set to 3
locale = locale || DEFAULT_LOCALE; // Default locale
// Check if locale is valid
if (!regex.test(locale)) {
console.warn(
'Invalid locale "' +
locale +
'". Falling back to default locale "' +
DEFAULT_LOCALE +
'".'
);
locale = DEFAULT_LOCALE;
}
return numberToFormat.toLocaleString(locale, {
minimumFractionDigits: minimumFractionDigits,
maximumFractionDigits: maximumFractionDigits,
});
}
// Function to get Shell Chain Stats data
function getShellChainStatsData() {
// Create request URL
var requestUrl = new URL("/haqq/chain_stats", FALCONER_URL);
return fetch(requestUrl, {
method: "GET",
headers: REQUEST_HEADERS,
})
.then(function (response) {
if (!response.ok) {
throw new Error("Chain stats fetch failed");
}
return response.json();
})
.then(function (data) {
return data; // Возвращаем весь объект
});
}
// Function to get price data
function getIslamicPriceData() {
// Create request URL
var requestUrl = new URL("/islamic/price", FALCONER_URL);
return fetch(requestUrl, {
method: "GET",
headers: REQUEST_HEADERS,
})
.then(function (response) {
if (!response.ok) {
throw new Error("Price fetch failed");
}
return response.json();
})
.then(function (data) {
return data.price;
});
}
// Function to update #price
function updatePrice() {
// Fetch price data
getIslamicPriceData()
.then(function (price) {
var priceElement = document.querySelector("[data-islm-price]");
if (priceElement) {
priceElement.textContent = formatNumber(Number(price)); // Convert price to number and format it
}
})
.catch(function (error) {
console.error("Failed to fetch price:", error);
var priceElement = document.getElementById("price");
if (priceElement) {
priceElement.textContent = "Error fetching price.";
}
});
}
// Function to update chain stats using data attributes
function updateChainStats() {
// Fetch chain stats data
getShellChainStatsData()
.then(function (chainStats) {
console.log("chainStats", { chainStats });
// Update Mainnet accounts created
var accountsElement = document.querySelector(
"[data-chain-stats-accounts]"
);
if (accountsElement) {
accountsElement.textContent = formatNumber(Number(chainStats.accounts)); // Convert accounts to number and format it
}
// Update Transactions in the last 24 hours
var transactionsElement = document.querySelector(
"[data-chain-stats-transactions]"
);
if (transactionsElement) {
transactionsElement.textContent = formatNumber(
Number(chainStats.transactionsIn24Hour) // Convert transactions to number and format it
);
}
// Update Seconds to consensus finality
var finalityElement = document.querySelector(
"[data-chain-stats-finality]"
);
if (finalityElement) {
finalityElement.textContent = formatNumber(
Number(chainStats.consensusFinality) // Convert finality to number and format it
);
}
// Update Average cost per transaction
var costElement = document.querySelector("[data-chain-stats-cost]");
if (costElement) {
costElement.textContent = formatNumber(
Number(chainStats.transactionAvgCost)
); // Convert cost to number and format it
}
// Update Active Validators
var validatorsActiveElement = document.querySelector(
"[data-chain-stats-validators-active]"
);
if (validatorsActiveElement) {
validatorsActiveElement.textContent = formatNumber(
Number(chainStats.validatorsActive)
);
}
// Update Total Supply
var totalSupplyElement = document.querySelector(
"[data-chain-stats-total-supply]"
);
if (totalSupplyElement) {
totalSupplyElement.textContent = formatNumber(
Number(chainStats.supply)
);
}
// Update Staked
var stakedElement = document.querySelector("[data-chain-stats-staked]");
if (stakedElement) {
stakedElement.textContent = formatNumber(Number(chainStats.staked));
}
// Update Supply
var supplyElement = document.querySelector("[data-chain-stats-supply]");
if (supplyElement) {
supplyElement.textContent = formatNumber(Number(chainStats.supply)); // Convert supply to number and format it
}
// Update Circulating Supply
var circulatingSupplyElement = document.querySelector(
"[data-chain-stats-circulating-supply]"
);
if (circulatingSupplyElement) {
circulatingSupplyElement.textContent = formatNumber(
Number(chainStats.circulatingSupply) // Convert circulating supply to number and format it
);
}
// Update staking amount
var stakedAmountElement = document.querySelector(
"[data-chain-stats-staked-amount]"
);
if (stakedAmountElement) {
stakedAmountElement.textContent = formatNumber(
Number(chainStats.staked) // Convert staking amount to number and format it
);
}
// Update staking ratio
var stakedRatioElement = document.querySelector(
"[data-chain-stats-staked-ratio]"
);
if (stakedRatioElement) {
stakedRatioElement.textContent = formatNumber(
Number(chainStats.stakeRatio * 100),
2,
2,
window.navigator.language
);
}
// Update Validators count
var validatorsCountElement = document.querySelector(
"[data-chain-stats-validators-count]"
);
if (validatorsCountElement) {
validatorsCountElement.textContent = formatNumber(
Number(chainStats.validatorsCount)
);
}
// Update Active validators count
var activeValidatorsCountElement = document.querySelector(
"[data-chain-stats-active-validators-count]"
);
if (activeValidatorsCountElement) {
activeValidatorsCountElement.textContent = formatNumber(
Number(chainStats.validatorsActive)
);
}
})
.catch(function (error) {
console.error("Failed to fetch chain stats:", error);
// Error handling
});
}
// Global Turnstile token
var globalTurnstileToken = "";
// Common Turnstile callback function
window.onTurnstileSuccess = function (token) {
console.log("Turnstile token received");
globalTurnstileToken = token;
// Enable submit buttons for all forms
document
.querySelectorAll('form[id^="feedback-"], form[id^="subscribe-"]')
.forEach(function (form) {
var submitButton = form.querySelector('input[type="submit"]');
if (submitButton) {
submitButton.disabled = false;
}
});
console.log("All submit buttons enabled");
};
// Initialize Feedback form
function initializeFeedbackForm() {
console.log("Initializing feedback form");
var feedbackForm = document.getElementById("feedback-form");
if (!feedbackForm) {
console.warn("Feedback form not found");
return;
}
var feedbackSubmitButton = feedbackForm.querySelector('input[type="submit"]');
if (feedbackSubmitButton) {
feedbackSubmitButton.disabled = true;
}
feedbackForm.addEventListener("submit", function (event) {
console.log("Feedback form submitted", { event });
event.preventDefault();
var formData = new FormData(feedbackForm);
console.log("Feedback form data collected");
var jsonData = {
email: formData.get("feedback-email"),
name: formData.get("feedback-name"),
message: formData.get("feedback-message"),
token: globalTurnstileToken,
};
console.log("Feedback JSON data prepared", jsonData);
var feedbackApiUrl = new URL("/api/feedback", API_DOMAIN);
console.log("Sending feedback data to:", feedbackApiUrl.toString());
fetch(feedbackApiUrl, {
method: "POST",
headers: REQUEST_HEADERS,
body: JSON.stringify(jsonData),
})
.then(function (response) {
console.log("Feedback API response received", response);
if (response.ok) {
return response.json();
}
throw new Error("Feedback form submission failed.");
})
.then(function (data) {
console.log("Feedback form successfully submitted:", data);
alert("Feedback form successfully submitted!");
})
.catch(function (error) {
console.error("Feedback form submission error:", error);
alert("Feedback form submission error.");
});
});
}
// Initialize Subscribe forms
function initializeSubscribeForms() {
console.log("Initializing subscribe forms");
var subscribeForms = document.querySelectorAll('[id^="subscribe-"]');
if (subscribeForms.length === 0) {
console.warn("No subscribe forms found");
return;
}
function handleSubscribeFormSubmit(event) {
event.preventDefault();
console.log("Subscribe form submitted", { event });
var form = event.target;
var formData = new FormData(form);
var jsonData = {
email: formData.get("subscribe-email"),
name: formData.get("subscribe-name") || "",
token: globalTurnstileToken,
};
console.log("Subscribe JSON data prepared", jsonData);
var subscribeApiUrl = new URL("/api/subscribe", API_DOMAIN);
console.log("Sending subscribe data to:", subscribeApiUrl.toString());
fetch(subscribeApiUrl, {
method: "POST",
headers: REQUEST_HEADERS,
body: JSON.stringify(jsonData),
})
.then(function (response) {
console.log("Subscribe API response received", response);
if (response.ok) {
return response.json();
}
throw new Error("Subscribe form submission failed.");
})
.then(function (data) {
console.log("Subscribe form successfully submitted:", data);
alert("Subscribe form successfully submitted!");
})
.catch(function (error) {
console.error("Subscribe form submission error:", error);
alert("Subscribe form submission error.");
});
}
subscribeForms.forEach(function (form) {
var submitButton = form.querySelector('input[type="submit"]');
if (submitButton) {
submitButton.disabled = true;
}
form.addEventListener("submit", handleSubscribeFormSubmit);
});
}
// Function to start updating price every 5 seconds
function startPriceUpdates() {
updatePrice(); // Initial update
setInterval(updatePrice, 5000); // Update every 5 seconds
}
// Single DOMContentLoaded event listener for initialization
document.addEventListener("DOMContentLoaded", function () {
// Update price data
startPriceUpdates(); // Update price data
updateChainStats(); // Update Chain Stats data
initializeFeedbackForm(); // Initialize Feedback form
initializeSubscribeForms(); // Initialize Subscribe form
});