0

How do I call showLottery to display on my webpage?

Front-End

class App extends React.Component {

  state = {
    address: '',
    lottery: '',
    events: '',
  }

  async componentDidMount() {

    await window.ethereum.enable()
    this.w3 = new Web3(window.ethereum)
    const address = (await this.w3.eth.getAccounts())[0]
    this.setState({ address })

    this.contract = new this.w3.eth.Contract([
      {
        "constant": false,
        "inputs": [],
        "name": "implement",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
      },
      {
        "anonymous": false,
        "inputs": [
          {
            "indexed": false,
            "name": "_lottery",
            "type": "uint256"
          }
        ],
        "name": "showLottery",
        "type": "event"
      },
      {
        "constant": true,
        "inputs": [],
        "name": "num",
        "outputs": [
          {
            "name": "",
            "type": "uint256"
          }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
      }
    ], "0xF666108e057d5CA30a962eEf43Ef25ff52b70364")

    const lottery = await this.contract.methods.num().call()
    this.setState({lottery})


  }

  async randLottery() {
    await this.contract.methods.implement().send({from : (await this.w3.eth.getAccounts())[0]});
  }

  render() {
    return (
      <div className="App">
        <p>Address KUU: {this.state.address}</p>
        <button onClick={this.randLottery.bind(this)}>Rand</button>
        <p>Lottery Number: {this.state.lottery}</p>
        <p>History Number: {this.state.events}</p>
      </div>
    );
  }
}

Smart Contract

contract testRand {

    uint256 public num;

    event showLottery(uint256 _lottery);

    function rand() private view returns (uint256) {
       return uint256(keccak256(abi.encodePacked(block.difficulty, block.timestamp, now , block.coinbase)))%(10**6);
    }

    function implement() public {
        num = rand();
        emit showLottery(num);
    }

}
  • Please post this in plain-text, so that we can use it in order to refer to your question (when posting comments or answers to it). – goodvibration Nov 15 '19 at 15:05
  • @goodvibration I have edited the content from the image to code. – YU Kunakorn Nov 15 '19 at 15:34
  • Add return at the beginning of function randLottery, and do some debugging to see what it returns when you call it. Being that this function is async, you'll have to call it with await of course. When you do so, you'll notice that your event is embedded in the response object. See exactly where it is stored within that object, and add in to your webpage code (which you haven't bothered to post here). – goodvibration Nov 15 '19 at 15:47
  • @goodvibration I'm sorry I don't understand because I am a beginner. – YU Kunakorn Nov 15 '19 at 17:36
  • I didn't write anything which a beginner could not understand, and I'm really not sure how I can write it in simpler words. – goodvibration Nov 15 '19 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.