3

When I instantiate a contract with myContract.new() like this:

 MyContract.new({ from: addr, data: code, gas: gas, gasPrice: price }, function (error, contract) {
                    if (!error) {
                        if (typeof contract.address != 'undefined') {
                            console.log("second call");
                            console.log('Confirmed. address: '
                                + contract.address
                                + ' transactionHash: '
                                + contract.transactionHash);
                        } else {
                            console.log("first call");
                        }

                    } else {
                        console.log('geth callback error: ' + error);
                    }
                });

.. the function gets called twice. First with the transaction hash (not shown) and the second with a contract address (shown).

Should I expect subsequent use of the contracts methods that are transactional to call the callback twice?

    var myContract = MyContract.at(cAddr);

    myContract.myFunction(par1, par2,
        { from: addr, data: code, gas: gas, gasPrice: price },
        function (error, obj) {
            if (error) {
                console.log("whoops!");
            } else {
               console.log("myFunction called");
            }
        });
1

A contract method that is invoked once, will only fire the callback once.

For .new, you're correct that the callback is fired twice: usually, first with the transaction hash and the second with the deployed contract's address.

  • So the behavior is not consistent then because the method is also a transaction and you need the underlying receipt before assuming it is persistent (mined) ? – Interition Jul 1 '16 at 15:43
  • Correct, .new is still a transaction behind the scenes and is currently a special case where its callback is called twice; for a contract method you don't get called a second time and you need to check the receipt manually as you've said. – eth Jul 25 '16 at 3:55
  • thanks. That is how I resolved the problem. For others reading this refer to Web3 API web3.js checkForContractAddress function. – Interition Jul 25 '16 at 8:41

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.