5

I'm using truffle.

How can I get transaction id before it is mined? For example:

contract.purchase(price).then(function(tx) {
    console.log("Success");

}).catch(function(e){
    console.log(e);
});

In that case I only get the tx after the transaction is mined. But I want to have the ability to print the transaction id as soon as it has been created.

I tried using callback function, like:

contract.purchase(price, function(error, tx) {
if(error != null)
    return;
console.log(tx);
})

but I get error "invalid address"

I have tried using web3.eth.filter('pending') and web3.eth.filter('latest') but I only get the id after it is mined. I'm testing it in TestRpc.

1
  • Try contract.purchase.sendTransaction(price).then(function (tx) { console.log(tx); });. It should not wait but return immediately.
    – Ismael
    Commented Sep 12, 2017 at 2:40

1 Answer 1

10

I've tried with a sample contract

const tx = await MyContract.MyMethod.sendTransaction(MyParams);
console.log(`Tx: ${tx}`);

It will return immediately without waiting for the transaction to be mined. The difference is the method was not called as MyMethod(MyParams) but MyMethod.sendTransaction(MyParams).

2
  • That's good, but I'm looking for a way to keep waiting also for the receipt (after tx is mined). Is there any?
    – tomericco
    Commented Aug 12, 2018 at 15:54
  • 1
    @tomericco Web3 v1.0 has promiEvents that support waiting for the transaction to be mined and trigger an event 'transactionHash' when the hash is available. For truffle and web3 v0.20 once you have the transaction hash you can set a timer to periodically check for the transaction to be mined (internally truffle does exactly that).
    – Ismael
    Commented Aug 12, 2018 at 22:34

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.