forked from MayankVats/web3_session
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
106 lines (92 loc) · 3.04 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Web3js | Tutorial</title>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/web3/1.7.3/web3.min.js"
integrity="sha512-Ws+qbaGHSFw2Zc1e7XRpvW+kySrhmPLFYTyQ95mxAkss0sshj6ObdNP3HDWcwTs8zBJ60KpynKZywk0R8tG1GA=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
></script>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100vw;
}
</style>
</head>
<body>
<div class="container">
<div>
Account:
<span id="metamask-btn"></span>
</div>
<input type="text" id="amount" placeholder="Enter Amount ..." />
<input type="text" id="address" placeholder="Enter Address ..." />
<button id="send-btn">send</button>
</div>
<script>
const metamaskBtn = document.querySelector("#metamask-btn");
const sendBtn = document.querySelector("#send-btn");
const amountInput = document.querySelector("#amount");
const addressInput = document.querySelector("#address");
let from;
async function init() {
if (window.ethereum) {
window.web3 = new Web3(window.ethereum);
await window.ethereum.request({ method: "eth_requestAccounts" });
let accounts = await web3.eth.getAccounts();
from = accounts[0];
metamaskBtn.innerHTML = `${accounts[0].slice(
0,
10
)}...${accounts[0].slice(-8)}`;
window.ethereum.on("accountsChanged", (accounts) => {
metamaskBtn.innerHTML = `${accounts[0].slice(
0,
10
)}...${accounts[0].slice(-8)}`;
from = accounts[0];
});
}
sendBtn.addEventListener("click", () => {
console.log(String(amountInput.value));
console.log(addressInput.value);
sendNativeCurrency(from, addressInput.value, amountInput.value);
});
}
async function sendNativeCurrency(from, to, amount) {
const gasPrice = await web3.eth.getGasPrice();
const nonce = await web3.eth.getTransactionCount(from);
const txObject = {
nonce,
from,
gasPrice,
gas: "21000",
to,
value: web3.utils.toWei(amount.toString()),
data: "",
};
console.log(
"\n\n🚀 ~ file: 2-web3_tutorial-adv.js ~ line 79 ~ sendNativeCurrency ~ txObject",
txObject
);
// const signedTx = await web3.eth.accounts.signTransaction(
// txObject,
// );
const receipt = await web3.eth.sendTransaction(txObject);
console.log(
"\n\n🚀 ~ file: 2-web3_tutorial-adv.js ~ line 106 ~ sendNativeCurrency ~ receipt",
receipt
);
}
init();
</script>
</body>
</html>