1

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?

0

Theres a function called getTransactionReceipt that lets you put the Txhash in and get the contract address and a bunch of other information that is only available after the transaction has been mined.

web3.eth.getTransactionReceipt(hashString [, callback]) 

So to answer my own question, you can just call this function manually when the smart contract has finally been mined.

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.