0

Here is what I am trying to do (Web3 1.0):

  • Deploy a contract
  • Wait until Nth confirmation.
  • Then stop getting updates from the contract.

My code:

  var myContract = new web3.eth.Contract(_abi, senderAddress, options);

  const toDeploy = myContract.deploy({
    data: _bytecode,
    arguments: [100000, "0xD41396Fa287B20311DD389D563539d8578dad23e"]
  });

  toDeploy
    .send({
      gas: 5000000,
      from: myAddress
    })
    .on("confirmation", (confirmationNumber, receipt) => {
       //After several confirmations, stop getting updates
    });

Is there a way to make web3 stop listening to the contract that was deployed?

  • maybe if(confirmationNumber === 3) return null; ? – Majd TL May 16 at 14:44
  • Unfortunately returning doesn't stop .on from firing. – fcs4rx7 May 16 at 18:32
0

You should receive up to 12 confirmations, from the web3 docs:

"confirmation" returns Number, Object: Is fired for every confirmation up to the 12th confirmation. Receives the confirmation number as the first and the receipt as the second argument. Fired from confirmation 0 on, which is the block where its minded.

So not sure how to unsubscribe from the event, this seems to be missing from the docs. But looks like an active unsubscription isn't required.

2

Try changing on for once

var myContract = new web3.eth.Contract(_abi, senderAddress, options);

const toDeploy = myContract.deploy({
data: _bytecode,
arguments: [100000, "0xD41396Fa287B20311DD389D563539d8578dad23e"]
  }); 

toDeploy
.send({
  gas: 5000000,
  from: myAddress
})
.once("confirmation", (confirmationNumber, receipt) => {
   //After several confirmations, stop getting updates
});

https://web3js.readthedocs.io/en/v1.2.0/callbacks-promises-events.html#promievent

you have different options to use on callback events; on, once, or off.

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.