-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
325 lines (284 loc) · 14.1 KB
/
Copy pathapp.js
File metadata and controls
325 lines (284 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
// --- CONFIGURATION ---
const USDC_ADDR = "0x3600000000000000000000000000000000000000";
const MERCHANT_ADDRESS = "0x7a67f9b3BB918182Ad94182aC10f80F9619be81C";
const ARC_CHAIN_ID = '0x4cef52';
const RPC_URL = 'https://rpc.testnet.arc.network';
const INR_RATE = 94.25;
let userAddress = "", provider, signer, codeReader, currentService = "DIRECT";
// --- INITIALIZE ON LOAD ---
window.addEventListener('load', async () => {
if (window.ethereum && localStorage.getItem("isWalletConnected") === "true") {
const accounts = await window.ethereum.request({ method: "eth_accounts" });
if (accounts.length > 0) setupWallet(accounts[0]);
}
});
// --- WALLET CORE ---
async function connectWallet() {
if (!window.ethereum) {
window.location.href = "https://metamask.app.link/dapp/" + window.location.href.replace(/https?:\/\//, "");
return;
}
try {
const accounts = await window.ethereum.request({ method: "eth_requestAccounts" });
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: ARC_CHAIN_ID }]
}).catch(async (e) => {
if (e.code === 4902) {
await window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: ARC_CHAIN_ID,
chainName: 'Arc Testnet',
rpcUrls: [RPC_URL],
nativeCurrency: { name: 'USDC', symbol: 'USDC', decimals: 18 },
blockExplorerUrls: ['https://testnet.arcscan.app']
}]
});
}
});
setupWallet(accounts[0]);
} catch (e) { console.error(e); }
}
function setupWallet(addr) {
userAddress = addr;
provider = new ethers.providers.Web3Provider(window.ethereum);
signer = provider.getSigner();
document.getElementById("dot").className = "bg-green-500 w-2 h-2 rounded-full";
document.getElementById("walletLabel").innerText = addr.substring(0, 6) + "..." + addr.slice(-4).toUpperCase();
localStorage.setItem("isWalletConnected", "true");
fetchBalance();
}
// --- NEW: SMART VALIDATION MODAL TRIGGER ---
function showValidationError(message) {
const validModal = document.getElementById('validModal');
const validText = document.getElementById('validText');
if (validModal && validText) {
validText.innerText = message.toUpperCase();
validModal.classList.remove('hidden');
} else {
alert(message); // Fallback agar modal missing ho
}
}
// --- DYNAMIC BILLING LOGIC ---
function openBilling(serviceType) {
if(!userAddress) return connectWallet();
currentService = serviceType.toUpperCase();
const modal = document.getElementById("sendModal");
const label = document.getElementById("inputLabel");
const input = document.getElementById("sendTo");
const billingFields = document.getElementById("billingFields");
const noteBox = document.getElementById("sendPurpose");
modal.classList.remove("hidden");
modal.querySelector('h3').innerText = currentService === 'ELECTRIC' ? 'ELECTRICITY BILL' : `${currentService} RECHARGE`;
if(noteBox) noteBox.classList.add("hidden");
billingFields.innerHTML = "";
if(currentService === 'MOBILE') {
label.innerText = "MOBILE NUMBER";
input.placeholder = "Enter 10 digit number";
input.classList.remove("font-mono");
billingFields.innerHTML = `
<label class="text-[9px] font-black uppercase text-[#FF9933] ml-2 mb-1 block">Operator</label>
<select class="w-full p-4 bg-gray-50 rounded-2xl border-none text-xs font-bold mb-4">
<option>JIO Prepaid</option><option>Airtel Prepaid</option><option>VI Prepaid</option><option>BSNL</option>
</select>
<label class="text-[9px] font-black uppercase text-[#FF9933] ml-2 mb-1 block">Select Plan</label>
<select id="planSelect" onchange="updateAmountFromPlan(this)" class="w-full p-4 bg-gray-50 rounded-2xl border-none text-xs font-bold mb-4">
<option value="0">Choose a Plan</option>
<option value="299">Unlimited Pack ₹299</option>
<option value="19">Data Booster ₹19</option>
<option value="155">Validity Pack ₹155</option>
<option value="666">Premium Pack ₹666</option>
</select>
`;
} else {
label.innerText = currentService === 'ELECTRIC' ? "CONSUMER NUMBER" : "CONSUMER ID / ACCOUNT NO";
input.placeholder = `Enter your ${currentService} ID`;
input.classList.remove("font-mono");
let billerOptions = "";
if(currentService === 'ELECTRIC') {
billerOptions = `<option>Adani Electricity</option><option>Tata Power</option><option>MSEDCL</option><option>UPPCL</option>`;
} else if(currentService === 'DTH') {
billerOptions = `<option>Tata Play</option><option>Airtel Digital TV</option><option>Dish TV</option>`;
} else if(currentService === 'WIFI') {
billerOptions = `<option>Airtel Xstream</option><option>Jio Fiber</option><option>ACT Fibernet</option>`;
}
billingFields.innerHTML = `
<label class="text-[9px] font-black uppercase text-[#FF9933] ml-2 mb-1 block">Select Provider</label>
<select class="w-full p-4 bg-gray-50 rounded-2xl border-none text-xs font-bold mb-4">
${billerOptions}
</select>
`;
}
document.getElementById("sendAmt").value = "";
document.getElementById("inrCalcDisplay").innerText = "≈ (0.00 USDC)";
}
function openSend() {
if(!userAddress) return connectWallet();
currentService = "DIRECT";
document.getElementById("sendModal").classList.remove("hidden");
document.getElementById("sendModal").querySelector('h3').innerText = "SEND USDC";
document.getElementById("inputLabel").innerText = "RECIPIENT ADDRESS";
document.getElementById("sendTo").placeholder = "0x...";
document.getElementById("sendTo").classList.add("font-mono");
document.getElementById("billingFields").innerHTML = "";
const noteBox = document.getElementById("sendPurpose");
if(noteBox) noteBox.classList.remove("hidden");
}
// --- CALCULATION & PAYMENT ---
function updateInrCalc() {
const val = document.getElementById("sendAmt").value;
const display = document.getElementById("inrCalcDisplay");
if (val > 0) {
if (currentService === "DIRECT") {
display.innerText = `≈ (₹${(val * INR_RATE).toFixed(2)})`;
} else {
display.innerText = `≈ (${(val / INR_RATE).toFixed(4)} USDC)`;
}
} else { display.innerText = "≈ (₹0.00)"; }
}
async function processSend() {
const target = document.getElementById("sendTo").value;
const inputVal = document.getElementById("sendAmt").value;
const labelText = document.getElementById("inputLabel").innerText;
const btn = document.getElementById("finalSendBtn");
// --- SMART VALIDATION REPLACING ALERT ---
if(!target || target.length < 5) {
if(labelText.includes("MOBILE")) {
showValidationError("Please enter a valid Mobile Number");
} else if(labelText.includes("CONSUMER") || labelText.includes("ID") || labelText.includes("NUMBER")) {
showValidationError("Please enter a valid Consumer ID");
} else {
showValidationError("Please enter a valid recipient address");
}
return;
}
if(!inputVal || inputVal <= 0) {
showValidationError("Please enter a valid amount");
return;
}
try {
btn.innerText = "PROCESSING...";
btn.disabled = true;
let finalDest = currentService === "DIRECT" ? target : MERCHANT_ADDRESS;
let usdcAmount = currentService === "DIRECT" ? inputVal : (inputVal / INR_RATE).toFixed(6);
const contract = new ethers.Contract(USDC_ADDR, [
"function transfer(address to, uint256 value) public returns (bool)"
], signer);
const gasPrice = ethers.utils.parseUnits("22", "gwei");
const tx = await contract.transfer(
finalDest,
ethers.utils.parseUnits(usdcAmount.toString(), 6),
{
gasPrice: gasPrice,
gasLimit: 100000
}
);
btn.innerText = "VERIFYING...";
const receipt = await tx.wait();
if(receipt.status === 1) {
document.getElementById("sendModal").classList.add("hidden");
document.getElementById("receiptModal").classList.remove("hidden");
const finalInr = (usdcAmount * INR_RATE).toFixed(2);
document.getElementById("recAmt").innerText = `${usdcAmount} USDC`;
document.getElementById("recInr").innerText = `≈ ₹${finalInr}`;
document.getElementById("recTo").innerText = `Ref: ${tx.hash.substring(0,10)}...`;
fetchBalance();
}
} catch (e) {
console.error("TX ERROR:", e);
if(e.code === 4001) {
showValidationError("Transaction Cancelled");
} else {
showValidationError("Failed. Check balance/gas.");
}
document.getElementById("sendModal").classList.add("hidden");
} finally {
btn.innerText = "Confirm Payment";
btn.disabled = false;
}
}
// --- UTILS ---
function fetchBalance() {
if(!userAddress) return;
const contract = new ethers.Contract(USDC_ADDR, ["function balanceOf(address) view returns (uint256)"], provider);
contract.balanceOf(userAddress).then(bal => {
const f = ethers.utils.formatUnits(bal, 6);
document.getElementById("usdcBal").innerText = parseFloat(f).toFixed(2);
document.getElementById("inrBal").innerText = (f * INR_RATE).toLocaleString('en-IN');
});
}
async function openScan() {
if(!userAddress) return connectWallet();
document.getElementById("scanModal").classList.remove("hidden");
codeReader = new ZXing.BrowserQRCodeReader();
const videoElem = document.getElementById("scanVideo");
try {
const stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" } });
videoElem.srcObject = stream;
videoElem.play();
const result = await codeReader.decodeFromVideoElement(videoElem);
if(ethers.utils.isAddress(result.text)) {
closeModal('scanModal'); openSend();
document.getElementById("sendTo").value = result.text;
}
} catch (e) { console.error(e); closeModal('scanModal'); }
}
function closeModal(id) {
const modal = document.getElementById(id);
if(modal) modal.classList.add("hidden");
if(id === 'scanModal' && codeReader) codeReader.reset();
}
function copyAddr() { navigator.clipboard.writeText(userAddress); alert("Copied!"); }
function toggleProfile() { if(!userAddress) connectWallet(); else document.getElementById("profileMenu").classList.toggle("show"); }
function disconnectWallet() { localStorage.removeItem("isWalletConnected"); location.reload(); }
function openReceive() {
if(!userAddress) return connectWallet();
document.getElementById("receiveModal").classList.remove("hidden");
const qrDiv = document.getElementById("qrcode");
qrDiv.innerHTML = "";
document.getElementById("myAddr").innerText = userAddress;
new QRCode(qrDiv, { text: userAddress, width: 180, height: 180, colorDark : "#121271", colorLight : "#ffffff" });
}
async function openHistory() {
if (!userAddress) return connectWallet();
const modal = document.getElementById("historyModal");
const list = document.getElementById("txList");
modal.classList.remove("hidden");
list.innerHTML = `<div class="flex flex-col items-center justify-center py-10 opacity-30"><i class="fa-solid fa-circle-notch animate-spin text-2xl mb-2 text-[#121271]"></i><p class="text-[10px] font-black uppercase tracking-widest text-[#121271]">Scanning Chain...</p></div>`;
try {
const contract = new ethers.Contract(USDC_ADDR, [
"event Transfer(address indexed from, address indexed to, uint256 value)"
], provider);
const filter = contract.filters.Transfer(userAddress, null);
const logs = await contract.queryFilter(filter, -5000, "latest");
if (logs.length === 0) { list.innerHTML = `<p class="text-center opacity-20 mt-10">No History Found</p>`; return; }
list.innerHTML = logs.reverse().slice(0, 15).map(l => {
const amt = ethers.utils.formatUnits(l.args.value, 6);
return `<div class="bg-gray-50 p-4 rounded-3xl mb-3 border border-gray-100 shadow-sm">
<div class="flex justify-between">
<div>
<p class="text-[9px] font-black text-red-500 italic uppercase">Sent Success</p>
<p class="text-[8px] truncate w-32 opacity-40 font-mono">To: ${l.args.to}</p>
</div>
<div class="text-right">
<p class="text-sm font-black italic text-[#121271]">₹${(amt * INR_RATE).toFixed(2)}</p>
<p class="text-[8px] opacity-20 font-bold">${amt} USDC</p>
</div>
</div>
<div class="mt-2 pt-2 border-t border-dashed border-gray-200 flex justify-between items-center">
<p class="text-[7px] font-bold opacity-20 uppercase tracking-widest text-[#121271]">Verified Log</p>
<a href="https://testnet.arcscan.app/tx/${l.transactionHash}" target="_blank" class="text-[7px] font-black text-blue-600 underline uppercase italic">View on Scan</a>
</div>
</div>`;
}).join('');
} catch (e) { list.innerHTML = `<p class="text-center text-red-500 font-bold uppercase text-[10px] mt-10">Node Busy</p>`; }
}
function updateAmountFromPlan(selectElement) {
const amount = selectElement.value;
const amountInput = document.getElementById("sendAmt");
if(amount > 0) {
amountInput.value = amount;
updateInrCalc();
}
}