-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwalletBalance.html
More file actions
51 lines (47 loc) · 1.91 KB
/
walletBalance.html
File metadata and controls
51 lines (47 loc) · 1.91 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wallet Balance</title>
<script src="https://cdn.jsdelivr.net/npm/web3@1.5.2/dist/web3.min.js"></script>
</head>
<body>
<h1>Wallet Balance</h1>
<div id="balance"></div>
<script>
if (typeof window.ethereum !== 'undefined') {
const web3 = new Web3(window.ethereum);
let userAddress;
// درخواست اتصال به MetaMask
async function connectWallet() {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
userAddress = accounts[0];
console.log('Connected wallet address:', userAddress);
getBalance(userAddress);
}
// تابع برای دریافت موجودی توکن ERC-20
async function getBalance(address) {
const tokenAddress = '0xYourTokenAddress'; // آدرس قرارداد توکن
const abi = [
{
"constant": true,
"inputs": [{"name": "_owner", "type": "address"}],
"name": "balanceOf",
"outputs": [{"name": "balance", "type": "uint256"}],
"payable": false,
"stateMutability": "view",
"type": "function"
}
];
const contract = new web3.eth.Contract(abi, tokenAddress);
const balance = await contract.methods.balanceOf(address).call();
document.getElementById('balance').innerText = `Balance: ${web3.utils.fromWei(balance)} Tokens`;
}
connectWallet();
} else {
alert('Please install MetaMask!');
}
</script>
</body>
</html>