Repository navigation
Expand file tree
/
Copy pathcustom.js
More file actions
264 lines (246 loc) · 9.27 KB
/
Copy pathcustom.js
File metadata and controls
264 lines (246 loc) · 9.27 KB
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
$(window).on('load', function() {
// var contractAddress = "0x915b5929f44b97bd99b5166f3747a07ff6e5b168"; // in Ropsten testnet!
var factoryAddress = "0x1c8ee6b443cdf7ac9675fb8e079c02b7ac1e55c7"; // in Ropsten testnet!
var factoryAbi = [
{
"constant": true,
"inputs": [
{
"name": "companyID",
"type": "uint256"
}
],
"name": "getContractAddress",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "uint256"
}
],
"name": "SurveyContracts",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "companyID",
"type": "uint256"
},
{
"name": "addressessOfEmployees",
"type": "address[]"
},
{
"name": "_hashToaddressessOfEmployees",
"type": "string"
}
],
"name": "createNewSurvey",
"outputs": [
{
"name": "newContract",
"type": "address"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
];
var surveyAbi = [
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "hashes",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "isAllowedToSumbitSurvey",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "myHash",
"type": "string"
}
],
"name": "submitResults",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"name": "addressessOfEmployees",
"type": "address[]"
},
{
"name": "_hashToaddressessOfEmployees",
"type": "string"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
}
];
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
// Use Mist/MetaMask's provider
$('#content').text('I has web3!!!');
window.web3 = new Web3(web3.currentProvider);
} else {
var errorMsg = 'I doesn\'t has web3 :( Please open in Google Chrome Browser and install the Metamask extension.';
$('#content').text(errorMsg);
console.log(errorMsg);
return;
}
var factoryInstance = web3.eth.contract(factoryAbi).at(factoryAddress);
factoryInstance.getContractAddress.call(0,function(error) {
if (error) {
console.log("Factory instantiation failed");
} else {
console.log("Factory instantiation succeeded");
}
$('#content').text('You are communication with the contract factory at address: ' + factoryAddress);
});
$('#contract_creation').on('submit', function(e) {
e.preventDefault(); // cancel the actual submit
var newCompanyID = parseInt($('#init_input_1').val(), 10);
var adrtemp = document.getElementById("init_input_2");
var newAdrList = adrtemp.value.split("\n");
var newIPFSAdr = $('#init_input_3').val();
var newContract;
newContract = factoryInstance.createNewSurvey(newCompanyID, newAdrList, newIPFSAdr, function(error, txHash) {
if (error) {
var errorMsg = 'error creating new survey (child) contract : ' + error;
$('#event_logging').text(errorMsg);
console.log(errorMsg);
return;
}
// $('#event_logging').text('submitted new creation of the survey (child) contract to blockchain, transaction hash: ' + txHash + '\nThe survey of your company is at: ' + newContract);
$('#event_logging').text('submitted new creation of the survey (child) contract to blockchain, transaction hash: ' + txHash);
});
// $('#event_logging').text('The survey of your company is at: ' + newContract);
$('#event_logging').text('Transacting. . .');
});
$("#enter_Questions_button").on('click', function(){
// var contractIndex = 1;
var contractIndex = parseInt($('#input_company_id').val(), 10);
factoryInstance.getContractAddress(contractIndex, function(error, contractAddress) {
if (error) {
console.log("No such contract found");
return;
} else {
console.log("Contract found adress: " + contractAddress);
}
// var newAddr = $('#input_contract_address').val();
// $('#input_contract_address').val("");
var one = $("#selection1").val() * 100;
var two = $("#selection2").val() * 10;
var three = $("#selection3").val();
var newHash = (parseInt(one) + parseInt(two) + parseInt(three)).toString();
console.log('1: ' + one + ' 2: ' + two + ' 3: ' + three );
// create instance of contract object that we use to interface the smart contract
var contractInstance = web3.eth.contract(surveyAbi).at(contractAddress);
// submit hash to the smart contract
contractInstance.submitResults(newHash, function(error, txHash) {
if (error) {
var errorMsg = 'error writing new message to smart contract: ' + error;
console.log(errorMsg);
return;
} else {
console.log('hash stored: ' + newHash);
alert('answers are succesfully stored on the blockchain');
}
//$('#content').text('submitted new message to blockchain, transaction hash: ' + txHash);
});
});
});
$("#getHashForAddress").on('click', function(){
// var newAddr = $('#input_contract_address').val();
// $('#input_contract_address').val("");
// var contractIndex = 1;
var contractIndex = parseInt($('#input_company_id').val(), 10);
factoryInstance.getContractAddress(contractIndex, function(error, contractAddress) {
if (error) {
console.log("No such contract found");
return;
} else {
console.log("Contract found adress: " + contractAddress);
}
var newAddr = $('#input_contract_address').val();
// create instance of contract object that we use to interface the smart contract
var contractInstance = web3.eth.contract(surveyAbi).at(contractAddress);
// get hash from the smart contract
contractInstance.hashes(newAddr, function(error, resultString) {
if (error) {
var errorMsg = 'error writing new message to smart contract: ' + error;
console.log(errorMsg);
return;
}else {
console.log("succesfully retrieved hash: " + resultString);
alert("selections: " + resultString);
}
});
$('#input_contract_address').val("");
});
});
});
function cb(error, response) {
// callback as helper function for debugging purposes
console.log('error: ' + error + ', response: ' + response);
}