-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
198 lines (163 loc) · 6.36 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!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" />
<!-- Bootstrap CSS -->
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous"
/>
<title>Document</title>
</head>
<body>
<table
id="dataTable"
class="table table-dark table-striped"
style="margin: 20px 0 50px 50px; width: 50%"
></table>
<table
id="hashTable"
class="table table-dark table-striped"
style="margin: 20px 0 50px 50px; width: 50%"
></table>
<div style="margin: 50px">
<button id="create_btn" onclick="createNewAccount()">계정생성</button>
<input type="password" id="pw" placeholder="비밀번호" />
</div>
<div style="margin: 50px">
account: <input type="text" id="account" placeholder="계정" /> password:
<input type="password" id="password" placeholder="비밀번호" />
<button id="unlock_btn" onclick="unlockAccount()">Unlock Account</button>
</div>
<div style="margin: 50px">
from: <input type="text" id="fromId" placeholder="보내는 사람" />
to:
<input type="text" id="toId" placeholder="받는 사람" /> value:<input
type="number"
id="sendValue"
placeholder="이더 수량(eth)"
/>
<button id="send_btn" onclick="sendAction()">send</button>
</div>
<div style="margin: 50px">
<input type="text" id="tx_input" placeholder="tx hash" />
<button id="getTx_btn">트랜잭션 조회</button>
</div>
</body>
<script type="text/javascript" src="./lib/bignumber.min.js"></script>
<script type="text/javascript" src="./lib/web3.js"></script>
<script type="text/javascript">
// if web3 is not undefined, set new web3 which is web3.currentprovider
if (typeof web3 !== "undefined") {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
if (web3.isConnected()) {
console.log("connected");
} else {
console.log("not connected");
}
// web3.eth.filter("latest", function (err, blockHash) {
// console.log(blockHash);
// });
function getAccounts() {
let accounts = web3.eth.accounts;
console.log(accounts);
let list = "";
let balance;
//get each accounts balance info. and add in datatable
for (var i = 0; i < accounts.length; i++) {
balance = web3.fromWei(web3.eth.getBalance(accounts[i])) + "ETH";
list += `<tr> <td> ${accounts[i]} </td> <td> ${balance} </td> </tr>`;
}
document.getElementById("dataTable").innerHTML = list;
}
//get fromid, toid, amount and txhash is reported to tx_input
function sendAction() {
let fromValue = document.getElementById("fromId").value;
let toValue = document.getElementById("toId").value;
let amountValue = document.getElementById("sendValue").value;
console.log(amountValue);
let txHash = web3.eth.sendTransaction({
from: fromValue,
to: toValue,
value: web3.toWei(amountValue, "ether"),
});
document.getElementById("tx_input").value = txHash;
getAccounts();
}
//newaccount set password
function createNewAccount() {
let pw = document.getElementById("pw").value;
web3.personal.newAccount(pw);
getAccounts();
}
function unlockAccount() {
let account = document.getElementById("account").value;
let password = document.getElementById("password").value;
web3.personal.unlockAccount(account, password);
}
function getBlock(blockNum) {
web3.eth.getBlock(blockNum, function (error, block) {
console.log(block);
});
}
//get transaction info. txhash and connect with ethereum
//what is function(err, tx)
function getTransaction() {
let txHash = document.getElementById("tx_input").value;
web3.eth.getTransaction(txHash, function (err, tx) {
console.log(tx);
console.log(txHash);
let hashData = `<tr> <td> key </td> <td> value </td> </tr>`;
hashData += `
<tr> <td> blockHash </td> <td> ${tx.blockHash} </td> </tr>
<tr> <td> blockNumber </td> <td> ${tx.blockNumber} </td> </tr>
<tr> <td> from </td> <td> ${tx.from} </td> </tr>
<tr> <td> gas </td> <td> ${tx.gas} </td> </tr>
<tr> <td> gasPrice </td> <td> ${web3.fromWei(
tx.gasPrice.toNumber()
)} </td> </tr>
<tr> <td> hash </td> <td> ${tx.hash} </td> </tr>
<tr> <td> input </td> <td> ${tx.input} </td> </tr>
<tr> <td> nonce </td> <td> ${tx.nonce} </td> </tr>
<tr> <td> r </td> <td> ${tx.r} </td> </tr>
<tr> <td> s </td> <td> ${tx.s} </td> </tr>
<tr> <td> to </td> <td> ${tx.to} </td> </tr>
<tr> <td> txIndex </td> <td> ${tx.transactionIndex} </td> </tr>
<tr> <td> v </td> <td> ${tx.v} </td> </tr>
<tr> <td> value </td> <td> ${web3.fromWei(
tx.value.toNumber()
)} </td> </tr>
`;
// blockHash: "0x22e67f2b1309c05bb996ce4dd77cbe83c578ac795fe8cc5e83b8158d78d04713"
// blockNumber: 13
// from: "0x1b0824de6a6ff8ea83e69de76ec01de3407ab0d7"
// gas: 90000
// gasPrice: BigNumber {s: 1, e: 10, c: Array(1)}
// hash: "0x55d59458e7a446cab3773f0710a71a205de115f1a94117f9f7b5388b6f94aa6e"
// input: "0x"
// nonce: 4
// r: "0x8b918b4d168671e71f8e19587de769aee91754d0c1febb3b09b7458e279f02b"
// s: "0x76efa18c22f3fdee1013e52d5e33d34f691b40d753f45e7e70717f2a5ef73cb5"
// to: "0x229b0baecbb373cd72ec891c2b64294bd66538ef"
// transactionIndex: 0
// v: "0x26"
// value: BigNumber {s: 1, e: 18, c: Array(1)}
// [[Prototype]]: Object
document.getElementById("hashTable").innerHTML = hashData;
});
}
getAccounts();
getTransaction();
document
.getElementById("getTx_btn")
.addEventListener("click", getTransaction);
</script>
</html>