-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript2.js
More file actions
515 lines (441 loc) · 14.1 KB
/
javascript2.js
File metadata and controls
515 lines (441 loc) · 14.1 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
import {
disconnectWallet,
borrowAsset,
repayAsset,
withdrawETH,
stakeETH,
spinner,
ETH_PRICE,
debtPrice,
sendAsset,
} from "./wallet.js";
// FOR HEALTH FACTOR
// Hide all first
// const safe = document.getElementById("safeStatus");
// const warning = document.getElementById("warningStatus");
// const trigger = document.getElementById("liquidateStatus");
const liquidation = document.getElementById("liquidationModal");
const closeLiquidation = document.getElementById("closeLiquidationModal");
// export function updateHealthStatus(healthFactor) {
// // Show appropriate one
// if (healthFactor >= 2) {
// warning.classList.add("hidden");
// trigger.classList.add("hidden");
// safe.classList.remove("hidden");
// } else if (healthFactor >= 1 && healthFactor < 2) {
// safe.classList.add("hidden");
// warning.classList.remove("hidden");
// } else {
// safe.classList.add("hidden");
// warning.classList.add("hidden");
// trigger.classList.remove("hidden");
// liquidation.classList.remove("hidden");
// liquidation.classList.add("flex");
// }
// }
closeLiquidation.addEventListener("click", () => {
liquidation.classList.add("hidden");
liquidation.classList.remove("flex");
});
//FOR MODULAR1
const openBtn1 = document.querySelectorAll(".Modaled1");
const closeBnt1 = document.getElementById("closeModal1");
const modals1 = document.getElementById("modalOverlay1");
openBtn1.forEach((bnt) => {
bnt.addEventListener("click", () => {
modals1.classList.remove("hidden");
modals1.classList.add("flex");
liquidation.classList.add("hidden");
liquidation.classList.remove("flex");
});
});
closeBnt1.addEventListener("click", () => {
modals1.classList.add("hidden");
modals1.classList.remove("flex");
});
modals1.addEventListener("click", (e) => {
if (e.target === modals1) {
modals1.classList.add("hidden");
modals1.classList.remove("flex");
}
});
//FOR MODULAR2
const openBtn2 = document.querySelectorAll(".Modaled2");
const closeBnt2 = document.getElementById("closeModal2");
const modals2 = document.getElementById("modalOverlay2");
openBtn2.forEach((bnt) => {
bnt.addEventListener("click", () => {
modals2.classList.remove("hidden");
modals2.classList.add("flex");
});
});
closeBnt2.addEventListener("click", () => {
modals2.classList.add("hidden");
modals2.classList.remove("flex");
});
modals2.addEventListener("click", (e) => {
if (e.target === modals2) {
modals2.classList.add("hidden");
modals2.classList.remove("flex");
}
});
//FOR MODULAR3
const openBtn3 = document.querySelectorAll(".Modaled3");
const closeBnt3 = document.getElementById("closeModal3");
const modals3 = document.getElementById("modalOverlay3");
openBtn3.forEach((bnt) => {
bnt.addEventListener("click", () => {
modals3.classList.remove("hidden");
modals3.classList.add("flex");
liquidation.classList.add("hidden");
liquidation.classList.remove("flex");
});
});
closeBnt3.addEventListener("click", () => {
modals3.classList.add("hidden");
modals3.classList.remove("flex");
});
modals3.addEventListener("click", (e) => {
if (e.target === modals3) {
modals3.classList.add("hidden");
modals3.classList.remove("flex");
}
});
//FOR MODULAR4
const openBtn4 = document.querySelectorAll(".Modaled4");
const closeBnt4 = document.getElementById("closeModal4");
const modals4 = document.getElementById("modalOverlay4");
openBtn4.forEach((bnt) => {
bnt.addEventListener("click", () => {
modals4.classList.remove("hidden");
modals4.classList.add("flex");
});
});
closeBnt4.addEventListener("click", () => {
modals4.classList.add("hidden");
modals4.classList.remove("flex");
});
modals4.addEventListener("click", (e) => {
if (e.target === modals4) {
modals4.classList.add("hidden");
modals4.classList.remove("flex");
}
});
//FOR MODULAR5
// SELECT ELEMENTS
const openBtn5 = document.querySelectorAll(".Modaled5");
const modal5 = document.getElementById("modalOverlay5");
const closeBtn5 = document.getElementById("closeModal5");
// OPEN MODAL
openBtn5.forEach((btn) => {
btn.addEventListener("click", () => {
modal5.classList.remove("hidden");
modal5.classList.add("flex"); // needed for center alignment
});
});
// CLOSE MODAL
closeBtn5.addEventListener("click", () => {
modal5.classList.add("hidden");
});
//FOR MODULAR6
// SELECT ELEMENTS
const openBtn6 = document.querySelectorAll(".Modaled6");
const modal6 = document.getElementById("modalOverlay6");
const closeBtn6 = document.getElementById("closeModal6");
// OPEN MODAL
openBtn6.forEach((btn) => {
btn.addEventListener("click", () => {
modal6.classList.remove("hidden");
modal6.classList.add("flex");
});
});
// CLOSE MODAL
closeBtn6.addEventListener("click", () => {
modal6.classList.add("hidden");
});
//SEND DROP DOWN MENU FOR DESKTOP
document.addEventListener("DOMContentLoaded", () => {
const walletModal = document.getElementById("modalOverlay5");
const sendModal = document.getElementById("sendModal");
const activityModal = document.getElementById("activityModal");
const sendBtn = document.querySelector(".send_btns");
const activityBtn = document.querySelector(".activity_btns");
const backBtns = document.getElementById("backToWallet");
const backActivityBtn = document.getElementById("backToWallet2");
const closeWallet = document.getElementById("closeModal5");
const closeSend = document.getElementById("closeSendModal");
const closeActivity = document.getElementById("closeActivityModal");
// OPEN SEND MODAL
sendBtn.addEventListener("click", () => {
walletModal.classList.add("hidden");
sendModal.classList.remove("hidden");
sendModal.classList.add("flex"); // IMPORTANT (because you used items-center)
});
// OPEN ACTIVITY MODAL
activityBtn.addEventListener("click", () => {
walletModal.classList.add("hidden");
activityModal.classList.remove("hidden");
activityModal.classList.add("flex");
});
// BACK TO WALLET FROM ACTIVITY
backActivityBtn.addEventListener("click", () => {
activityModal.classList.add("hidden");
walletModal.classList.remove("hidden");
walletModal.classList.add("flex");
});
// BACK TO WALLET
backBtns.addEventListener("click", () => {
sendModal.classList.add("hidden");
walletModal.classList.remove("hidden");
walletModal.classList.add("flex");
});
// CLOSE ACTIVITY MODAL
closeActivity.addEventListener("click", () => {
activityModal.classList.add("hidden");
});
// CLOSE BUTTONS
closeWallet.addEventListener("click", () => {
walletModal.classList.add("hidden");
});
closeSend.addEventListener("click", () => {
sendModal.classList.add("hidden");
});
});
//SEND DROP DOWN MENU FOR DESKTOP
document.addEventListener("DOMContentLoaded", () => {
const walletModals = document.getElementById("modalOverlay6");
const sendModals = document.getElementById("sendModal1");
const sendBtns = document.querySelector(".send_btns1");
const backBtns = document.getElementById("backToWallet1");
const closeWallets = document.getElementById("closeModal6");
const closeSends = document.getElementById("closeSendModal1");
// OPEN SEND MODAL
sendBtns.addEventListener("click", () => {
walletModals.classList.add("hidden");
sendModals.classList.remove("hidden");
sendModals.classList.add("flex"); // IMPORTANT (because you used items-center)
});
// BACK TO WALLET
backBtns.addEventListener("click", () => {
sendModals.classList.add("hidden");
walletModals.classList.remove("hidden");
walletModals.classList.add("flex");
});
// CLOSE BUTTONS
closeWallets.addEventListener("click", () => {
walletModals.classList.add("hidden");
});
closeSends.addEventListener("click", () => {
sendModals.classList.add("hidden");
});
});
// WALLET DISCONNECT
document
.getElementById("USER_DISCONNET")
.addEventListener("click", async () => {
await disconnectWallet();
if (window.location.pathname.endsWith("page1.html")) {
location.href = "index.html";
}
console.log("Permissions revoked");
location.href = "index.html";
});
document
.getElementById("disconnectMobile")
.addEventListener("click", async () => {
await disconnectWallet();
if (window.location.pathname.endsWith("page1.html")) {
location.href = "index.html";
}
console.log("Permissions revoked");
location.href = "index.html";
});
// Stake, Borrow and Repay and withdraw
const stake = document.getElementById("connectWalletBtn1");
if (stake) {
document.getElementById("connectWalletBtn1").onclick = async () => {
const eth = document.getElementById("stakeInput").value;
// console.log(getuserAddress());
if (!eth || isNaN(eth) || eth == 0) {
stake.textContent = "Invalid Input";
return;
}
stake.disabled = true; // Disable button to prevent multiple clicks
try {
stake.innerHTML = `
${spinner}
<span>Staking...</span>
`;
await stakeETH(eth);
stake.textContent = "✓ Success";
setTimeout(() => {
modals1.classList.add("hidden");
modals1.classList.remove("flex");
}, 1500);
} catch (err) {
stake.innerHTML = "Failed";
} finally {
setTimeout(() => {
stake.disabled = false;
stake.innerHTML = "Stake";
}, 1500);
}
};
}
const borrow = document.getElementById("connectWalletBtn2");
borrow.onclick = async () => {
const amt = document.getElementById("borrowInput").value;
const selectedSymbol = document.getElementById("tokenSelect").value;
console.log("Borrowing", amt, selectedSymbol);
if (!amt || isNaN(amt) || amt == 0) {
borrow.textContent = "Invalid Input";
return;
}
// this.disabled = true;
try {
console.log("Borrowing asset...");
borrow.innerHTML = `
${spinner}
<span>Borrowing...</span>
`;
await borrowAsset(selectedSymbol, amt);
borrow.disabled = false;
borrow.textContent = "✓ Success";
setTimeout(() => {
modals2.classList.add("hidden");
modals2.classList.remove("flex");
}, 1500);
} catch (err) {
borrow.disabled = false;
borrow.innerHTML = err.shortMessage || "Failed";
} finally {
setTimeout(() => {
borrow.disabled = false;
borrow.innerHTML = "Borrow";
}, 1500);
}
};
const repay = document.getElementById("connectWalletBtn3");
repay.onclick = async () => {
const amt = document.getElementById("repayInput").value;
const selectedSymbol = document.getElementById("tokenSelect2").value;
console.log("Repaying", amt, selectedSymbol);
if (!amt || isNaN(amt) || amt == 0) {
repay.textContent = "Invalid Input";
return;
}
repay.disabled = true;
try {
repay.innerHTML = `
${spinner}
<span>Repaying...</span>
`;
await repayAsset(selectedSymbol, amt);
repay.textContent = "✓ Success";
setTimeout(() => {
modals3.classList.add("hidden");
modals3.classList.remove("flex");
}, 1500);
} catch (err) {
repay.innerHTML = "Failed";
} finally {
setTimeout(() => {
repay.disabled = false;
repay.innerHTML = "Repay";
}, 1500);
}
};
const withdraw = document.getElementById("connectWalletBtn4");
if (withdraw) {
document.getElementById("connectWalletBtn4").onclick = async () => {
const eth = document.getElementById("withdrawInput").value;
// console.log(getuserAddress());
if (!eth || isNaN(eth) || eth == 0) {
withdraw.textContent = "Invalid Input";
return;
}
withdraw.disabled = true; // Disable button to prevent multiple clicks
try {
withdraw.innerHTML = `
${spinner}
<span>Withdrawing...</span>
`;
await withdrawETH(eth);
withdraw.textContent = "✓ Sucess";
setTimeout(() => {
modals4.classList.add("hidden");
modals4.classList.remove("flex");
}, 1500);
} catch (err) {
withdraw.textContent = "Failed";
// alert("Withdrawal failed: " + err.message);
} finally {
setTimeout(() => {
withdraw.disabled = false;
withdraw.innerHTML = "Withdraw";
}, 1500);
}
};
}
const sendToken = document.querySelector(".confirmSend");
if (sendToken) {
sendToken.onclick = async () => {
const recipient = document.getElementById("recipientAddress").value;
const amount = document.getElementById("sendAmount").value;
const selectedSymbol = document.getElementById("sendTokenSelect").value;
if (!recipient || !amount || isNaN(amount) || amount == 0) {
sendToken.textContent = "Invalid Input";
return;
}
// console.log("Sending", amount, selectedSymbol, "to", recipient);
sendToken.disabled = true;
try {
sendToken.innerHTML = `
${spinner}
<span>Sending...</span>
`;
await sendAsset(selectedSymbol, recipient, amount);
sendToken.textContent = "✓ Success";
} catch (err) {
sendToken.innerHTML = "Failed";
} finally {
setTimeout(() => {
sendToken.disabled = false;
sendToken.innerHTML = "Confirm Send";
}, 1500);
}
};
}
// const ETH_PRICE = 3200;
const ethInputs = document.querySelectorAll(".ethInput");
const usdOutputs = document.querySelectorAll(".usdOutput");
const debtUsdOutputs = document.querySelectorAll(".repayUsdValue");
const debtInputs = document.querySelectorAll(".borrowInput");
const repayInput = document.getElementById("repayInput");
ethInputs.forEach((input, index) => {
input.addEventListener("input", () => {
const eth = Number(input.value);
if (!eth) {
usdOutputs[index].textContent = "$0";
return;
}
const usd = eth * ETH_PRICE;
usdOutputs[index].textContent = usd.toFixed(2) + " USD";
});
});
debtInputs.forEach((input, index) => {
input.addEventListener("input", () => {
const debtAmount = Number(input.value);
const maxrepay = Number(repayInput.value);
if (!debtAmount || !debtPrice) {
debtUsdOutputs[index].textContent = "$0";
return;
}
if (maxrepay) {
const debtUsd = maxrepay * debtPrice;
debtUsdOutputs[index].textContent = debtUsd.toFixed(2) + " USD";
}
const debtUsd = debtAmount * debtPrice;
debtUsdOutputs[index].textContent = debtUsd.toFixed(2) + " USD";
});
});