Repository navigation
Expand file tree
/
Copy pathwallet.js
More file actions
1077 lines (900 loc) · 30.1 KB
/
Copy pathwallet.js
File metadata and controls
1077 lines (900 loc) · 30.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
createWalletClient,
createPublicClient,
parseAbiItem,
custom,
http,
parseEther,
parseUnits,
formatEther,
formatUnits,
getContract,
maxUint256,
} from "https://esm.sh/viem";
import { setModalState, MODAL_STATE, closeModal } from "./modalController.js";
import { EXPECTED_CHAIN } from "./config.js";
import { CONTRACTS, TOKENS } from "./minilendContract.js";
import { linkinfo } from "./linkAbi.js";
import {
renderLiquidationOpportunity,
clearLiquidations,
} from "./liquidation.js";
// import { updateHealthStatus } from "./javascript2.js";
const connectHeaderBtn = document.getElementById("headerConnect");
const modalActionBtn = document.getElementById("connectWalletBtn");
const tokenMetaCache = {}; // simple in-memory cache for token metadata
const _liquidationThreshold = 0.7; // example threshold, adjust as needed
let walletClient;
let publicClient;
let userAddress = null;
let miniLend;
export let ETH_PRICE = null;
export let debtPrice = null;
let chainId = null;
// 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");
// shorten address
export function shortenAddress(addr) {
return addr.slice(0, 6) + "..." + addr.slice(-4);
}
export function getuserAddress() {
const saveduserAddress = localStorage.getItem("userAddress");
if (saveduserAddress) {
userAddress = saveduserAddress;
}
return userAddress;
}
function updateInfo(
newuserAddress,
_walletClient,
_publicClient,
_chainId,
_minilend,
) {
userAddress = newuserAddress;
walletClient = _walletClient;
publicClient = _publicClient;
chainId = _chainId;
miniLend = _minilend;
localStorage.setItem("userAddress", newuserAddress);
localStorage.setItem("connected", "true");
localStorage.setItem("walletClient", walletClient);
localStorage.setItem("publicClient", publicClient);
localStorage.setItem("chainId", chainId);
localStorage.setItem("miniLend", miniLend);
}
// testing mode: always reset
localStorage.setItem("hideMetaMaskWarning", "false");
/*//////////////////////////////////////////////////////////////
CORE CONTRACT INTERACTIONS & WALLET CONNECTION
//////////////////////////////////////////////////////////////*/
// =========== SWITCH NETWORK ============
async function switchNetwork() {
try {
await window.ethereum.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: `0x${EXPECTED_CHAIN.id.toString(16)}` }], //"0x7a69"
});
} catch (err) {
// chain not added yet
if (err.code === 4902) {
await window.ethereum.request({
method: "wallet_addEthereumChain",
params: [
{
chainId: `0x${EXPECTED_CHAIN.id.toString(16)}`,
chainName: EXPECTED_CHAIN.name,
rpcUrls: [EXPECTED_CHAIN.rpcUrl],
nativeCurrency: {
name: "Ether",
symbol: "ETH",
decimals: 18,
},
},
],
});
} else {
throw err;
}
}
}
// =========== WALLET CLIENT & PUBLIC CLIENT ============
async function connectWallet() {
try {
if (!window.ethereum) {
throw new Error("NO_WALLET");
}
miniLend = loadContract(); // initialize clients and contract
// 🔍 Check network FIRST
const chainId = await walletClient.getChainId();
if (chainId !== EXPECTED_CHAIN.id) {
throw new Error("WRONG_NETWORK");
}
const addresses = await walletClient.requestAddresses();
userAddress = addresses[0];
connectHeaderBtn.innerText = shortenAddress(userAddress);
await updateUI();
updateInfo(userAddress, walletClient, publicClient, chainId, miniLend);
return { miniLend, walletClient, userAddress };
} catch (err) {
console.error("Connection error:", err);
throw err;
}
}
// ========== LOAD MINILEND-CONTRACT ============
export function loadContract() {
walletClient = createWalletClient({
chain: EXPECTED_CHAIN,
transport: custom(window.ethereum),
});
publicClient = createPublicClient({
chain: EXPECTED_CHAIN,
transport: http(EXPECTED_CHAIN.rpcUrl),
});
return getContract({
address: CONTRACTS.sepolia.myContract.address,
abi: CONTRACTS.sepolia.myContract.abi,
client: {
public: publicClient,
wallet: walletClient,
},
});
}
// =========== TOKEN DECIMAL FETCHING WITH CACHING ============
async function getTokenDecimals(tokenAddress) {
if (tokenMetaCache[tokenAddress]?.decimals) {
return tokenMetaCache[tokenAddress].decimals;
}
const decimals = await publicClient.readContract({
address: tokenAddress,
abi: linkinfo.linkSepolia.abi, // standard ERC20 ABI with decimals function
functionName: "decimals",
});
tokenMetaCache[tokenAddress] = { decimals };
return decimals;
}
// =========== Erc20 Approve function ============
async function userApprove({
tokenAddress, // ERC20 token address
owner, // token holder address eg msg.sender
spender, // address allowed to spend tokens (eg miniLend contract)
amount, //human-readable amount (eg "1.5" for 1.5 LINK) - will be converted to base units
}) {
const amountWei = parseUnits(amount.toString());
// check allowance
const allowance = await publicClient.readContract({
address: tokenAddress,
abi: linkinfo.linkSepolia.abi, // standard ERC20 ABI with allowance function
functionName: "allowance",
args: [owner, spender],
});
if (allowance >= amountWei) return; // already approved
// prompt wallet approval
const hash = await walletClient.writeContract({
address: tokenAddress,
abi: linkinfo.linkSepolia.abi,
functionName: "approve",
args: [spender, maxUint256], // approve max to avoid repeated approvals
account: owner,
});
await publicClient.waitForTransactionReceipt({ hash });
}
// ============ txHandler ============
async function executeMiniLendTx({
functionName,
args = [],
value = undefined,
}) {
try {
if (!miniLend) {
miniLend = loadContract();
}
// await checkNetworkLimits();
console.log("Preparing transaction with params:", {
functionName,
args,
value: value ? formatEther(value) + " ETH" : "N/A",
});
const hash = await walletClient.writeContract({
address: CONTRACTS.sepolia.myContract.address,
abi: CONTRACTS.sepolia.myContract.abi,
functionName,
args,
account: userAddress,
...(value !== undefined ? { value } : {}),
});
console.log("Transaction sent:", hash);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
handleUserAction({
type: functionName,
amount: args[1]
? formatUnits(args[1], 18)
: value
? formatEther(value)
: "N/A",
hash,
}); // log activity with actual amount and hash.
if (receipt.status !== "success") {
throw new Error("Transaction failed");
}
await updateUI();
return receipt;
} catch (error) {
console.error("Transaction error:", error);
throw error;
}
}
// ============ Stake ETH function ============
export async function stakeETH(amountInETH) {
const amountWei = parseEther(amountInETH.toString());
return executeMiniLendTx({
functionName: "stakeEth",
args: [],
value: amountWei,
});
}
export async function borrowAsset(tokenSymbol, amount) {
const token = TOKENS[tokenSymbol];
if (!token) throw new Error("Unsupported token");
const tokenAddress = token.address;
// Get cached decimals
const decimals = await getTokenDecimals(tokenAddress);
const amountUnits = parseUnits(amount.toString(), decimals);
// updateUI(tokenSymbol);
return executeMiniLendTx({
functionName: "borrowAsset",
args: [tokenAddress, amountUnits],
tokenSymbol: tokenSymbol,
});
}
// =========== Repay function ============
export async function repayAsset(tokenSymbol, amount) {
const token = TOKENS[tokenSymbol];
if (!token) throw new Error("Unsupported token");
const tokenAddress = token.address;
// Get cached decimals
const decimals = await getTokenDecimals(tokenAddress);
const amountUnits = parseUnits(amount.toString(), decimals);
// ensure userAddress and publicClient are initialized
const accounts = await window.ethereum.request({
method: "eth_accounts",
});
userAddress = accounts.length > 0 ? accounts[0] : null;
console.log("Current user address:", userAddress);
// Approve tokens first
await userApprove({
tokenAddress,
owner: userAddress,
spender: CONTRACTS.sepolia.myContract.address,
amount: amount.toString(),
});
return executeMiniLendTx({
functionName: "repayAsset",
args: [tokenAddress, amountUnits],
tokenSymbol: tokenSymbol, // for UI update after tx
});
}
// ============ Withdraw ETH function ============
export async function withdrawETH(amountInETH) {
const amountWei = parseEther(amountInETH.toString());
return executeMiniLendTx({
functionName: "withdrawCollateralEth",
args: [amountWei],
});
}
// =========== Get price ============
// tokenDecimals must be passed (18 for ETH)
async function getUsdPrice(token, amount) {
// convert input amount → token base units
const amountWei = parseUnits(amount.toString());
// call contract
const usdValue = await publicClient.readContract({
address: CONTRACTS.sepolia.myContract.address,
abi: CONTRACTS.sepolia.myContract.abi,
functionName: "getUsdValue",
args: [token, amountWei],
});
// convert from 18-decimal USD
const usdNumber = Number(formatUnits(usdValue, 18));
// format with commas
const formattedUsd = usdNumber.toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
return formattedUsd;
}
// get latest price from contract (for availableToBorrow calculation)
async function getLatestPrice(token) {
// call contract
const price = await publicClient.readContract({
address: CONTRACTS.sepolia.myContract.address,
abi: CONTRACTS.sepolia.myContract.abi,
functionName: "getLatestPrice",
args: [token],
});
// Store in localStorage as backup/default
localStorage.setItem(`lastPrice_${token}`, price.toString());
return Number(formatUnits(price, 18));
}
async function availableToBorrow(user, token) {
const available = await publicClient.readContract({
address: CONTRACTS.sepolia.myContract.address,
abi: CONTRACTS.sepolia.myContract.abi,
functionName: "_borrowableAmount",
args: [user, token],
});
return Number(formatUnits(available, 18));
}
export async function disconnectWallet() {
if (window.ethereum) {
try {
await window.ethereum.request({
method: "wallet_revokePermissions",
params: [
{
eth_accounts: {},
},
],
});
if (window.location.pathname.endsWith("page1.html")) {
location.href = "index.html";
}
console.log("Permissions revoked");
location.href = "index.html";
} catch (error) {
console.error("Failed to revoke:", error);
}
}
userAddress = null;
localStorage.removeItem("connected");
localStorage.removeItem("userAddress");
localStorage.clear();
}
// =========== Spinner ============
export const spinner = `
<svg class="animate-spin h-4 w-4" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10"
stroke="currentColor" stroke-width="4" fill="none"></circle>
<path class="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z">
</path>
</svg>
`;
// closeBtn.onclick = closeModal();
document.querySelectorAll(".openModal").forEach((btn) => {
btn.addEventListener("click", () => {
// CASE 3: No wallet detected
if (!window.ethereum) {
setModalState(MODAL_STATE.NO_WALLET);
return;
}
// CASE 2: Wallet already connected
if (userAddress) {
setModalState(MODAL_STATE.DISCONNECT, {
userAddress,
onAction: async () => {
await disconnectWallet();
location.href = "index.html"; // optional
},
});
return;
}
// CASE 1: Not connected
setModalState(MODAL_STATE.CONNECT, {
onAction: async () => {
try {
modalActionBtn.innerHTML = `
${spinner}
<span>Connecting...</span>
`;
await connectWallet();
setTimeout(() => {
modalActionBtn.innerHTML = "Connected";
}, 1500);
closeModal();
location.href = "page1.html"; // optional: redirect after connection
} catch (err) {
// WRONG NETWORK TRANSITION
if (err.message === "WRONG_NETWORK") {
setModalState(MODAL_STATE.WRONG_NETWORK, {
expectedName: EXPECTED_CHAIN.name,
onAction: async () => {
try {
modalActionBtn.innerHTML = `
${spinner}
<span>Switching...</span>
`;
await switchNetwork();
if (window.location.pathname.endsWith("index.html")) {
location.href = "page1.html";
}
// location.reload();
} catch (err) {
setModalState(MODAL_STATE.ERROR, {
message: "Network switch failed.",
});
}
},
});
return;
}
setModalState(MODAL_STATE.ERROR, {
message: err.shortMessage,
});
}
},
});
// closeBtn.onclick = closeModal();
});
});
document.addEventListener("DOMContentLoaded", async () => {
console.log("Initializing DApp...");
// 1. Check if wallet is already connected
// console.log(formatEther(104489248056523438n));
await updateUI();
await checkExistingConnection();
});
export async function checkExistingConnection() {
if (window.ethereum && window.ethereum.selectedAddress) {
userAddress = window.ethereum.selectedAddress;
window.ethereum.on("accountsChanged", (userAddress) => {
if (userAddress.length === 0) {
// Wallet disconnected
console.log("Wallet disconnected, reloading...");
setTimeout(() => window.location.reload(), 100);
if (window.location.pathname.endsWith("page1.html")) {
location.href = "index.html";
}
}
});
if (window.location.pathname.endsWith("index.html")) {
location.href = "page1.html";
}
console.log("Wallet already connected:", userAddress);
connectHeaderBtn.innerText = shortenAddress(
window.ethereum.selectedAddress,
);
loadContract(); // initialize clients and contract
console.log(userAddress);
// await updateUI();
console.log("Processing user position ...");
getUserPosition(userAddress);
const userBalance = await publicClient.getBalance({
address: userAddress,
});
// console.log("User balance:", formatEther(userBalance), "ETH");
document.getElementById("walletEthBalance").textContent =
`${Number(formatEther(userBalance)).toFixed(3)} ETH`;
if (window.location.pathname.endsWith("page1.html")) {
document.getElementById("userAddress").textContent =
shortenAddress(userAddress);
document.getElementById("mobile").textContent =
shortenAddress(userAddress);
document.getElementById("mobileAddress").textContent =
shortenAddress(userAddress);
document.getElementById("userBalance").textContent =
`${Number(formatEther(userBalance)).toFixed(3)} ETH`;
document.getElementById("mobileBalance").textContent =
`${Number(formatEther(userBalance)).toFixed(3)} ETH`;
}
console.log("User address after checkExistingConnection:", userAddress);
// if (window.location.pathname.endsWith("page1.html")) {
// location.href = "index.html";
// }
}
}
async function userEthUsd() {
const accounts = await window.ethereum.request({
method: "eth_accounts",
});
userAddress = accounts.length > 0 ? accounts[0] : null;
try {
const userInfo = await getUserPosition(userAddress);
const stakedAmount = userInfo.stakedAmount;
const stakedAsset = userInfo.stakedAsset;
collateralInUsd = await getUsdPrice(stakedAsset, stakedAmount);
// for every occurances of eth replace the price with the usd price
} catch (err) {
console.error("unable to get user debt in USD:", err);
}
}
async function updateUI() {
const accounts = await window.ethereum.request({
method: "eth_accounts",
});
userAddress = accounts.length > 0 ? accounts[0] : null;
if (!userAddress || !publicClient) {
await loadContract();
}
try {
const userInfo = await getUserPosition(userAddress);
const stakedAmount = userInfo.stakedAmount;
const debtAmount = userInfo.debtAmount;
const debtAsset = userInfo.debtAsset;
const stakedAsset = userInfo.stakedAsset;
// console.log("User position details:", {
// stakedAmount: formatEther(stakedAmount),
// debtAmount: formatEther(debtAmount),
// debtAsset,
// stakedAsset,
// });
// -----------------------------
// USD VALUES (SAFE HANDLING)
// -----------------------------
// Always calculate collateral USD (if user has staked)
let collateralInUsd = "0.00";
if (stakedAmount > 0n) {
collateralInUsd = await getUsdPrice(stakedAsset, stakedAmount);
ETH_PRICE = await getLatestPrice(stakedAsset); // update global ETH price for availableToBorrow calculations
console.log("Eth price updated:", ETH_PRICE);
}
// Only calculate debt USD if valid
let debtInUsd = "0.00";
if (
debtAsset !== "0x0000000000000000000000000000000000000000" &&
debtAmount > 0n
) {
debtInUsd = await getUsdPrice(debtAsset, debtAmount);
debtPrice = await getLatestPrice(debtAsset);
console.log("Debt asset price updated:", debtPrice);
}
// -----------------------------
// HEALTH FACTOR (SAFE)
// -----------------------------
let healthFactor = Infinity; // default to infinitely healthy
const debtUsdNumber = Number(debtInUsd.replace(/,/g, ""));
const collateralUsdNumber = Number(collateralInUsd.replace(/,/g, ""));
if (debtUsdNumber > 0) {
healthFactor = calculateHealthFactor(
_liquidationThreshold,
collateralUsdNumber,
debtUsdNumber,
);
}
updateHealthStatus(healthFactor, {
borrower: userAddress,
debt: debtInUsd,
collateral: collateralInUsd,
bonus: 5,
});
console.log("User position in USD:", {
debtInUsd,
collateralInUsd,
healthFactor,
});
// -----------------------------
// UPDATE UI (ALWAYS)
// -----------------------------
if (window.location.pathname.endsWith("page1.html")) {
document.getElementById("stakedEth").textContent =
`${formatEther(stakedAmount)} ETH ($${collateralInUsd})`;
document.getElementById("collateral").textContent =
`${formatEther(stakedAmount)} ETH ($${collateralInUsd})`;
document.getElementById("usdOutput").textContent = `$${collateralInUsd}`;
// Default debt display (empty state)
document.getElementById("debt").textContent = `0.0 ($0.00)`;
document.getElementById("available").textContent = `0.0 ($0.00)`;
document.getElementById("debt1").textContent = `0.0 Token`;
document.getElementById("debtUsdValue").textContent = `$ 0.0`;
document.getElementById("asset").textContent = `Token`;
// -----------------------------
// AVAILABLE BORROW LOGIC
// -----------------------------
if (debtAsset !== "0x0000000000000000000000000000000000000000") {
for (const symbol in TOKENS) {
const token = TOKENS[symbol];
const tokenAddress = token.address;
if (tokenAddress === debtAsset) {
const tokenSymbol = symbol;
const availableBorrowUsd = await availableToBorrow(
userAddress,
tokenAddress,
);
const avail = parseEther(availableBorrowUsd.toString());
let availableBorrowUsdPrice = "0.00";
if (availableBorrowUsd > 0) {
availableBorrowUsdPrice = await getUsdPrice(tokenAddress, avail);
}
if (availableBorrowUsd === 0) {
document.getElementById("connectWalletBtn2").disabled = true;
}
document.getElementById("debt").textContent =
`${formatEther(debtAmount)} ${tokenSymbol} ($${debtInUsd})`;
document.getElementById("debt1").textContent =
`${formatEther(debtAmount)} ${tokenSymbol}`;
document.getElementById("debtUsdValue").textContent =
`$${debtInUsd}`;
document.getElementById("asset").textContent = tokenSymbol;
document.getElementById("available").textContent =
`${Number(availableBorrowUsd).toFixed(3)} ${tokenSymbol} ($${availableBorrowUsdPrice})`;
break;
}
}
}
}
} catch (err) {
console.error("Error updating UI:", err);
console.log("error details:", err.shortMessage);
}
}
// ========== Get USER DEBT ========
export async function getUserPosition(userAddress) {
const accounts = await window.ethereum.request({
method: "eth_accounts",
});
userAddress = accounts.length > 0 ? accounts[0] : null;
console.log("Current user address:", userAddress);
if (!userAddress || !publicClient) {
await loadContract();
console.log("Public client:", publicClient);
console.log("User address after connection:", userAddress);
}
try {
console.log("Fetching user state from contract...");
// Getting user state from contract
const user = await publicClient.readContract({
address: CONTRACTS.sepolia.myContract.address,
abi: CONTRACTS.sepolia.myContract.abi,
functionName: "getUser",
args: [userAddress],
});
const [stakedAsset, stakedAmount, debtAsset, debtAmount] = user;
console.log(debtAmount, stakedAsset, stakedAmount, debtAsset);
return { stakedAsset, stakedAmount, debtAsset, debtAmount }; // bigint
} catch (err) {
console.error("unable to get user info:", err);
}
}
export async function getUserPositionInUSD(userAddress) {
try {
const user = await getUserPosition(userAddress);
const debtAmount = user.debtAmount;
const debtAsset = user.debtAsset;
const stakedAmount = user.stakedAmount;
const stakedAsset = user.stakedAsset;
const debtInUsd = await getUsdPrice(debtAsset, debtAmount);
const collateralInUsd = await getUsdPrice(stakedAsset, stakedAmount);
return { debtInUsd, collateralInUsd };
} catch (err) {
console.error("unable to get user debt in USD:", err);
}
}
export async function loadRepayMax(user, whichToken) {
try {
const userInfo = await getUserPosition(user);
const currentDebtWei = userInfo.debtAmount;
const token = TOKENS[whichToken];
if (!token) throw new Error("Unsupported token");
const tokenAddress = token.address;
const decimals = await getTokenDecimals(tokenAddress);
console.log("rapay details", {
userDept: currentDebtWei,
token: token,
tokenAddress: tokenAddress,
tokenDecimal: decimals,
});
const formatted = formatUnits(currentDebtWei, decimals);
return formatted;
} catch (err) {
console.error(err);
}
}
// ============= MAX REPAY GETTER =========
document.getElementById("repayMaxBtn").onclick = async () => {
const selectedSymbol = document.getElementById("tokenSelect2").value;
const accounts = await window.ethereum.request({
method: "eth_accounts",
});
userAddress = accounts.length > 0 ? accounts[0] : null;
console.log("Current user address:", userAddress);
const formatted = await loadRepayMax(userAddress, selectedSymbol);
document.getElementById("repayInput").value = formatted;
document.getElementById("repayMaxDisplay").textContent =
"Debt: " + Number(formatted).toFixed(4);
};
function calculateHealthFactor(
_liquidationThreshold,
collateralValue,
debtValue,
) {
if (debtValue === 0) return Infinity; // no debt means infinitely healthy
const healthFactor = (_liquidationThreshold * collateralValue) / debtValue;
return healthFactor;
}
// FOR HEALTH FACTOR
export function updateHealthStatus(healthFactor, positionData) {
// Show appropriate one
if (window.location.pathname.endsWith("page1.html")) {
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");
trigger.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");
}
}
if (healthFactor < 1) {
if (window.location.pathname.endsWith("Liquidation.html")) {
// clearLiquidations();
renderLiquidationOpportunity(positionData);
}
}
}
// Send token modal logic
export async function sendAsset(symbol, recipient, amount) {
const tokenAddress = TOKENS[symbol];
const accounts = await window.ethereum.request({
method: "eth_accounts",
});
userAddress = accounts.length > 0 ? accounts[0] : null;
console.log("Current user address:", userAddress);
console.log("Sending asset with details:", {
symbol,
tokenAddress,
recipient,
amount,
userAddress,
});
// 🔹 Case 1: Native ETHa
try {
if (!walletClient || !publicClient) {
await loadContract();
}
if (tokenAddress.address === "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE") {
console.log("Sending native ETH...");
console.log("walletClient:", walletClient);
const hash = await walletClient.sendTransaction({
account: userAddress,
to: recipient,
value: parseEther(amount),
});
await publicClient.waitForTransactionReceipt({ hash });
console.log("ETH transfer successful:", hash);
await handleUserAction({
type: `Send ${symbol}`,
amount,
hash,
}); // log activity with actual amount and hash
return hash;
}
// 🔹 Case 2: ERC20 Token
console.log("Sending ERC20 token...");
const hash = await walletClient.writeContract({
account: userAddress,
address: tokenAddress.address,
abi: linkinfo.linkSepolia.abi, // standard ERC20 ABI with transfer function
functionName: "transferFrom",
args: [
userAddress,
recipient,
parseUnits(amount, tokenAddress.decimals), // ⚠️ adjust decimals if needed
],
});
await publicClient.waitForTransactionReceipt({ hash });
console.log(`${symbol} transfer successful:`, hash);
await handleUserAction({
type: `Send ${symbol}`,
amount,
hash,
}); // log activity with actual amount and hash
return hash;
} catch (err) {
console.error("Error loading contract in sendAsset:", err);
}
}
// populate activity log
// const logs = await publicClient.getLogs({
// address: contractAddress,
// event: parsedAbiItem("event Borrowed(address indexed user, uint256 amount)"),
// });
// import { } from "viem";
export function updateActivity(newItem) {
const existing = JSON.parse(localStorage.getItem("activity")) || [];
// Prevent duplicate txHash entries
const index = existing.findIndex((item) => item.txHash === newItem.txHash);
if (index !== -1) {
// Update existing item (e.g. Pending → Success)
existing[index] = { ...existing[index], ...newItem };
} else {
// Add newest at the top
existing.unshift(newItem);
}
localStorage.setItem("activity", JSON.stringify(existing));
renderActivity(); // auto re-render
}
async function handleUserAction({ type, amount, hash }) {
// const hash = await writeContract(writeArgs);
updateActivity({
txHash: hash,
type,
amount,
status: "Pending",
});
try {
await publicClient.waitForTransactionReceipt({ hash });
updateActivity({
txHash: hash,
status: "Success",
});
} catch (err) {
updateActivity({
txHash: hash,
status: "Failed",
});
}