-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
337 lines (276 loc) · 7.56 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
<!Doctype html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.2.3/milligram.min.css">
<script src="./lib/web3.min.js"></script>
<body>
<!-- raised Amount -->
<div id="raised">Raised Amount</div>
<!-- connect wallet -->
<div>
<button onclick="init()">Connect Wallet</button>
<div id="account"></div>
</div>
<!-- getContract -->
<div>
<button onclick="GetContract()">Get Contract Instance</button>
<div id="contract"></div>
</div>
<!-- setAddress -->
<!-- <div>
<button onclick="SetAddress()">Set Your Address</button>
<input type="text" id="setaddress"/>
</div> -->
<div>
<!-- <th> <td> Number </td> <td> Account </td> <td> SentEther </td> <td> Opinion </td> </th> -->
<table
id="stateTable"
class=""
style="margin: 20px 0 50px 50px; width: 50%"
>
</table>
</div>
<!-- sendEther -->
<div>
<button onclick="SendEther()">Send Ether</button>
<input type="number" id="sendether"/>
</div>
<!-- signAgree -->
<div>
<button onclick="SignAgree()">Consensus</button>
<input type="radio" name="sign" id="signagree" value="true"/> Agree
<input type="radio" name="sign" id="signdisagree" value="false"/> Disagree
</div>
<!-- withdraw to beneficiary -->
<div>
<button onclick="WithdrawTo()">Withdraw Ether</button>
<input type="text" id="withdrawbenefi" placeholder="beneficiary address"/>
<input type="number" id="withdrawether" placeholder="amount of ether"/>
</div>
</body>
<script type="text/javascript">
let provider;
let web3;
let account;
let contract;
let raised_result;
async function init() {
if (window.ethereum) {
web3 = new Web3(window.ethereum);
try {
// Request account access if needed
await window.ethereum.enable();
// Acccounts now exposed
web3.eth.getAccounts().then(function(accounts){
account = accounts[0]
document.getElementById("account").innerText = account
})
} catch (error) {
}
}
// Legacy dapp browsers...
else if (window.web3) {
// Use Mist/MetaMask's provider.
web3 = window.web3;
console.log("Injected web3 detected.");
}
}
async function GetContract() {
let abi = [
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "ownersArr",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "sendEther",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_address",
"type": "address"
}
],
"name": "setAddress",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bool",
"name": "_answer",
"type": "bool"
}
],
"name": "signAgree",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "signs",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "beneficiary",
"type": "address"
},
{
"internalType": "uint256",
"name": "withdraw_amount",
"type": "uint256"
}
],
"name": "withdraw",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
contract = await new web3.eth.Contract(abi, "0xd9145CCE52D386f254917e481eB44e9943F39138")
document.getElementById("contract").innerText = contract._address
console.log(contract)
// raised_result
// document.getElementById("").innerText = web3.fromWei(eth.getBalance(this))
// contract.methods.ownersArr().call().then(console.log)
// contract.methods.signs(account).call().then(function(signs_result) {
// signs_result = _signs_result;
// })
// let index = contract.methods.ownersArr.length
await getList()
}
async function getList() {
raised_result = await web3.eth.getBalance("0xd9145CCE52D386f254917e481eB44e9943F39138")
document.getElementById("raised").innerText = raised_result
let ownerArr = []
for (i = 0; i < 3; i++) {
ownerArr.push(await contract.methods.ownersArr(i).call())
}
console.log('ownerArr', ownerArr)
console.log('ownersArr', contract.methods.ownersArr)
let amount_result = []
for (i = 0; i < 3; i++) {
amount_result.push(web3.utils.toWei(await contract.methods.balanceOf(ownerArr[i]).call()))
}
console.log('amount_result', amount_result)
let signs_result = []
for (i = 0; i < 3; i++) {
signs_result.push(await contract.methods.signs(ownerArr[i]).call())
}
console.log('sign_result', signs_result)
// num address ether opin
let stateData = `<tr> <td> Number </td> <td> Account </td> <td> SentEther </td> <td> Opinion </td> </tr>
<tr> <td> ${1} </td> <td> ${ownerArr[0]} </td> <td> ${amount_result[0]} </td> <td> ${signs_result[0]} </td></tr>
<tr> <td> ${2} </td> <td> ${ownerArr[1]} </td> <td> ${amount_result[1]} </td> <td> ${signs_result[1]} </td></tr>
<tr> <td> ${3} </td> <td> ${ownerArr[2]} </td> <td> ${amount_result[2]} </td> <td> ${signs_result[2]} </td></tr>`;
document.getElementById("stateTable").innerHTML = stateData;
}
// function SetAddress() {
// let addr = document.getElementById("setaddress").value
// console.log('addr', addr)
// contract.methods.setAddress(addr).send(
// {from: account}
// )
// .then(function(receipt){
// console.log(receipt)
// getList()
// });
// }
function SendEther() {
let ether = document.getElementById("sendether").value
contract.methods.sendEther().send(
{from: account, value: web3.utils.toEther(ether, "ether")}
).then(function(receipt){
console.log(receipt)
getList()
});
}
function SignAgree() {
// let sign = document.getElementByName("signagree").value
let sign = document.querySelector('input[name="sign"]:checked').value; // 체크된 값(checked value)
console.log(typeof sign);
let param;
if (sign == 'true') {
param = true;
} else if(sign == 'false') {
param = false;
}
contract.methods.signAgree(param).send(
{from: account}
).then(function(receipt){
console.log(receipt)
getList()
});
}
function WithdrawTo() {
let beneficiary = document.getElementById("withdrawbenefi").value
let amountether = document.getElementById("withdrawether").value
contract.methods.withdraw(beneficiary, web3.utils.toWei(amountether)).send(
{from: account}
).then(function(receipt){
console.log(receipt)
getList()
});
}
</script>
</html>