I want to call a function on a contract, using the JS ABI, and receive both:
1) The return value of the function
2) The transaction hash created by the function call (assuming our call changes the state of the contract).
Both data are necessary to assure that a returned value from a contract function call is mined and settled into the state of the blockchain.
Under the current Javascript API, these operations are mutually exclusve; I can use myContract.myMethod.call() to invoke myMethod and get it's return value, but not the hash of the created transaction, or I can use myContract.myMethod.sendTransaction() to invoke myMethod with no visibility to the return value, but I do get the new transaction hash.
One approach with the current API is to use myContract.myMethod.call(params..., tx) with a unique tx.nonce, and then watch the blockchain for our transaction to appear with that nonce. However, it would be preferable to receive the transaction hash in the return value from .call() so we can properly watch the blockchain for our tx hash instead of relying on nonce as a unique id.
1) The return value of the function
You can't do that. Client doesn't know when transaction will be executed so it can't return result from sendTransaction. Instead you should use events
contract MyContract {
event MyEvent(uint value);
function myFunction(uint value) {
MyEvent(value);
}
}
... be preferable to receive the transaction hash in the return value from .call()
call immediately executes a message call transaction on VM of the node, but it's never mined into the blockchain. There is no transaction hash.
Just to add some clarity to the issue: the reason why it is not possible to get the return value is because the function, unless it is view (i.e. constant), could have side effects. This means that the return value can be different, depending on the order in which the transaction is mined.
What you should do, therefore, is to create the relevant events in your smart contract and watch for them.
@simonjanin Thank you for adding some clarity to the issue. At least to me the point you raised was really necessary and important to understand it.
Most helpful comment
Just to add some clarity to the issue: the reason why it is not possible to get the return value is because the function, unless it is view (i.e. constant), could have side effects. This means that the return value can be different, depending on the order in which the transaction is mined.
What you should do, therefore, is to create the relevant events in your smart contract and watch for them.