Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
63 lines (51 loc) · 2.39 KB
/
Copy pathapp.js
File metadata and controls
63 lines (51 loc) · 2.39 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
// Import the page's CSS. Webpack will know what to do with it.
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.
import hello_artifacts from '../../build/contracts/HelloEther.json'
// hello is our usable abstraction, which we'll use through the code below.
var HelloEther = contract(hello_artifacts);
// The following code is simple to show off interacting with your contracts.
// As your needs grow you will likely need to change its form and structure.
// For application bootstrapping, check out window.addEventListener below.
var accounts;
var account;
window.App = {
start: function() {
var self = this;
HelloEther.setProvider(web3.currentProvider);
},
setStatus: function(message) {
var status = document.getElementById("status");
status.innerHTML = message;
},
sayHello: function() {
var self = this;
this.setStatus("Initiating transaction... (please wait)");
var hello;
HelloEther.deployed().then(function(instance) {
hello = instance;
return hello.getHello.call();
}).then(function(res) {
self.setStatus(res);
}).catch(function(e) {
console.log(e);
self.setStatus("Error saying hello; see log.");
});
}
};
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 HelloEther, 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:7545. 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:7545"));
}
App.start();
});