2

I want to test off chain data input in truffle testing. So basically I kick off a test and then need to wait for another program to do something. In the old web3, this worked great:

function promisifyLogWatch(_event) {
  return new Promise((resolve, reject) => {
    _event.watch((error, log) => {
      _event.stopWatching();
      if (error !== null)
        reject(error);
      resolve(log);
    });
    });
}

And I test it in my js file like this:

await promisifyLogWatch(oracle.NewValue({ fromBlock: 'latest' }));

But now I get that _event.watch is not a function. How do I update this for web3 v1?

2

This is a pattern I use as well to test Oraclize callback functions. I'm using something like this in a presentation at EthCC next week, so today I had to figure out how to do this in newer versions of Truffle. I ended up doing the following:

const getFirstEvent = (_event) => {
  return new Promise((resolve, reject) => {
    _event.once('data', resolve).once('error', reject)
  });
}

I remembered this post from a while back, so I came back to add this as an answer.

Your Answer

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

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