|
| 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 | +// web3 is going to be set on load event |
| 9 | +var web3; |
| 10 | + |
| 11 | +// Import our contract artifacts and turn them into usable abstractions. |
| 12 | +import ticketSalesArtifacts from "../../build/contracts/TicketSales.json"; |
| 13 | +var TicketSales = contract(ticketSalesArtifacts); |
| 14 | + |
| 15 | +// The following code is simple to show off interacting with your contracts. |
| 16 | +// As your needs grow you will likely need to change its form and structure. |
| 17 | +// For application bootstrapping, check out window.addEventListener below. |
| 18 | +var accounts; |
| 19 | +var account; |
| 20 | + |
| 21 | +window.App = { |
| 22 | + start: function() { |
| 23 | + let self = this; |
| 24 | + |
| 25 | + // Bootstrap the MetaCoin abstraction for Use. |
| 26 | + TicketSales.setProvider(web3.currentProvider); |
| 27 | + |
| 28 | + // Get the initial account balance so it can be displayed. |
| 29 | + web3.eth.getAccounts(function(err, accs) { |
| 30 | + if (err != null) { |
| 31 | + alert("There was an error fetching your accounts."); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + if (accs.length == 0) { |
| 36 | + alert( |
| 37 | + "Couldn't get any accounts! Make sure your Ethereum client is configured correctly." |
| 38 | + ); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + accounts = accs; |
| 43 | + account = accounts[0]; |
| 44 | + |
| 45 | + self.refresh(); |
| 46 | + }); |
| 47 | + }, |
| 48 | + |
| 49 | + setStatus: function(message) { |
| 50 | + var status = document.getElementById("status"); |
| 51 | + status.innerHTML = message; |
| 52 | + }, |
| 53 | + |
| 54 | + refresh: async function() { |
| 55 | + let self = this; |
| 56 | + try { |
| 57 | + //let instance = await TicketSales.deployed(); |
| 58 | + let value = await web3.eth.getBalance(account); |
| 59 | + let balanceElement = document.getElementById("balance"); |
| 60 | + let accountElement = document.getElementById("account"); |
| 61 | + accountElement.innerHTML = account; |
| 62 | + balanceElement.innerHTML = web3.utils.fromWei(value.valueOf()); |
| 63 | + } catch (e) { |
| 64 | + console.log(e); |
| 65 | + self.setStatus("Error getting balance; see log."); |
| 66 | + } |
| 67 | + }, |
| 68 | + |
| 69 | + buyTicket: async function() { |
| 70 | + let self = this; |
| 71 | + try { |
| 72 | + this.setStatus("Initiating transaction... (please wait)"); |
| 73 | + alert("todo"); |
| 74 | + //let instance = TicketSales.deployed(); |
| 75 | + } catch (e) { |
| 76 | + console.log(e); |
| 77 | + self.setStatus("Error buying ticket coin; see log."); |
| 78 | + } |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +window.addEventListener("load", function() { |
| 83 | + // Checking if Web3 has been injected by the browser (Mist/MetaMask) |
| 84 | + if (typeof window.web3 !== "undefined") { |
| 85 | + console.debug("Using web3 detected from external source."); |
| 86 | + web3 = new Web3(window.web3.currentProvider); |
| 87 | + } else { |
| 88 | + // throw new Error("No web3 detected."); |
| 89 | + // set the provider you want from Web3.providers |
| 90 | + console.debug( |
| 91 | + "No web3 detected. Falling back to http://localhost:8545. 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" |
| 92 | + ); |
| 93 | + web3 = new Web3( |
| 94 | + new Web3.providers.HttpProvider("http://localhost:8545") |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + window.App.start(); |
| 99 | +}); |
0 commit comments