3

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!

1
  • to inspect an object in the console use console.dir(result); instead
    – Julien
    Feb 11, 2018 at 5:03

2 Answers 2

3

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);
1

If you have a signedTransaction object, you can run

var transactionHash = web3.utils.sha3(signedTx, { encoding: "hex" });

https://web3js.readthedocs.io/en/1.0/web3-utils.html?highlight=utils#sha3 https://web3js.readthedocs.io/en/1.0/web3-eth-accounts.html#signtransaction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.