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");
            }
        });
eth
  • 85,679
  • 53
  • 285
  • 406
Interition
  • 437
  • 2
  • 11

1 Answers1

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.

eth
  • 85,679
  • 53
  • 285
  • 406
  • 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 01 '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 03: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 08:41