3

I just started with truffle react box.. and i changed a little bit to :

const tx = await instance.set.sendTransaction(999,{from : accounts[0]});
console.log("tx is on the way : ",tx);
const txReceipt = await getMinnedTxPromise(tx);
console.log("tx minned !",txReceipt);
const response = await instance.get.call({from : accounts[0]});
console.log("the response is : ",response.toNumber());
this.setState({storageValue : response.toNumber()});

i added .sendTransaction() so that i could atleast see the transaction has begun. but when i ran this code, this line

   console.log("tx is on the way : ",tx);

it logged after the transaction was minned. when i tried .sendTransaction() from trufle console, it did log the transaction hash right away.. only when i try to run this in browser, it doesnt work as expected..

2
  • getMinnedTxPromise - this is your function, which you did not bother to post here, making it quite hard to refer to your question. sendTransaction returns a hash, not a transaction. You can use this hash to get the transaction receipt via web3.eth.getTransactionReceipt. – goodvibration Dec 11 '18 at 5:51
  • yes, that function is gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6 .. but that line is after the console.log , so there is no issue with that.. i know sendTransaction() should return the hash, which i want to display to user.. but i dont get the hash, im getting the receipt after the transaction is mined..in console.log('tx is on the way : ',tx). im running this on my private chain..so i can see when the transaction is created and when it is mined. – Ankush Tripathi Dec 11 '18 at 9:33
3

If you use await web3.eth.sendTransaction, then you get the receipt.

Alternatively, you can use the event emitter and handle each one of the following events:

  • transactionHash
  • receipt
  • confirmation (up to 12 times)
  • error

Here is an example from the official documentation for web3.js v1.x:

web3.eth.sendTransaction({
    ...
})
.on('transactionHash', function(hash){
    ...
})
.on('receipt', function(receipt){
    ...
})
.on('confirmation', function(confirmationNumber, receipt){ ... })
.on('error', console.error); // If a out of gas error, the second parameter is the receipt.

If you're using web3.js v0.x (most likely via Truffle), then web3.eth.sendTransaction returns the hash immediately (i.e., without await). See here for more details, but note that the documentation for web3.js v0.x can be quite ambiguous in some cases, in particularly with regards to asynchronous function calls. So you might need to understand the exact behavior via trial & error.

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.