forked from toncenter/dapp-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dapp-eth.html
73 lines (63 loc) · 2.26 KB
/
dapp-eth.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ETH dApp Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="h">ETH dApp Example</div>
<div class="grey">
<span>My address is</span>
<span id="address"></span>
</div>
<button id="button" class="button">Send ETH via MetaMask</button>
<div class="grey" style="margin-top: 50px">Code on <a href="https://github.com/toncenter/dapp-example" target="_blank">GitHub</a></div>
<script>
const provider = window.ethereum;
if (!provider) {
alert('MetaMask not installed');
} else {
console.log('isMetaMask=', provider.isMetaMask);
// A) Set provider in web3.js
if (window.Web3) {
var web3 = new Web3(provider);
}
// B) Use provider object directly
document.getElementById('button').addEventListener('click', async () => {
try {
// Request account access if needed
const accounts = (await provider.send('eth_requestAccounts')).result;
// Accounts now exposed, use them
const account = accounts[0] // We currently only ever provide a single account,
// but the array gives us some room to grow.
showAccountAddress(account);
// Send ETH
provider.send(
'eth_sendTransaction',
[{
from: account,
to: account,
gas: "0x76c0", // 30400
gasPrice: '0x9184e72a',
value: '0x9184e72a000', // 10000000000000 wei = 0.00001 ETH
data: '0x123'
}]
);
} catch (error) {
// User denied or Error
console.log(error);
}
});
provider.on('accountsChanged', function (accounts) {
console.log('accountsChanged', accounts);
const account = accounts[0];
showAccountAddress(account);
});
function showAccountAddress(address) {
document.getElementById('address').innerText = address;
}
}
</script>
</body>
</html>