2

Simple logic event with firing in function, work perfectly as expected in truffle develop console, but never firing on my Dapp created with create-react-app, it's just seems to not listen/monitor the network, I've people saying it should continuously load POST request to monitor network, but not in my case. I use Metamask, and all smart-contract function calls works fine on react, except events.

event RoundStarted(uint256 closingBlock);

function roundStart() public ownerOnly {
    RoundStarted(block.number + ROUND_LENGTH);
}

And the line of code I used in truffle develop console, which is not working when I paste it in componentWillMount()

  Loto.deployed().then(function(instance){
    return instance.RoundStarted({},{fromBlock: 0, toBlock: 'latest'});
  }).then((result) => {
    return result.watch((error,result) => { 
      console.log('Found him!')
    })
  })

UPD: I found out that events works fine in truffle react box, so this kinda solves the problem, but I still don't know how did they manage it, it's not my code problem, something about dependencies, I'd like to know what do I miss in my create-react-app.

2
  • To get a reference to the Loto contract are you doing something similar to this const Loto = Contract(LotoContract); Loto.setProvider(web3.currentProvider)? Where web3 is from the injected provider eg MetaMask.
    – willjgriff
    Commented Jan 28, 2018 at 10:49
  • @willjgriff, Yep, exactly, but I use truffle-contract and json file to get contract, like this: import { default as contract } from 'truffle-contract' import loto_artifacts from './Loto.json' var Loto = contract(loto_artifacts); Loto.setProvider(web3.currentProvider); Again, I think problem more like in react or something between them, I tried web3 0.20.4 and 1.0.0-29beta, both works for functions and everything but not events. Commented Jan 28, 2018 at 13:45

1 Answer 1

1

Does it work if you remove all of the JS object arguments in the contract event call, eg if you replace line 2 in your snippet with return instance.RoundStarted();

I haven't experimented with specifying start and end blocks that I want to monitor within but perhaps specifying the end block as 'latest' takes the latest block at the time your componentWillMount() function is called which is likely a block before your event is fired.

1
  • Yep, I was trying it without args in a first time, and I saw someone suggest to use them which helped to someone, so I tried both ways, and I tried to call an event watcher in componentWillMount() as well as in componentDidMount() simultaneously, and separately, and on button click, so I think it's not were the problem is, need to go deeper. Commented Jan 29, 2018 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.