[Ethereum] Get transaction id after contract function call

dapp-developmenttruffleweb3js

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.

Best Answer

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).

Related Topic