-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
490 lines (360 loc) · 16.9 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<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">
<title>Data</title>
<link rel="stylesheet" href="style/css/bootstrap.min.css">
<script src="style/js/jquery.min.js"></script>
<script src="style/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="https://unpkg.com/@metamask/detect-provider/dist/detect-provider.min.js"></script>
<script src="abi/abi.js" type="text/javascript" charset="utf-8"></script>
<script src="abi/getpairs.js" type="text/javascript" charset="utf-8"></script>
<script src="abi/nftAbi.js" type="text/javascript" charset="utf-8"></script>
<style>
.row{
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container" style="width: 600px;">
<div class="row" style="text-align: center">
<h1> DATA</h1>
</div>
<div class="row">
<div class="col-md-12">
<input class="btn btn-primary btn-block" type="submit" value="登录" id="login" onclick="login()">
</div>
</div>
</div>
<div class="container" style="width: 600px;margin-top: 10px">
<div class="row">
<div class="col-md-12">
<div class="form-group form-group-sm">
<label class="col-sm-2 control-label" for="address_from">你的地址</label>
<span></span>
<div class="col-sm-10">
<input class="form-control" type="text" id="address_from" placeholder="你的地址" disabled >
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group form-group-sm">
<label class="col-sm-2 control-label" for="contract_address">合约地址</label>
<span></span>
<div class="col-sm-10">
<input class="form-control" type="text" id="contract_address" placeholder="合约地址" >
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group form-group-sm">
<label class="col-sm-2 control-label" for="contract_address">方法名</label>
<span></span>
<div class="col-sm-10">
<input class="form-control" type="text" id="fname" placeholder="fun()" >
</div>
</div>
</div>
</div>
<div class="container" style="width: 600px;margin-top: 10px" id="balanceforblind"> </div>
<div class="row">
<div class="col-md-12">
<input class="btn btn-primary btn-block" type="submit" value="获取参数" onclick="fun()" >
</div>
</div>
<div id="data"></div>
<div class="row">
<div class="col-md-12">
<input class="btn btn-primary btn-block" type="submit" value="write" onclick="writeC()" >
</div>
</div>
<div class="row">
<div class="col-md-12">
<input class="btn btn-primary btn-block" type="submit" value="read" onclick="read()" >
</div>
</div>
<div class="container" style="width: 600px;margin-top: 10px" id="send-tx"> </div>
<div class="container" style="width: 600px;margin-top: 10px" id="read-tx"> </div>
<div class="row">
<div class="col-md-12">
<div class="form-group form-group-sm">
<label class="col-sm-2 control-label" for="typesArray">typesArray</label>
<span></span>
<div class="col-sm-10">
<input class="form-control" type="text" id="typesArray" value='["uint256"]'>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group form-group-sm">
<label class="col-sm-2 control-label" for="hexString">hexString</label>
<span></span>
<div class="col-sm-10">
<input class="form-control" type="text" id="hexString" value="0x00000000000000000000000000000000000000000000021e19e0c9bab2400000">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input class="btn btn-primary btn-block" type="submit" value="decodeParameters" onclick="decodeParameters()" >
</div>
</div>
<div class="container" style="width: 600px;margin-top: 10px" id="hexStringTo"> </div>
<!--<div class="row">-->
<!-- <div class="col-md-12">-->
<!-- <div class="form-group form-group-sm">-->
<!-- <label class="col-sm-2 control-label" for="hexString">16进制</label>-->
<!-- <span></span>-->
<!-- <div class="col-sm-10">-->
<!-- <input class="form-control" type="text" id="hex" value="">-->
<!-- </div>-->
<!-- </div>-->
<!-- </div>-->
<!--</div>-->
<!--<div class="row">-->
<!-- <div class="col-md-12"> -->
<!-- <input class="btn btn-primary btn-block" type="submit" value="16进制转10进制" onclick="hexToInt()" >-->
<!-- </div>-->
<!--</div>-->
</div>
<!--<div class="container" style="width: 600px;margin-top: 10px" id="account-list">-->
<!--</div>-->
<script>
const desiredNetwork = '56';
var web3 = new Web3();
var okt_t_e ="https://www.oklink.com/okexchain-test/";
var ropsten_e = "https://ropsten.etherscan.io/";
var rinkeby_e = "https://rinkeby.etherscan.io/";
var bsc_e ="https://bscscan.com/"
const desiredNetwork_16 = '0x3';
login();
async function login(){
console.log('login')
if (typeof window.ethereum.isMetaMask === 'undefined') {
alert('看起来您需要一个 Dapp 浏览器才能开始使用。')
alert('请安装 MetaMask!')
}else{
ethereum.request({ method: 'eth_requestAccounts' }).catch(function (reason) {
if (reason.code == 4001 ) {
alert('请同意连接 MetaMask,才能继续使用!');
return;
} else {
alert('哦!NO, 好像发生了点意外。别着急, 我们很快修复它')
return;
}
}).then(function (accounts) {
console.log('account',accounts)
// if (ethereum.networkVersion !== desiredNetwork) {
// var switch_status = switchEthereumChain(desiredNetwork_16).then(result=>{
// console.log(result);
// })
// }abi
const account = accounts[0]
//alert('登录成功');
// console.log("success");
$("#login-address").html("账户地址"+account);
$("#address_from").val(account);
$("#balance_address").val(account);
$("#login").val("已登录");
// var _provider = _detectEthereumProvider().then(result=>{
// console.log("provider");
// console.log(result);
// if (result !== window.ethereum) {
// console.error('Do you have multiple wallets installed?');
// }else{
// provider = result;
// }
// });
console.log(window.ethereum);
})
}
}
async function read(){
var contract_address = $("#contract_address").val();
var address_from = $("#address_from").val();
// var data =$("#data").val();
var data = datasha3();
if(!address_from){
alert("请先连接钱包");
return false;
}
console.log(data);
var eth_read = eth_call(contract_address , address_from , data).then(result=>{
console.log(result);
$("#read-tx").html(result);
$("#hexString").val(result);
})
//console.log(web3.utils.toDecimal(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff))
}
async function writeC(){
var contract_address = $("#contract_address").val();
var address_from = $("#address_from").val();
// var data =$("#data").val();
var data = datasha3();
if(!address_from){
alert("请先连接钱包");
return false;
}
console.log(data);
var eth_write = eth_sendTransaction(contract_address , address_from , data).then(txHash=>{
console.log(txHash);
$("#send-tx").html("交易哈希TX:"+ bsc_e +"tx/" + txHash);
})
}
function fun(){
//alert(1);
var fname = $("#fname").val();
var re = /\([^\)]+\)/g;
fname = fname.match(re)[0];
fname = fname.substring(1,fname.length-1);
var fundata =fname.split(",");
$('.datadiv').remove();
for(var index in fundata){
//console.log(index+"参数是"+fundata[index]);
$('#data').before("<div class='row datadiv'><div class='col-md-12'><div class='form-group form-group-sm'><label class='col-sm-2 control-label'>"+fundata[index]+ "</label><span></span><div class='col-sm-10'><input class='form-control datainput' type='text' id='data"+ index +"' data-for='"+ fundata[index] +"'></div></div></div></div>");
}
}
function datasha3(){
var dlength = $(".datainput").length;
var fname = $("#fname").val();
var data = web3.utils.sha3(fname).substring(0,10);
if(dlength > 0){
var re = /\([^\)]+\)/g;
fname = fname.match(re)[0];
fname = fname.substring(1,fname.length-1);
var fundata =fname.split(",");
console.log(fundata);
for(i = 0 ; i < dlength; i++){
var date_start = $("#data"+i).val();
var data_att = fundata[i];
console.log(date_start)
console.log(data_att)
// alert(date_start);
// alert(data_att);
if(data_att == "address"){
date_end = web3.eth.abi.encodeParameter('address', date_start);
date_end = padLeft(date_end.substring(2));
}else if( data_att == "uint256"){
date_end = web3.eth.abi.encodeParameter('uint256', date_start);
date_end = padLeft(date_end.substring(2));
}else if( data_att == "bool"){
date_end = web3.eth.abi.encodeParameter('uint256', date_start);
date_end = padLeft(date_end.substring(2));
}else if(data_att == "string"){
date_end = web3.eth.abi.encodeParameter('string', date_start);
date_end = padLeft(date_end.substring(2));
}else if(data_att == "address[]"){
date_end = web3.eth.abi.encodeParameter('address[]', JSON.parse(date_start));
date_end = padLeft(date_end.substring(2));
}else if(data_att == "uint256[]"){
date_end = web3.eth.abi.encodeParameter('uint256[]', JSON.parse(date_start));
date_end = padLeft(date_end.substring(2));
}else if(data_att == "bool[]"){
date_end = web3.eth.abi.encodeParameter('bool[]', JSON.parse(date_start));
date_end = padLeft(date_end.substring(2));
}
console.log(date_end)
data = data + date_end;
}
}
return data;
}
async function decodeParameters(){
//console.log(nftAbi);
var hexString = $("#hexString").val();
var typesArray = $("#typesArray").val();
typesArray = JSON.parse(typesArray);
console.log(typesArray);
console.log(hexString);
var result = web3.eth.abi.decodeParameters(typesArray, hexString);
$("#hexStringTo").empty();
$.each(result, function(key, value) {
$('#hexStringTo').append($('<p>').text(key + ': ' + value));
});
console.log(result);
}
function toHex(toHex_value){
return web3.utils.toHex(toHex_value);
}
function padLeft(oldvalue){
return web3.utils.padLeft(oldvalue, 64);
}
function utf8ToHex(string){
return web3.utils.utf8ToHex(string);
}
// // 参数补齐64位
// function changeDecimalBuZero(str, length){
// str = str.slice(2);
// // if(str.substring(0, 2) == "0x"){}
// return (Array(length).join('0') + str).slice(-length);
// }
async function eth_call(contract_address,address_from,data){
try {
var eth_call = await ethereum.request({
method: 'eth_call',
params: [
{
"from": address_from,
"to": contract_address,
"data": data,
},"latest"]
});
return eth_call;
} catch (error) {
console.error(error);
}
}
async function eth_sendTransaction(contract_address,address_from,data){
try {
const eth_sendTransaction = await ethereum.request({
method: 'eth_sendTransaction',
params: [
{
"from": address_from,
"to": contract_address,
"data": data,
},"latest"]
});
return eth_sendTransaction;
} catch (error) {
if (error.code == 4001 ) {
alert('请在 MetaMask 确认,才能继续使用!');
return;
} else {
alert('哦!NO, 好像发生了点意外。别着急, 我们很快修复它')
return;
}
console.error(error);
}
}
async function switchEthereumChain(chainId){
try {
await ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: desiredNetwork_16 }],
});
} catch (switchError) {
// This error code indicates that the chain has not been added to MetaMask.
if (error.code === 4902) {
alert("Please add the corresponding node information first");
return;
}
}
}
function hexToInt() {
var hex = $("#hex").val();
console.log(hex);
}
</script>
</body>
</html>