-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
266 lines (217 loc) · 9.51 KB
/
Copy pathscript.js
File metadata and controls
266 lines (217 loc) · 9.51 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
document.addEventListener('DOMContentLoaded', () => {
// ============================================
// 0. CONFIGURATION (MARKET SETUP)
// ============================================
// 1. MERCHANT WALLET: Where the money goes.
// REPLACE with your wallet address!
const MERCHANT_ADDRESS = "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4";
// 2. TOKEN CONTRACT ADDRESS (USDT on Sepolia Testnet)
// This is a "Fake USDT" contract for testing.
// On Mainnet (Real Life), use USDT: 0xdAC17F958D2ee523a2206206994597C13D831ec7
const USDT_CONTRACT_ADDRESS = "0x7169D38820dfd117C3FA1f22a697dBA58d90BA06";
// 3. TARGET NETWORK: Sepolia Testnet
// Chain ID 11155111 in Hex is 0xaa36a7
const TARGET_CHAIN_ID = '0xaa36a7';
// 4. ABI (Application Binary Interface)
// This tells JS how to talk to the Token Contract
const ERC20_ABI = [
"function transfer(address to, uint amount) returns (bool)",
"function decimals() view returns (uint8)",
"function balanceOf(address owner) view returns (uint256)",
"function symbol() view returns (string)"
];
// ============================================
// 1. UI & ANIMATIONS (Pure Frontend)
// ============================================
// Mobile Menu Toggle
const hamburger = document.querySelector('.hamburger');
const navLinks = document.querySelector('.nav-links');
const navLinksItems = document.querySelectorAll('.nav-links li a');
hamburger.addEventListener('click', () => {
navLinks.classList.toggle('active');
const icon = hamburger.querySelector('i');
icon.classList.toggle('fa-bars');
icon.classList.toggle('fa-times');
});
navLinksItems.forEach(item => {
item.addEventListener('click', () => {
navLinks.classList.remove('active');
hamburger.querySelector('i').classList.remove('fa-times');
hamburger.querySelector('i').classList.add('fa-bars');
});
});
// Header Glassmorphism
const header = document.getElementById('header');
window.addEventListener('scroll', () => {
if (window.scrollY > 50) {
header.style.boxShadow = "0 4px 20px rgba(0,0,0,0.4)";
header.style.background = "rgba(15, 23, 42, 0.95)";
} else {
header.style.boxShadow = "none";
header.style.background = "rgba(15, 23, 42, 0.9)";
}
});
// Scroll Reveal Animation
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
observer.unobserve(entry.target);
}
});
}, { threshold: 0.15, rootMargin: "0px 0px -50px 0px" });
document.querySelectorAll('.scroll-reveal').forEach(el => observer.observe(el));
// ============================================
// 2. UTILITY FUNCTIONS (UX)
// ============================================
function showToast(message, type = "info") {
let background = "linear-gradient(to right, #0f172a, #334155)";
if (type === "success") background = "linear-gradient(to right, #00b09b, #96c93d)";
if (type === "error") background = "linear-gradient(to right, #ff5f6d, #ffc371)";
if (type === "warning") background = "linear-gradient(to right, #f2994a, #f2c94c)";
Toastify({
text: message,
duration: 4000,
close: true,
gravity: "top",
position: "right",
style: {
background: background,
borderRadius: "10px",
fontFamily: "Inter, sans-serif",
boxShadow: "0 4px 15px rgba(0,0,0,0.3)"
},
}).showToast();
}
// ============================================
// 3. WEB3 INTEGRATION (Ethers.js + ERC20)
// ============================================
const connectBtn = document.getElementById('connect-wallet-btn');
const buyButtons = document.querySelectorAll('.buy-btn');
// Ethers.js variables
let provider;
let signer;
let userAddress;
// A. CONNECT WALLET
async function connectWallet() {
// Check if MetaMask is installed
if (!window.ethereum) {
showToast("MetaMask not found. Please install it.", "error");
setTimeout(() => window.open('https://metamask.io/download/', '_blank'), 2000);
return;
}
try {
// 1. Initialize Ethers Provider
provider = new ethers.BrowserProvider(window.ethereum);
// 2. Request Access
await provider.send("eth_requestAccounts", []);
// 3. Get Signer (The User)
signer = await provider.getSigner();
userAddress = await signer.getAddress();
// 4. Check Network
const network = await provider.getNetwork();
// Convert BigInt chainId to Hex string for comparison
const chainIdHex = "0x" + network.chainId.toString(16);
if (chainIdHex !== TARGET_CHAIN_ID) {
await switchNetwork();
} else {
updateUI(userAddress);
showToast("🚀 Connected! Ready to pay with USDT.", "success");
}
} catch (error) {
console.error("Connection Error:", error);
showToast("Failed to connect wallet.", "error");
}
}
// B. SWITCH NETWORK
async function switchNetwork() {
try {
showToast("⚠️ Switching to Sepolia Network...", "warning");
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: TARGET_CHAIN_ID }],
});
window.location.reload(); // Reload to refresh provider
} catch (error) {
// Error 4902 means the chain hasn't been added to MetaMask
if (error.code === 4902) {
showToast("❌ Error: Sepolia network not found in MetaMask.", "error");
} else {
showToast("❌ Network switch rejected.", "error");
}
}
}
// C. UPDATE UI
function updateUI(address) {
const shortAddress = `${address.substring(0, 6)}...${address.substring(38)}`;
connectBtn.innerText = shortAddress;
connectBtn.classList.add('btn-primary');
connectBtn.classList.remove('btn-sm');
}
// D. PAYMENT LOGIC (The Core Feature)
async function payWithToken(productName, price) {
if (!signer) {
showToast("⚠️ Connect wallet first!", "warning");
connectWallet();
return;
}
try {
showToast(`Initializing contract for ${productName}...`, "info");
// 1. Load Token Contract
const tokenContract = new ethers.Contract(USDT_CONTRACT_ADDRESS, ERC20_ABI, signer);
// 2. Get Decimals (Crucial for USDT vs ETH)
// USDT usually has 6 decimals, ETH has 18. We must ask the contract.
const decimals = await tokenContract.decimals();
const symbol = await tokenContract.symbol();
// 3. Calculate Amount (Price * 10^decimals)
// ethers.parseUnits handles the math safely
const amountToPay = ethers.parseUnits(price.toString(), decimals);
// 4. Check User Balance
const balance = await tokenContract.balanceOf(userAddress);
if (balance < amountToPay) {
showToast(`❌ Insufficient ${symbol} balance.`, "error");
return;
}
// 5. Send Transaction (Transfer)
showToast(`⏳ Confirm transaction in MetaMask...`, "info");
// This calls the 'transfer' function inside the smart contract
const tx = await tokenContract.transfer(MERCHANT_ADDRESS, amountToPay);
// 6. Wait for Confirmation (Mining)
showToast(`Transaction sent! Waiting for block confirmation...`, "info");
await tx.wait(); // This pauses code until block is mined
// 7. Success!
showToast(`✅ Purchase Successful!`, "success");
console.log("Tx Hash:", tx.hash);
setTimeout(() => {
window.open(`https://sepolia.etherscan.io/tx/${tx.hash}`, '_blank');
}, 1000);
} catch (error) {
console.error("Payment Error:", error);
if (error.code === 'ACTION_REJECTED') {
showToast("❌ You rejected the transaction.", "error");
} else {
showToast("❌ Transaction failed.", "error");
}
}
}
// ============================================
// 4. EVENT LISTENERS
// ============================================
// Handle Buy Buttons
buyButtons.forEach(btn => {
btn.addEventListener('click', (e) => {
e.preventDefault();
const item = btn.getAttribute('data-item');
const price = btn.getAttribute('data-price');
// Call the USDT/Token payment function
payWithToken(item, price);
});
});
// Detect Wallet Changes
if (window.ethereum) {
window.ethereum.on('accountsChanged', () => window.location.reload());
window.ethereum.on('chainChanged', () => window.location.reload());
}
// Connect Button
connectBtn.addEventListener('click', connectWallet);
});