0

I need have a problem, when I execute a solidity function, event emits multiple times, but when I refresh page then it works normally and only past events get called.

Here is my code:

  addEventListenerOrderSent(component) {

if (typeof this.state.contractInstance !== 'undefined') {      
  try{           
    this.state.contractInstance.events.OrderSent({
      filter: {buyerAddr: [this.state.account]},
      fromBlock: 0,
      toBlock: 'latest'        
      }/* , function(error, event){ console.log(event);} */)
    .on('data', function(event){
        var epochValue = event.returnValues.dateOfOrder;      
        var d = moment.unix(epochValue).utc(); // The 0 there is the key, which sets the date to the epoch
        event.returnValues.dateOfOrder = d.local().format('DD-MM-YYYY h:mm:ss');  

        var web3 = new Web3();
        event.returnValues.itemName = web3.utils.hexToAscii(event.returnValues.itemName);

        var newArray = component.state.orders.slice();
        newArray.push(event.returnValues);
        component.setState({ orders: newArray });

    })
    .on('error', console.error);

  } catch (e) {
  // Send error to Error reporting service in 
  // production/staging stage or log to console in dev.
  console.error(e);
  }
}

}

When I call solidity function Order, event emits and for some reason event emits multiple times in my table: enter image description here

And after I call refresh, only one event stays in the table as it should. enter image description here

What am I doing wrong?

4
  • It appears like a bug in your frontend code. A couple of possibilities: you are subscribing to the same event several times, you are updating your component at the wrong time. It is better to ask in stackoverflow.com for frontend issues.
    – Ismael
    Commented Aug 9, 2019 at 16:55
  • 2
    It appears to be a bug from web3. I solved it with this piece of code var eventBlocks = new Set(); let blockNumber = event.blockNumber; if (eventBlocks.has(blockNumber)) return; eventBlocks.add(blockNumber);
    – Rin
    Commented Aug 11, 2019 at 11:38
  • Web3 has several issues of its own, but by Occam's razor it is more likely new code without reviews (your frontend) has bugs than old code with several revisions (web3 is used in many projects). Without examing your frontend code and a way to reproduce your issue it is unlikely we can determine the exact cause of the error. Your workaround might work but it looks fragile it should be better to fix web3 if that is the case.
    – Ismael
    Commented Aug 11, 2019 at 18:48
  • I dont think its the bug on my part as I literally have the event and subscription to it. It might be react-bootstrap-table bug then or web3 bug. Its weird as the same code works just fine when I refresh page but when I call my solidity function then it fires from 2 to 6 times... and after refresh it becomes just one row of data in table.. :s
    – Rin
    Commented Aug 12, 2019 at 21:25

2 Answers 2

0

Upto 24 events can be fired from a single operation.

In version 1.0 beta 38 events can be fired

And in higher version of web3.js this could be changed via: web3.transactionConfirmationBlocks=..

Have a look at this tutorial

However version 1.2.1 of web3.js is basically 1.0 beta 37 so doesn't have this (anymore)

Perhaps this helps

0

It seems to be issue with web3 as I also have the same problem. I used the workaround below.

const BLOCKS = [];

isDuplicate(blockNumber) {
  if (BLOCKS.includes(blockNumber)) return true;
  BLOCKS.push(blockNumber);
  return false;
}

Now, all you need todo is call the isDuplicate() method with the blockNumber as a parameter that you can get from the event data that's sent as the result as seen below.

if(!isDuplicate(blockNumber)){
    // proceed with execution whilst avoiding duplicated blocks
    // from being called
}

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.