-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
83 lines (73 loc) · 2.75 KB
/
Copy pathscript.js
File metadata and controls
83 lines (73 loc) · 2.75 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
const API_KEY = 'cqt_rQVdcBFdDwjJmVk9tRjgrBfXJmVG'; // Replace with your Covalent API key
const BASE_URL = 'https://api.covalenthq.com/v1';
const CHAIN_ID = '1'; // Ethereum Mainnet
async function fetchWalletData() {
const walletAddress = document.getElementById('walletAddress').value.trim();
const errorDiv = document.getElementById('error');
// Validate wallet address
if (!Web3.utils.isAddress(walletAddress)) {
errorDiv.style.display = 'block';
errorDiv.textContent = 'Invalid wallet address!';
return;
}
errorDiv.style.display = 'none';
try {
// Fetch token balances
const balanceResponse = await fetch(
`${BASE_URL}/${CHAIN_ID}/address/${walletAddress}/balances_v2/?key=${API_KEY}`
);
const balanceData = await balanceResponse.json();
displayBalances(balanceData.data.items);
// Fetch transaction history
const txResponse = await fetch(
`${BASE_URL}/${CHAIN_ID}/address/${walletAddress}/transactions_v2/?key=${API_KEY}`
);
const txData = await txResponse.json();
displayTransactions(txData.data.items);
// Calculate portfolio value
calculatePortfolioValue(balanceData.data.items);
} catch (error) {
errorDiv.style.display = 'block';
errorDiv.textContent = 'Error fetching data. Please try again.';
console.error(error);
}
}
function displayBalances(balances) {
const balanceBody = document.getElementById('balanceBody');
balanceBody.innerHTML = '';
balances.forEach(token => {
const balance = (token.balance / 10 ** token.contract_decimals).toFixed(4);
const value = token.quote ? token.quote.toFixed(2) : 'N/A';
const row = `
<tr>
<td>${token.contract_name || 'Unknown'}</td>
<td>${token.contract_ticker_symbol || 'N/A'}</td>
<td>${balance}</td>
<td>$${value}</td>
</tr>
`;
balanceBody.innerHTML += row;
});
}
function displayTransactions(transactions) {
const txBody = document.getElementById('txBody');
txBody.innerHTML = '';
transactions.forEach(tx => {
const date = new Date(tx.block_signed_at).toLocaleDateString();
const value = (tx.value / 10 ** 18).toFixed(4); // Convert wei to ETH
const row = `
<tr>
<td><a href="https://etherscan.io/tx/${tx.tx_hash}" target="_blank">${tx.tx_hash.slice(0, 6)}...</a></td>
<td>${date}</td>
<td>${tx.from_address.slice(0, 6)}...</td>
<td>${tx.to_address ? tx.to_address.slice(0, 6) + '...' : 'N/A'}</td>
<td>${value} ETH</td>
</tr>
`;
txBody.innerHTML += row;
});
}
function calculatePortfolioValue(balances) {
const totalValue = balances.reduce((sum, token) => sum + (token.quote || 0), 0);
document.getElementById('portfolioValue').textContent = `Total: $${totalValue.toFixed(2)}`;
}