-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
281 lines (235 loc) · 6.98 KB
/
demo.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="author" content="Kenneth Hu">
<title>Web3jsdemo</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<div class="container">
<h1>Web3jsdemo</h1>
<label for="name"><h3>node info</h3></label>
Connect nodes : <input id="NodeInfo" type="text" disabled>
<hr>
<label for="name"><h3>account information</h3></label>
<p>account info : <input id="Account" type="text"> </p>
<p>account balance : <input id="Balance" type="text" disabled></p>
<button id="checkBalance">Check balances</button>
<hr>
<label for="name"><h3>transfer</h3></label>
<p>From address : <input id="From" type="text"> Default is the account address found above</p>
<p>To address : <input id="To" type="text" value="0xbb44a15462C5c5042A74b4Adc770793A7E57210a"> 默认我的账户地址</p>
<p>Amount : <input id="Amount" type="text"> uint:Ether</p>
<p>Hash : <span id="Tx"></span></p>
<button id="Transfer">Transfer</button>
<hr>
<label for="name"><h3>Contract</h3></label>
<p>Contract code : <textarea id="code" disabled>
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
/// @dev Test contract
contract TestContract {
// State variables - messages
string message;
/// @dev Constructor - initialization message
constructor() {
message = "Hello World!";
}
/// @dev Query message
function getMsg() external view returns (string memory) {
return message;
}
/// @dev Set message
function setMsg(string memory _msg) external {
message = _msg;
}
}</textarea></p>
<p>Contract ABI : <textarea id="abi" disabled>[
{
"inputs": [
{
"internalType": "string",
"name": "_msg",
"type": "string"
}
],
"name": "setMsg",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "getMsg",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
}
]</textarea></p>
<p>Contract Deploy : redbellynetwork Test Network </p>
<p>Contract Address : <input id="address" type="text" value="0x13CCB62EaE384fbcAacA1c24b58ac27f49D717cD" disabled> </p>
<p>Call Contract - query : <input id="getMsg" type="text" disabled><button id="GetMsg">query</button></p>
<p>Call Contract - set : <input id="setMsg" type="text">
<p>Hash : <span id="TxContract"></span></p>
<button id="SetMsg">set</button></p>
</div>
<script src="js/web3.min.js"></script>
<script src="js/jquery-3.2.1.slim.min.js"></script>
<script src="js/jquery-3.3.1.min.js"></script>
<script>
$( document ).ready(function() {
console.log( "ready!" );
console.log("window.ethereum===",window.ethereum)
//判断是否支持浏览器钱包
if (!window.ethereum) {
alert("没有浏览器钱包,请先下载浏览器钱包");
return false;
}
// 获取服务提供器,若没有,则可设置成自己的服务提供器
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
web3 = new Web3(new Web3.providers.HttpProvider("https://192.168.237.110:8545"));
}
/* 节点信息 - 提前加载 */
web3.eth.getNodeInfo(function(error, result){
if(error){
console.log( "error" ,error);
}
else{
$('#NodeInfo').val(result);
}
});
/* 账户信息 - 提前加载*/
web3.eth.getAccounts(function(error, accounts) {
if(error) {
console.log(error);
} else {
$('#Account').val(accounts[0]);
$('#From').val(accounts[0]);
web3.eth.getBalance(accounts[0]).then(function(result){
$('#Balance').val(web3.utils.fromWei(result, 'ether'));
});
}
});
/* 事件 - 查询账户余额 */
$('#checkBalance').click(function() {
var _account = $('#Account').val();
if (_account == "") {
alert("请输入账户地址");
return;
}
web3.eth.getBalance(_account).then(function(result){
$('#Balance').val(web3.utils.fromWei(result, 'ether'));
});
});
/* 事件 - 转账 */
$('#Transfer').click(function() {
$('#Tx').text('');
var _from = $('#From').val();
var _to = $('#To').val();
var _Amount = $('#Amount').val();
if (_from == "") {
alert("请入输入账户地址");
return;
}
if (_to == "") {
alert("请入输出账户地址");
return;
}
if (_Amount == "") {
alert("请入输入转账金额");
return;
}
var txnObject = {
"from":_from,
"to": _to,
"value": web3.utils.toWei(_Amount,'ether'),
"gas": 3000000, // (可选)
// "gasPrice": 4500000, (可选)
// "data": 'For testing' (可选)
// "nonce": 10 (可选)
}
web3.eth.sendTransaction(txnObject, function(error, result){
if(error){
alert("转账失败,错误信息:" + error.message);
}
else{
//显示生成的交易hash
var txn_hash = result;
$('#Tx').text(txn_hash);
}
});
});
/* 合约 */
// 合约地址
var address = $("#address").val();
// 合约ABI
var abi = $("#abi").val();
abi = JSON.parse(abi);
// 创建合约对象
var myContract = new web3.eth.Contract(abi, address);
// 显示消息
function getMsg() {
myContract.methods.getMsg().call().then(function(msg){
$("#getMsg").val(msg);
});
}
// 显示消息 - 提前显示
getMsg();
/* 事件 - 查询消息 */
$("#GetMsg").click(function() {
getMsg();
});
/* 事件 - 设置消息 */
$("#SetMsg").click(function() {
var newMsg = $("#setMsg").val();
if (newMsg == "") {
alert("请输入要设置的消息");
return;
}
const sendFunc = async () => {
try {
const accounts = await web3.eth.getAccounts();
//调用合约函数
myContract.methods.setMsg(newMsg).send({
from: accounts[0],
gas: 3000000
}).on("transactionHash", function(hash){
// 显示交易Hash
$('#TxContract').text(hash);
}).on("receipt", function(receipt) {
console.log("receipt==", receipt);
if (receipt.status) {
alert("设置成功");
} else {
alert("设置失败");
}
}).on("error", function(error) {
alert("交易发生错误:" + error);
});
return accounts;
} catch (err) {
console.log(err);
}
}
sendFunc();
});
});
</script>
</body>
</html>