Repository navigation
Expand file tree
/
Copy pathcustom.js
More file actions
208 lines (193 loc) · 4.29 KB
/
Copy pathcustom.js
File metadata and controls
208 lines (193 loc) · 4.29 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
$(window).on('load', function() {
var contractAbi = [
{
"constant": false,
"inputs": [
{
"components": [
{
"name": "subject",
"type": "string"
},
{
"name": "institute",
"type": "string"
},
{
"name": "grade",
"type": "uint256"
},
{
"name": "ects",
"type": "uint256"
}
],
"name": "cc",
"type": "tuple"
}
],
"name": "addCreditCoin",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "returnTotalNumberOfCC",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getCreditCoins",
"outputs": [
{
"components": [
{
"name": "subject",
"type": "string"
},
{
"name": "institute",
"type": "string"
},
{
"name": "grade",
"type": "uint256"
},
{
"name": "ects",
"type": "uint256"
}
],
"name": "",
"type": "tuple[]"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "index",
"type": "uint256"
}
],
"name": "readEntry",
"outputs": [
{
"name": "",
"type": "string"
},
{
"name": "",
"type": "string"
},
{
"name": "",
"type": "uint256"
},
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "numberOfCC",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
}
];
tablebody = document.getElementById("record_table").tBodies[0];
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
// Use Mist/MetaMask's provider
$('#content').text('I have web3!!!');
window.web3 = new Web3(web3.currentProvider);
} else {
var errorMsg = 'I don\'t have web3 :( Please open in Google Chrome Browser and install the Metamask extension.';
$('#content').text(errorMsg);
console.log(errorMsg);
return;
}
function updateRecord(record_address) {
// create instance of contract object that we use to interface the smart contract
var contractInstance = web3.eth.contract(contractAbi).at(record_address);
// asynchronously call a function on the blockchain
contractInstance.returnTotalNumberOfCC(
function(error, n_cc) {
console.log("returnTotalNumberOfCC callback");
if (error) {
var errorMsg = 'Update record: an error occurred' + error;
$('#content').text(errorMsg);
console.log(errorMsg);
return;
}
// clear table
$("#record_table tbody tr").remove();
for(i=0; i<n_cc; ++i) {
contractInstance.readEntry(
i,
function(error, cc_entries) {
console.log("readEntry callback");
if (error) {
var errorMsg = 'Update record: an error occurred' + error;
$('#content').text(errorMsg);
console.log(errorMsg);
return;
}
// append entry to table
var row = tablebody.insertRow(-1);
var cell_subject = row.insertCell(0);
cell_subject.innerHTML = cc_entries[0];
var cell_institute = row.insertCell(1);
cell_institute.innerHTML = cc_entries[1];
var cell_grade = row.insertCell(2);
cell_grade.innerHTML = cc_entries[2];
var cell_ects = row.insertCell(3);
cell_ects.innerHTML = cc_entries[3];
}
);
}
}
);
}
$('#record_form').on('submit', function(e) {
console.log("SUBMITTING FORM");
e.preventDefault(); // cancel the actual submit
var record_address = $('#academic_record_input').val();
updateRecord(record_address);
});
});