Guest User

Untitled

a guest
Oct 13th, 2018
113
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2. import SimpleStorageContract from "./data/SimpleStorage.json";
  3. import getWeb3 from "../../client/src/utils/getWeb3";
  4. import truffleContract from "truffle-contract";
  5.  
  6. import "./App.css";
  7.  
  8. class App extends Component {
  9.   state = { storageValue: 0, web3: null, accounts: null, contract: null };
  10.  
  11.   componentDidMount = async () => {
  12.     try {
  13.       const web3 = await getWeb3();
  14.       const accounts = await web3.eth.getAccounts();
  15.       const Contract = truffleContract(SimpleStorageContract);
  16.       Contract.setProvider(web3.currentProvider);
  17.       const instance = await Contract.deployed();
  18.       this.setState({ web3, accounts, contract: instance }, this.runExample);
  19.     } catch (error) {
  20.       alert(`Failed to load web3, accounts, or contract. Check console for details.`);
  21.       console.log(error);
  22.     }
  23.   };
  24.  
  25.   runExample = async () => {
  26.     const { accounts, contract } = this.state;
  27.     await contract.set(5, { from: accounts[0] });
  28.     const response = await contract.get();
  29.     this.setState({ storageValue: response.toNumber() });
  30.   };
  31.  
  32.   render() { ... snipped ... }
  33. }
  34.  
  35. export default App;
RAW Paste Data