forked from validitylabs/EthDapp-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom.js
More file actions
44 lines (39 loc) · 2 KB
/
Copy pathcustom.js
File metadata and controls
44 lines (39 loc) · 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
$(window).on('load', function() {
var contractAddress = "0x57b005EffEd29DFE55EAC92A6833E5328787eD83"; // in Rinkeby testnet!
var contractAbi = [{"constant":false,"inputs":[{"name":"s","type":"string"}],"name":"setGreeting","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getGreeting","outputs":[{"name":"s","type":"string"}],"payable":false,"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);
});
});
});