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?
[Ethereum] find the pending transactions
etherscanexternal-apipending-transactionstransactionsweb3js
Related Solutions
Have a look here Understanding transactions better
Full nodes listen and broadcast transactions. As a full node you receive and can display the pending transactions in your transaction pool. That's what etherscan does. Maybe you can't access it through their API, their choice.
So you can get some of the pending transactions as a node, but you don't get to choose, and most likely some transactions will have been included in a block before they reach you in a pending state.
It looks to me as though this is a bit of an optimisation/edge case that is causing some confusion - that the node assumes that the network that you are connected to is functioning normally i.e. that blocks will be mined by rational actors.
Initially I was under the impression that the txpool is where all the pending transactions are stored on a node
This is correct.
...that the pending block is only present when a node is mining (i.e. the block that's being solved for)
This, specifically, is the part where I think the node assumes that it's part of a network that is or will be functioning correctly in the future.
To help clarify, we can run through some scenarios:
- In the case above: where there are fewer transactions (TXs) than could be expected to fit into a block (based on the gas limit) then it is safe to assume that which ever node goes on to mine the next block will put all of the TXs from the mempool into the block (after all, there's an economic incentive for the miner to maximise their profit by including as many TXs as possible). So the node can make an intelligent guess at what the next block will look like.
- In the case where there are more TXs in the mempool than would be able to fit into the block, then it's more difficult for predict which TXs will be included in the block (as reordering/modifying the list of TXs impacts the mining of the block), but it's probably safe to assume that any miner will be aiming to include the TXs with the largest fees to maximise their profit.
Could someone explain the difference between these two concepts in the context of pending transactions, the account nonce and pending transaction broadcasting?
To summarise and hopefully answer these questions:
Valid TXs (either created on this node or received from a Peer) will be added to the node's, local mempool until they are included in a block (whether that block is mined by this node or another on the network). At which point the TX are removed from the mempool and included in the block.
Since we can expect that most of the nodes are rational and behaving correctly; it's possible to make an intelligent guess as to which TXs (from the mempool) will be included in the next block.
As an account makes TXs, the account's nonce will increase to enforce the ordering of TXs for that account. Even if the past TXs have not been included in a block, the account can continue to make further valid TXs as long as the nonce is incremented. They can appear in the mempool and could be expected to appear in the next block - depending on gas fees, num TXs in the mempool etc.
New TXs that are submitted on a node will be broadcast to all or a random subset of its peers (depending on how the node is coded). The new, pending block will only be broadcast to a node's peers when it is mined by a mining node.
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
is0
then it was successful/ IfisError
is1
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.