-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
325 lines (283 loc) · 13.2 KB
/
Copy pathapp.js
File metadata and controls
325 lines (283 loc) · 13.2 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
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
// Import the page's CSS.
import "../stylesheets/app.css";
// Import libraries we need.
import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract'
// Import our contract artifacts and turn them into usable abstractions.
// As we started with the webpack from truffle the file is still called "meta-coin". We kept that name to avoid confusion during
// the version tracking.
import metacoin_artifacts from '../../build/contracts/MetaCoin.json'
//---------------------------------------------------------------------------------
//Try some IPFS action. Does not work yet, will soon ;0 - skip to the end of IPFS-action
//---------------------------------------------------------------------------------
/* const Block = require('ipfs-block')
var ipfs = IPFS()
function store () {
var toStore = document.getElementById('source').value
ipfs.add(Buffer.from(toStore), function (err, res) {
if (err || !res) {
return console.error('ipfs add error', err, res)
}
res.forEach(function (file) {
if (file && file.hash) {
console.log('successfully stored', file.hash)
display(file.hash)
}
})
})
}
function display (hash) {
// buffer: true results in the returned result being a buffer rather than a stream
ipfs.cat(hash, {buffer: true}, function (err, res) {
if (err || !res) {
return console.error('ipfs cat error', err, res)
}
document.getElementById('hash').innerText = hash
document.getElementById('content').innerText = res.toString()
})
}
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('store').onclick = store
}) */
//---------------------------------------------------------------------------------
// End of IPFS action
//---------------------------------------------------------------------------------
var MetaCoin = contract(metacoin_artifacts);
var accounts;
var account;
// App is wrapping now all functionalities, the functions up to "setStatus" are as in truffles webpack.
window.App = {
start: function() {
var self = this;
// Bootstrap the MetaCoin abstraction for Use.
MetaCoin.setProvider(web3.currentProvider);
// Get the initial account balance so it can be displayed.
web3.eth.getAccounts(function(err, accs) {
if (err != null) {
alert("There was an error fetching your accounts.");
return;
}
if (accs.length == 0) {
alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
return;
}
accounts = accs;
account = accounts[0];
self.refreshReputation();
self.refreshPower();
});
},
setStatus: function(message) {
var status = document.getElementById("status");
status.innerHTML = message;
},
//---------------------------------------------------------------------------------
//Now our SciDapp Logic starts.
//---------------------------------------------------------------------------------
//Allows to submit the Proposal name, the data (meaning the tags for the project i.e. "awsome SciDapp") and the IPFS hash value.
//In a later implementation the hash would be handed over via above (not working) IPFS implementation, so that the Proposal can be written
// and submitted via one Interface.
//The logic can later be extended and used for submits of scientific data etc.
sendData: function() {
var self = this;
//Read the three parameters out of the html code snippets of the correct ID.
var name = document.getElementById("propname").value;
var data = document.getElementById("data").value;
var hash = document.getElementById("hash").value;
//get the instance of the deployed contract
var meta;
MetaCoin.deployed().then(function(instance) {
meta = instance;
//now Hand over the Data to the code running on the blockchain.
return meta.sendData(name, data, hash, {from: account});
}).then(function() {
self.setStatus("Proposal submitted!");
//the submission of data costs POWER - one of our two tokens - which will be updated.
self.refreshPower();
}).catch(function(e) {
console.log(e);
self.setStatus("Error sending proposal data; see log.");
});
},
//This function refreshes the display of REPUTATION - our POWER generating token -
refreshReputation: function() {
var self = this;
var meta;
MetaCoin.deployed().then(function(instance) {
meta = instance;
return meta.getReputation.call({from: account});
}).then(function(value) {
var reputation_element = document.getElementById("reputation");
reputation_element.innerHTML = value.valueOf();
}).catch(function(e) {
console.log(e);
self.setStatus("Error getting reputation balance; see log.");
});
},
//This function refreshes the display of POWER. POWER will be used for doing submits and payment for services (i.e. "research that for me")
refreshPower: function() {
var self = this;
var meta;
MetaCoin.deployed().then(function(instance) {
meta = instance;
return meta.getPower.call({from: account});
}).then(function(value) {
console.log("refresh");
var balance_element = document.getElementById("power");
balance_element.innerHTML = value.valueOf();
}).catch(function(e) {
console.log(e);
self.setStatus("Error getting power balance; see log.");
});
},
//This function can later on be used at different instances (see above), right now it is only called when a submit is done. For the moment
// one submit costs 5 POWER (as specified in solidity code), for later implementation it would be a variable
usePower: function() {
var self = this;
var meta;
MetaCoin.deployed().then(function(instance) {
meta = instance;
return meta.usePower({from: account});
}).then(function() {
self.setStatus("Power used!");
self.refreshPower();
}).catch(function(e) {
console.log(e);
self.setStatus("Error using your power; see log.");
});
},
//Refreshes the display of what the user has submitted, a status will be displayed (under review, accepted, funded, burned)
refreshData: function() {
var self = this;
var meta;
MetaCoin.deployed().then(function(instance) {
meta = instance;
return meta.getData.call({from: account});
}).then(function(data) {
console.log(data);
//Display the submitted data.
var proposal_element = document.getElementById("submittedProposal");
proposal_element.innerHTML = data[0];
var data_element = document.getElementById("submittedData");
data_element.innerHTML = data[1];
var hash_element = document.getElementById("submittedHash");
hash_element.innerHTML = data[2];
console.log(data[4].valueOf());
var status_element = document.getElementById("propstatus");
//Display the status. It will be displayed as colored dots. See app.css
// unset = black
// consideration = yellow
// revise = orange
// burned = rot
// funded = green
if (data[3]==0){
status_element.innerHTML = '<div id="unset"></div>'; }
else if (data[3]==1){
status_element.innerHTML = '<div id="consideration"></div>'; }
else if (data[3]==2){
status_element.innerHTML = '<div id="revise"></div>'; }
else if (data[3]==3){
status_element.innerHTML = '<div id="burned"></div>'; }
else if (data[3]==4){
status_element.innerHTML = '<div id="funded"></div>'; }
}).catch(function(e) {
console.log(e);
self.setStatus("Error getting proposal data; see log.");
});
},
// Displays all the proposals that do not come from the user.
getVotingData: function() {
this.setStatus("Fetching the data... (please wait)");
var meta;
var dataStor = [];
var visible = [];
MetaCoin.deployed().then(function(instance) {
meta = instance;
// this getApplicants function returns all(!) the porposals. The filter logic is better be done in Js than on the blockchain.
return (meta.getApplicants.call({from: account}));
}).then(function(Applicants) {
// now we filter. every adresses of accounts that made a proposal, that is not equal to the current account are saved into the local variable s
for (let s of Applicants) {
if(s != account){
// not we call all the specifics of the proposals using the local variable. All this is done in a loop, as solidity cannot export dynamic file types
meta.getProposal.call(s,{from: account}).then(function(PropsData){dataStor.push(PropsData)});
visible.push(s);
}
}
// now we wait 4s, that all the data is stored into dataStor. It is a messy solution, that in a later stage should be rather implemented via
// an event listener.
setTimeout(function (r){console.log(dataStor);
window.data=dataStor;
// The proposals are getting displayed together with buttons for the vote. At a later stage this should be done via a loop, that loops over
// the amount of proposals and it should include some filtering according to the users reputation and the tags of the proposal.
// For the vote we simply send -1, 0 or +1 as a value to the voting function together with the according adress of the proposal.
if(visible.length>=1){
console.log(visible);
document.querySelector('.Vote').innerHTML = "Proposal: " + dataStor[0][0] +", "
+ "Tags: " + dataStor[0][1] + ", " + "Hash: " + dataStor[0][2] + " _______ "+ "<button id=\"vote\" onclick=\"App.Vote(-1, '"+visible[0]+"' )\">Reject</button>"
+ "<button id=\"vote\" onclick=\"App.Vote(0, '"+visible[0]+"' )\">Revise</button>" + "<button id=\"vote\" onclick=\"App.Vote(1, '"+visible[0]+"' )\">Accept</button>";
}
if(visible.length>=2){
document.querySelector('.Vote2').innerHTML = "Proposal: " + dataStor[1][0] +", "
+ "Tags: " + dataStor[1][1] +", " + "Hash: " + dataStor[1][2] + " _______ "+ "<button id=\"vote\" onclick=\"App.Vote(-1, '"+visible[1]+"' )\">Reject</button>"
+ "<button id=\"vote\" onclick=\"App.Vote(0, '"+visible[1]+"' )\">Revise</button>" + "<button id=\"vote\" onclick=\"App.Vote(1, '"+visible[1]+"' )\">Accept</button>";
}
if(visible.length>=3){
document.querySelector('.Vote3').innerHTML = "Proposal: " + dataStor[2][0] +", "
+ "Tags: " + dataStor[2][1] +", " + "Hash: " + dataStor[2][2] + " _______ "+ "<button id=\"vote\" onclick=\"App.Vote(-1, '"+visible[2]+"' )\">Reject</button>"
+ "<button id=\"vote\" onclick=\"App.Vote(0, '"+visible[2]+"' )\">Revise</button>" + "<button id=\"vote\" onclick=\"App.Vote(1, '"+visible[2]+"' )\">Accept</button>";
}
}, 4000);
});
},
// the function vote is being called via "onclick" inside the getVotingData, see above.
Vote: function(vote, adr) {
var self = this;
var meta;
MetaCoin.deployed().then(function(instance) {
meta = instance;
console.log(vote);
console.log(adr);
return meta.Vote(vote, adr, {from: account});
}).then(function() {
self.setStatus("Voting done!");
self.refreshReputation();
}).catch(function(e) {
console.log(e);
self.setStatus("Voting error; see log.");
});
},
// This function is displayed here for testing purposes. It normally would be triggered via a vote on for instance a
// completed project to award the scientist. It would award with the primary, not tradable token REPUTATION.
reward: function() {
var self = this;
this.setStatus("Initiating reward... (please wait)");
var meta;
MetaCoin.deployed().then(function(instance) {
meta = instance;
return meta.reward({from: account});
}).then(function() {
self.setStatus("Rewarding process complete!");
self.refreshReputation();
}).catch(function(e) {
console.log(e);
self.setStatus("Error topping up your reputation; see log.");
});
}
};
//---------------------------------------------------------------------------------
//End of our SciDapp Logic.
//---------------------------------------------------------------------------------
window.addEventListener('load', function() {
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
// Use Mist/MetaMask's provider
window.web3 = new Web3(web3.currentProvider);
} else {
console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:9545"));
}
App.start();
});