Web3js – How to Get Transaction Hash of a Function Call

web3js

Lets say I use web3 to call foo.bar() like so:

var foo = await Foo.deployed();
var result = await foo.bar();

If I console.log(result) I just see something that says [Object object]

How can I get the transaction hash of the function call? I'd like to be able to keep track of its status. e.g. failed, pending etc.

Thanks!

Best Answer

This depends if you are using the new beta version of web3.js 1.0 or the older 0.x version:

from the web3 1.0 beta documentation:

myContract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'})
    .on('transactionHash', function(hash){
        console.log(hash);
})

in web3 0.x you can see here how to call a smart contract function and then look here for the return value:

var txHash = myContractInstance.myMethod(param1, param2);
console.log(txHash);