-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
executable file
·175 lines (163 loc) · 5.37 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
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/main.css">
<!--引入web3-->
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
</head>
<body>
<div class="container">
<h1>Dapp Demo</h1>
<h2 id="info"></h2>
<label>Name:</label>
<input type="text" id="name">
<button id="button">Create</button>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
var a = document.getElementById("info");
<!--验证引入web3成功-->
console.log("web3" + web3);
<!--连接web3的provider或本地,其实本例中值得就是lite-server所创建的那个服务器环境-->
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"));
}
<!--此处中括号[]中的内容直接从remix中compile后的ABI处复制-->
var infoContract = web3.eth.contract(
[
{
"constant": false,
"inputs": [
{
"name": "_name",
"type": "string"
}
],
"name": "createRandomZombie",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"name": "zombieId",
"type": "uint256"
},
{
"indexed": false,
"name": "name",
"type": "string"
},
{
"indexed": false,
"name": "dna",
"type": "uint256"
}
],
"name": "NewZombie",
"type": "event"
},
{
"constant": true,
"inputs": [
{
"name": "_zombieId",
"type": "uint256"
}
],
"name": "getZombieDetails",
"outputs": [
{
"name": "name",
"type": "string"
},
{
"name": "dna",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "_totalSupply",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "uint256"
}
],
"name": "zombies",
"outputs": [
{
"name": "name",
"type": "string"
},
{
"name": "dna",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
);
var info = infoContract.at('0xF1191B8f85a17AD6c2e0feAaCeCe36Eb1E6deBF7');
<!--也就是web3.js提供的对于智能合约方法的操作与交互-->
<!--getInfo方法是对上面的id="info"设置,result是从智能合约获得的结果-->
info.totalSupply(function (error, result) {
if (!error) {
var re = result.toNumber();
console.log(re);
for (i = 0;
i < re;
i++
){
info.zombies(i, function (error1, result1) {
if (!error1) {
a.innerHTML = a.innerHTML+"<h5>" + "Name: " + result1[0] + " DNA: " + result1[1] + "</h5>";
// $("#info").html(result[0]+'('+result[1]+')years old');
}
});
}
// {
// }
// $("#info").html(result[0]+'('+result[1]+')years old');
}
});
<!--这里setInfo对应智能合约的set方法,此处将html中的name和age设置到智能合约-->
$("#button").click(function () {
var name = $("#name").val();
var age = $("#age").val();
info.createRandomZombie(name, function (error, result) {
if (!error) {
console.log("ok");
}
});
});
</script>
</body>
</html>