-
Notifications
You must be signed in to change notification settings - Fork 22
/
sendtransaction.html
45 lines (42 loc) · 1.95 KB
/
sendtransaction.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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://unpkg.com/web3@latest/dist/web3.min.js"></script>
</head>
<body>
<h1>sendTransaction</h1>
<pre id="log" style="width:100%;height:200px"></pre>
<script type="text/javascript">
function log(logstr) {
document.getElementById("log").innerHTML +=logstr+"\n";
}
async function f() {
web3 = new Web3(Web3.givenProvider); // provider from metamask
log(`Version of web3.js: ${web3.version}`);
var result=await web3.eth.requestAccounts().catch(x=>log(x.message));
log(`Value from eth_requestAccounts: ${JSON.stringify(result)}`);
var acts=await web3.eth.getAccounts().catch(log);
log(`Here are the accounts: ${JSON.stringify(acts)}`);
var fromadr=acts[0];
toadr="0x356D3d2e3dd533E1293087e73c2C36D91337d0c6";
log(`fromadr ${fromadr} has ${Web3.utils.fromWei(
await web3.eth.getBalance(fromadr), 'ether')} ether` );
log(`toadr ${toadr} has ${Web3.utils.fromWei (
await web3.eth.getBalance(toadr), 'ether')} ether` );
log(`Transfering 0.01 ether`);
obj= await web3.eth.sendTransaction({
from: fromadr,
to: toadr,
value: Web3.utils.toWei('0.01', 'ether')
}).catch(x=>log(x.message));
log(`Stored in block ${obj.blockNumber}`)
log(`fromadr ${fromadr} has ${Web3.utils.fromWei (
await web3.eth.getBalance(fromadr), 'ether')} ether` );
log(`toadr ${toadr} has ${Web3.utils.fromWei (
await web3.eth.getBalance(toadr), 'ether')} ether` );
}
window.addEventListener('DOMContentLoaded', f);
</script>
</body>
</html>