|
| 1 | +// Import the page's CSS. Webpack will know what to do with it. |
| 2 | +import "../stylesheets/app.css"; |
| 3 | + |
| 4 | +// Import libraries we need. |
| 5 | +import { default as Web3} from 'web3'; |
| 6 | +import { default as contract } from 'truffle-contract' |
| 7 | + |
| 8 | +// Import our contract artifacts and turn them into usable abstractions. |
| 9 | +import metacoin_artifacts from '../../build/contracts/MetaCoin.json' |
| 10 | + |
| 11 | +// MetaCoin is our usable abstraction, which we'll use through the code below. |
| 12 | +var MetaCoin = contract(metacoin_artifacts); |
| 13 | + |
| 14 | +// The following code is simple to show off interacting with your contracts. |
| 15 | +// As your needs grow you will likely need to change its form and structure. |
| 16 | +// For application bootstrapping, check out window.addEventListener below. |
| 17 | +var accounts; |
| 18 | +var account; |
| 19 | + |
| 20 | +window.App = { |
| 21 | + start: function() { |
| 22 | + var self = this; |
| 23 | + |
| 24 | + // Bootstrap the MetaCoin abstraction for Use. |
| 25 | + MetaCoin.setProvider(web3.currentProvider); |
| 26 | + |
| 27 | + // Get the initial account balance so it can be displayed. |
| 28 | + web3.eth.getAccounts(function(err, accs) { |
| 29 | + if (err != null) { |
| 30 | + alert("There was an error fetching your accounts."); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + if (accs.length == 0) { |
| 35 | + alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly."); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + accounts = accs; |
| 40 | + account = accounts[0]; |
| 41 | + |
| 42 | + self.refreshBalance(); |
| 43 | + }); |
| 44 | + }, |
| 45 | + |
| 46 | + setStatus: function(message) { |
| 47 | + var status = document.getElementById("status"); |
| 48 | + status.innerHTML = message; |
| 49 | + }, |
| 50 | + |
| 51 | + refreshBalance: function() { |
| 52 | + var self = this; |
| 53 | + |
| 54 | + var meta; |
| 55 | + MetaCoin.deployed().then(function(instance) { |
| 56 | + meta = instance; |
| 57 | + return meta.getBalance.call(account, {from: account}); |
| 58 | + }).then(function(value) { |
| 59 | + var balance_element = document.getElementById("balance"); |
| 60 | + balance_element.innerHTML = value.valueOf(); |
| 61 | + }).catch(function(e) { |
| 62 | + console.log(e); |
| 63 | + self.setStatus("Error getting balance; see log."); |
| 64 | + }); |
| 65 | + }, |
| 66 | + |
| 67 | + sendCoin: function() { |
| 68 | + var self = this; |
| 69 | + |
| 70 | + var amount = parseInt(document.getElementById("amount").value); |
| 71 | + var receiver = document.getElementById("receiver").value; |
| 72 | + |
| 73 | + this.setStatus("Initiating transaction... (please wait)"); |
| 74 | + |
| 75 | + var meta; |
| 76 | + MetaCoin.deployed().then(function(instance) { |
| 77 | + meta = instance; |
| 78 | + return meta.sendCoin(receiver, amount, {from: account}); |
| 79 | + }).then(function() { |
| 80 | + self.setStatus("Transaction complete!"); |
| 81 | + self.refreshBalance(); |
| 82 | + }).catch(function(e) { |
| 83 | + console.log(e); |
| 84 | + self.setStatus("Error sending coin; see log."); |
| 85 | + }); |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +window.addEventListener('load', function() { |
| 90 | + // Checking if Web3 has been injected by the browser (Mist/MetaMask) |
| 91 | + if (typeof web3 !== 'undefined') { |
| 92 | + 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") |
| 93 | + // Use Mist/MetaMask's provider |
| 94 | + window.web3 = new Web3(web3.currentProvider); |
| 95 | + } else { |
| 96 | + 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"); |
| 97 | + // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail) |
| 98 | + window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:9545")); |
| 99 | + } |
| 100 | + |
| 101 | + App.start(); |
| 102 | +}); |
0 commit comments