[Ethereum] find the pending transactions

etherscanexternal-apipending-transactionstransactionsweb3js

I wanted to know where I can find the current pending transaction for an account which isnt committed. Is it even possible? How can I get transaction hashes for transactions which are pending? Any API for this purpose?

Best Answer

Well I know of one way that should work.

Use web3js or geth console and invoke whatever function/transaction. If you use the .sendTransaction() method it will return the transaction hash, which you will then use to look up your transaction status.

Next, using Etherscan API you can use the transaction endpoint:

https://etherscan.io/api?module=localchk&action=txexist&txhash=<<TX HASH>>

The response should look like this:

{"status":"1","message":"OK","result":"False"}

Next use the Etherscan transaction API. It will not tell you if it is pending, but if the transaction was a success/error it will return a different response:

https://api-rinkeby.etherscan.io/api?module=transaction&action=getstatus&txhash=<<TX HASH>>&apikey=<<API KEY>>

{"status":"1","message":"OK","result":{"isError":"0","errDescription":""}}

If isError is 0 then it was successful/ If isError is 1 then the transaction failed.

To sum it up, as long as you get the transaction hash from the .sendTransaction() (so you know it's a valid transaction), you can now hit the first endpoint with the ?action=txexists, which will return if the tx exists. Then you can hit the second endpoint to see if the TX passed or failed. So prior to you needing to hit the second endpoint, you will know the TX is pending since it does not exist yet.

If the above method is too confusing for you then below is how Etherscan.io currently gets pending transactions.

I pulled it from their website source code.

var interval;
var loopcounter = 1;

// startTxPendingCheck is a global window variable set by another script

if (startTxPendingCheck) {
    var div = document.getElementById('spinnerwait');
    div.style.display = 'block';
    interval = setTimeout(checkForConfirmedTx, 2000);
    function checkForConfirmedTx() {
        if (loopcounter < 45) {
            $.ajax({
                url: "/api?module=localchk&action=txexist&txhash=" + txHash,
                type: "GET",
                success: function(data) {
                    if (data.result == "True") {
                        window.location.href = "/tx/" + txHash;
                    }
                },
                dataType: "json"
            })
            loopcounter = loopcounter + 1;
            interval = setTimeout(checkForConfirmedTx, 20000);
        } else {
            stopInterval();
        }
    }
    function stopInterval() {
        console.log("stopInterval called");
        var div = document.getElementById('spinnerwait');
        div.style.display = 'none';
        clearTimeout(interval);
    }
    function startInterval() {
        console.log("startInterval called");
        clearTimeout(interval);
        var div = document.getElementById('spinnerwait');
        div.style.display = 'block';
        interval = setTimeout(checkForConfirmedTx, 5000);
    }
}
Related Topic