forked from ninikolov/EDUCoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom.js
More file actions
78 lines (72 loc) · 2.35 KB
/
custom.js
File metadata and controls
78 lines (72 loc) · 2.35 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
$(window).on('load', function() {
var contractAddress = "0x563b01e18316d1e85c320335b1360c25648af12d"; // in Ropsten testnet!
var contractAbi = [
{
"constant": true,
"inputs": [],
"name": "getGreeting",
"outputs": [
{
"name": "s",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "s",
"type": "string"
}
],
"name": "setGreeting",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
];
// 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;
}
// create instance of contract object that we use to interface the smart contract
var contractInstance = web3.eth.contract(contractAbi).at(contractAddress);
contractInstance.getGreeting(function(error, greeting) {
if (error) {
var errorMsg = 'error reading greeting from smart contract: ' + error;
$('#content').text(errorMsg);
console.log(errorMsg);
return;
}
$('#content').text('greeting from contract: ' + greeting);
});
$('#my-form').on('submit', function(e) {
e.preventDefault(); // cancel the actual submit
var newGreeting = $('#greeting').val();
contractInstance.setGreeting(newGreeting, function(error, txHash) {
if (error) {
var errorMsg = 'error writing new greeting to smart contract: ' + error;
$('#content').text(errorMsg);
console.log(errorMsg);
return;
}
$('#content').text('submitted new greeting to blockchain, transaction hash: ' + txHash);
});
});
});
function cb(error, response) {
// callback as helper function for debugging purposes
console.log('error: ' + error + ', response: ' + response);
}