-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwalletPage.html
More file actions
90 lines (81 loc) · 3.49 KB
/
walletPage.html
File metadata and controls
90 lines (81 loc) · 3.49 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wallet Page</title>
<script src="https://cdn.jsdelivr.net/npm/web3@1.5.2/dist/web3.min.js"></script>
</head>
<body>
<h1>Wallet Page</h1>
<div id="walletDetails">
<p>Address: <span id="walletAddress"></span></p>
<p>Balance: <span id="walletBalance"></span></p>
</div>
<h3>Send Tokens</h3>
<form id="sendForm">
<label for="recipient">Recipient Address:</label>
<input type="text" id="recipient" required>
<label for="amount">Amount:</label>
<input type="number" id="amount" required>
<button type="submit">Send</button>
</form>
<script>
if (typeof window.ethereum !== 'undefined') {
const web3 = new Web3(window.ethereum);
let userAddress;
async function connectWallet() {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
userAddress = accounts[0];
document.getElementById('walletAddress').innerText = userAddress;
getBalance(userAddress);
}
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('walletBalance').innerText = `${web3.utils.fromWei(balance)} Tokens`;
}
document.getElementById('sendForm').addEventListener('submit', async (e) => {
e.preventDefault();
const recipient = document.getElementById('recipient').value;
const amount = web3.utils.toWei(document.getElementById('amount').value, 'ether');
const tokenAddress = '0xYourTokenAddress'; // آدرس قرارداد توکن
const abi = [
{
"constant": false,
"inputs": [{"name": "recipient", "type": "address"}, {"name": "amount", "type": "uint256"}],
"name": "transfer",
"outputs": [{"name": "", "type": "bool"}],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
];
const contract = new web3.eth.Contract(abi, tokenAddress);
try {
await contract.methods.transfer(recipient, amount).send({ from: userAddress });
alert('Transfer successful');
} catch (error) {
console.error(error);
alert('Transfer failed');
}
});
connectWallet();
} else {
alert('Please install MetaMask!');
}
</script>
</body>
</html>