-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
154 lines (150 loc) · 5.09 KB
/
events.js
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
window.onload = async function () {
let metamask = false;
if (window.ethereum) {
window.web3 = new Web3(ethereum);
metamask = true;
try {
await ethereum.enable();
accounts = await web3.eth.getAccounts();
option = {from: accounts[0]};
} catch (error) {
// User denied account access...
}
}
// Legacy dapp browsers...
else if (window.web3) {
window.web3 = new Web3(web3.currentProvider);
metamask = true;
// Acccounts always exposed
try {
web3.eth.defaultAccount = web3.eth.accounts[0];
option = {from: web3.eth.accounts[0]}
} catch (error) {
}
web3.eth.sendTransaction({/* ... */});
}
// Non-dapp browsers...
else {
web3 = new Web3(new Web3.providers.HttpProvider(inforaUrl));
var account = web3.eth.accounts.create();
option = {from: account.address};
}
CounterAddress = '0xaA4634f66a97A0Df92Be537e04a0446cdf68e755';
CounterAbi = [
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"name": "",
"type": "uint256"
}
],
"name": "BoxProduced",
"type": "event"
},
{
"constant": false,
"inputs": [
{
"name": "p",
"type": "uint256"
}
],
"name": "multiProduce",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "produce",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"constant": true,
"inputs": [],
"name": "getProductCount",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
];
Counter = new web3.eth.Contract(CounterAbi, CounterAddress);
Counter.events.BoxProduced({}, function (error, result) {
if (!error) {
returnValues = result.returnValues;
alert(returnValues[0]);
} else
alert(error);
})
document.getElementById('produce').onclick = function () {
document.getElementById('loading').style.display = 'block';
Counter.methods.produce().send(option, function (error, result) {
document.getElementById('loading').style.display = 'none';
if (!error)
Counter.methods.getProductCount().call(option, function (error, result) {
if (error)
alert(error);
});
else
alert(error);
}).on('confirmation', function (number, receipt) {
if (number == 0)
Counter.methods.getProductCount().call(option, function (error, result) {
if (!error)
document.getElementById('productCount').innerText = result.concat(' product built.');
else
alert(error);
});
});
}
document.getElementById('getProductCount').onclick = function () {
document.getElementById('loading').style.display = 'block';
Counter.methods.getProductCount().call(option, function (error, result) {
document.getElementById('loading').style.display = 'none';
if (!error)
document.getElementById('productCount').innerText = result.concat(' product built.');
else
alert(error);
});
}
document.getElementById('multiProduce').onclick = function () {
document.getElementById('loading').style.display = 'block';
Counter.methods.multiProduce(document.getElementById('prCount').value).send(option, function (error, result) {
document.getElementById('loading').style.display = 'none';
if (!error)
Counter.methods.getProductCount().call(option, function (error, result) {
if (error)
alert(error);
});
else
alert(error);
}).on('confirmation', function (number, receipt) {
if (number == 0)
Counter.methods.getProductCount().call(option, function (error, result) {
if (!error)
document.getElementById('productCount').innerText = result.concat(' product built.');
else
alert(error);
});
});
}
}