Skip to main content
edited title
Link

Double callback when deploying a new smart contract with web3 api and metamask

Source Link

Double callback when deploying a new smart contract with web3 api and metamask

So this is the code on the web3 api: https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract

It mentions that the function(err, myContract) callback will be fired 2 times

var myContractReturned = MyContract.new(param1, param2, {
   from:mySenderAddress,
   data:bytecode,
   gas:gasEstimate}, function(err, myContract){
    if(!err) {
       // NOTE: The callback will fire twice!
       // Once the contract has the transactionHash property set and once its deployed on an address.

   // e.g. check tx hash on the first call (transaction send)
   if(!myContract.address) {
       console.log(myContract.transactionHash) // The hash of the transaction, which deploys the contract
       $.post('/hash', {contractAddress: myContract.address, txHash:myContract.transactionHash}, function(success){
            if(success){
                
            }
       });
   
   // check address on the second call (contract deployed)
   } else {
       console.log(myContract.address) // the contract address
       $.post('/hash', {contractAddress: myContract.address, txHash:myContract.transactionHash}, function(success){
            if(success){
                
            }
       });
   }

   // Note that the returned "myContractReturned" === "myContract",
   // so the returned "myContractReturned" object will also get the address set.
 }
});

--As a note I am currently testing this through TestRPC, so my contract is deployed and mined very quickly--

I have added 2 post requests that send data to a node.js server into the code to replicate what I am trying to do with my website.

This code works perfectly fine if you deploy the contract and wait on the webpage for this callback to happen 2 times. However, if I deploy a contract, then leave the current webpage, the 2nd callback never happens and the contract address is never received.

So my question is, if i were to put my app out into production, where it could take hours to get back the contract address. Will it never be called if the user changes to a different webpage or closes out of the site?