App.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import React, { Component } from 'react'
  2. import SimpleStorageContract from '../build/contracts/SimpleStorage.json'
  3. import getWeb3 from './utils/getWeb3'
  4. import './css/oswald.css'
  5. import './css/open-sans.css'
  6. import './css/pure-min.css'
  7. import './App.css'
  8. class App extends Component {
  9. constructor(props) {
  10. super(props)
  11. this.state = {
  12. storageValue: 0,
  13. web3: null
  14. }
  15. }
  16. componentWillMount() {
  17. // Get network provider and web3 instance.
  18. // See utils/getWeb3 for more info.
  19. getWeb3
  20. .then(results => {
  21. this.setState({
  22. web3: results.web3
  23. })
  24. // Instantiate contract once web3 provided.
  25. this.instantiateContract()
  26. })
  27. .catch(() => {
  28. console.log('Error finding web3.')
  29. })
  30. }
  31. instantiateContract() {
  32. /*
  33. * SMART CONTRACT EXAMPLE
  34. *
  35. * Normally these functions would be called in the context of a
  36. * state management library, but for convenience I've placed them here.
  37. */
  38. const contract = require('truffle-contract')
  39. const simpleStorage = contract(SimpleStorageContract)
  40. simpleStorage.setProvider(this.state.web3.currentProvider)
  41. // Declaring this for later so we can chain functions on SimpleStorage.
  42. var simpleStorageInstance
  43. // Get accounts.
  44. this.state.web3.eth.getAccounts((error, accounts) => {
  45. simpleStorage.deployed().then((instance) => {
  46. simpleStorageInstance = instance
  47. // Stores a given value, 5 by default.
  48. return simpleStorageInstance.set(1350, {from: accounts[0]})
  49. }).then((result) => {
  50. // Get the value from the contract to prove it worked.
  51. return simpleStorageInstance.get.call(accounts[0])
  52. }).then((result) => {
  53. // Update state with the result.
  54. return this.setState({ storageValue: result.c[0] })
  55. })
  56. })
  57. }
  58. render() {
  59. return (
  60. <div className="App">
  61. <nav className="navbar pure-menu pure-menu-horizontal">
  62. <a href="#" className="pure-menu-heading pure-menu-link">Truffle Box</a>
  63. </nav>
  64. <main className="container">
  65. <div className="pure-g">
  66. <div className="pure-u-1-1">
  67. <h1>Good to Go!</h1>
  68. <p>Your Truffle Box is installed and ready.</p>
  69. <h2>Smart Contract Example</h2>
  70. <p>If your contracts compiled and migrated successfully, below will show a stored value of 5 (by default).</p>
  71. <p>Try changing the value stored on <strong>line 59</strong> of App.js.</p>
  72. <p>The stored value is: {this.state.storageValue}</p>
  73. </div>
  74. </div>
  75. </main>
  76. </div>
  77. );
  78. }
  79. }
  80. export default App