0

I need to store the transaction details to my database. And here is my code:

    var rawTx = {
        nonce: nonce,
        gas: estimate_gas,
        to: contract_add,
        value: '0x0',
        data: dataField,
        chainId: 4
    };
    web3.eth.accounts.signTransaction(rawTx, privateKey, function (err, hash) {
        if (err) {
              // Store err to database here
                 ...
        }
        else {
            web3.eth.sendSignedTransaction(hash.rawTransaction)
                .then(res => {
                    console.log("DONE SENDING");
                    // Save res to Database here
                    ...
                })
                .catch(error => {
                    console.log("FAILED SENDING");
                    // Save error to Database here
                    ...
                });
        }
    });
} catch (err) {
      // Save err to Database here
         ...
}

The code is works for me, except when sendSignedTransaction catch an error that transaction was not mined in 750second. I know the transaction is still processing even this timeout error occurs.

But the critical problem here is further transactions will getting stuck in the pending, and get the same timeout error in my code. These transaction need to wait in pending until the previous transactions success.

After the first transaction success, geth will continue process the pending transactions in node, but execute them without go through my code above, so I can't store the transaction result into my database now.

Is there any solution for this? Or there is some callback in geth when a transaction done so I could use this callback to store the transaction result?

1

1 Answer 1

1

If you are using web3js 1.0, you can fix it quiet easily. In web3js 1.0, you can listen to the event of the transaction got mined into the block. So after that you can send next transaction based on the success status of the mined transaction. This success status you can get from the transaction receipt of that particular transaction. But, point to be noted that before getting mined in the blockchain, you will get null as transaction receipt if you explicitly call api to get transaction receipt.

The way of tx sending you can follow according to the given pic. There, in on receipt received, you will send your next transaction. The same process will be triggered for the next transactions.

To get further explanation, go through the link below https://web3js.readthedocs.io/en/v1.2.1/web3-eth.html#id64enter image description here

Your Answer

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

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