-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
67 lines (56 loc) · 2.44 KB
/
background.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
function formatTokenAmount(amount) {
if (!amount || isNaN(amount)) return "0"; // Handle edge cases
return Number(amount).toLocaleString(undefined, { maximumFractionDigits: 6 });
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'checkTokenHolder') {
const walletAddress = message.walletAddress;
fetch(`https://genyframe.xyz/tokenHolder?walletAddress=${encodeURIComponent(walletAddress)}`)
.then(response => response.json())
.then(data => {
if (data.status === '1') {
const rawTokenAmount = parseFloat(data.result);
const formattedTokenAmount = formatTokenAmount(rawTokenAmount);
console.log(`Raw Token Amount: ${rawTokenAmount}`);
console.log(`Formatted Token Amount: ${formattedTokenAmount}`);
const userData = {
isTokenHolder: true,
tokenAmount: formattedTokenAmount,
rawTokenAmount: rawTokenAmount,
walletAddress: walletAddress,
displayAvatar: data.displayAvatar,
userName: data.userName,
displayName: data.displayName,
userBio: data.userBio,
followersCount: data.followersCount,
followingCount: data.followingCount
};
// Store validated data
chrome.storage.sync.set(userData, () => {
console.log('Token holder data stored successfully.');
// Notify popup that data is ready
chrome.runtime.sendMessage({ type: 'userDataUpdated' });
});
sendResponse({ success: true, ...userData });
} else {
console.log('User does not have sufficient tokens.');
const emptyData = {
isTokenHolder: false,
tokenAmount: "0",
rawTokenAmount: 0,
walletAddress: walletAddress
};
chrome.storage.sync.set(emptyData, () => {
console.log('Token holder status updated to false.');
chrome.runtime.sendMessage({ type: 'userDataUpdated' });
});
sendResponse({ success: false, message: data.message || 'Insufficient balance.', tokenAmount: "0" });
}
})
.catch(error => {
console.error("Error checking token holder status:", error);
sendResponse({ success: false, message: 'Error contacting the server. Please try again later.' });
});
return true; // Keep the response asynchronous
}
});