How to get pending transactions using Infura?

I used “etc_pendingTransactions” to get pending transactions, only to be informed : “The method eth_pendingTransactions does not exist/is not available”. Is there any API that I can use to get pending transactions?

Hi @sccyye we do not currently support this method, can you help us understand what you’re trying to do and we can possibly suggest an alternative?

Hello

I can maybe add to this as I have the same problem, the reason I am using it is because I am running a demo of a DAPP, essentially I have created a HD wallet using these different addresses as different participants in the smart contract. The problem ofcourse in doing this is that if I use a given address to send a token, and someone else from a different computer attempts to send a token with that same address, they will receive a revert error saying something like, ‘underpriced replacement transaction’. Instead as i load the data from a given address, I would like to know if the address has any pending transactions i should know of, to disable the send token button.

** Quick update **

Perhaps we don’t have to use Infura for this, I am currently working on the ropsten test network so i could just get this information from the ropsten etherscan api!
https://ropsten.etherscan.io/apis

It goes without saying that if there is a simpler web3 based version to achieve this I would appreciate learning about it.

Hi @Ignace_Loomans, welcome to the Infura community! There is a newPendingTransactions subscription that can be used for this purpose.

@Leiya_Kenney Hi Leiya. Is there any way to get the details of newPendingTransactions from their tx hashes. I am using the newPendingTransactions ws subscription which you linked above, which returns the tx hashes of pending transactions. However, when trying to get details of those transactions using web3.eth.getTransaction most of the time they return null. I’m not sure if this is because when using the infura url (which is acting as a proxy to different nodes) it is connecting to a separate node on the subsequent request which doesnt have details of that transaction in its local mempool.

Here’s my code - I am using the official web3 js library.

require('dotenv').config()

const Web3 = require("web3");

let web3 = new Web3(new Web3.providers.WebsocketProvider(`wss://mainnet.infura.io/ws/v3/${process.env.PROJECT_ID}`));

var subscription = web3.eth.subscribe('newPendingTransactions', function(error, result){})
.on("data", function(transaction){
    console.log(transaction);
    web3.eth.getTransaction(transaction).then(response => console.log(`${transaction}: ${JSON.stringify(response)}`));
});

and here is an snippet of the output i am getting (null when fetching details of the tx)

0x3f2e28f41b60b012716332112e0a3adc280470a3adbc0a3c1b1bc112dc5b582a: null
0x515a4aa24902306991532bc0d0a67299839a12dbf9d25e1ece7e47651b1bc192: null
0x165a07874e75507c98144272670669e3d2c22bd97052278689d64eba0ebb46d9: null
0xe8fd0048ebbafee3ad5641ffae0a60d577ded8b9f4f3d11bdc6e379376cea07d
0x001c446df1205756b45d4da19f7495426167a64f3adf58c682b81a809c5aa561
0x6e3f43276a3a4e9c0d11be4911d7563533be1be6e70314ec624b38ab0b581c1b
0x4b975026737f23f3d002834b7fcce37e3665dbf2e184c833a612d0ab7ec278fb: null
0x0b3d3781e1d8d550f64bb0be65594f17cc9867b39ab410339873254b37047df8: null
0x91cf43fe1978b634ea7be6162eb8282c6caef9dbfa578a551eea2715075dfd2a
0xf3540206106198a1f4186f766c6202b79e14a86506ee4927ab2cc6d8a4dcb2c4: null
0xa96eca74162a7b58e199ee93f7379724c0aef75df1d282a2247791717eea4675: null
0x6c78fc85128d55a53c85782cbe977964e15500b25a1452131ca4f231621de0ef
0x07bcbd7f52a905b21910697ebfe125ceee8fcc5ae71a04c6009a48ed1f11dd40: null
0x67aa777e97af0017e5e1dc841fc609cd2f393054ad86c0ddbc05586198ab9d10
0x954189a69faa5dc16a2cf864430e62e7ab475f09d299265ce8503470c21aad9b: null
0x922f6efe07d9a753b84f6b4d98fe81b565c83e4c0298e0c0813037ad70ef7a60
0xa1f2cd1f4bd674da1503cce659df89966fda8cd83b22144c5d64898245c8c7a3: null
0x1c0ace4368c365be2e7d98c45b9b50d8cf885d1a773079d6c9d48df5bd0ddf29
0x6d4ba3b1c172dccea3b50bb5fcc67ea431f1affe139fc1bbdbd53aaa546f13d9
0x4c60879460136af0f5efc89ef6bcbf3ba69d39820cd46aa1d9e24a935aa76f92: null
0x5818944e7cb5ca9c710b6e18825ed003f2918c896fb4f0a78af2a4849d2111c9: null

Hi @spiz - welcome to the Infura community, and thanks for your patience!

I would check out getContent for web3 here. This will return a list of pending as well as queued transactions both with transaction details. I believe this may be what you’re looking for!

Hi @Leiya_Kenney , I have the same question. Since getContent is a rpc call, is there a way to get the transaction details via the websocket instead of rpc call. I’m hoping to minimize the latency here. Do you have suggestions on this?

Hi @Lucas_Zhang - welcome to the Infura community! There are lots of ways to get pending transactions using websocket vs. rpc call - check this tutorial out. It shows you how to use web3js to access the Infura websocket and get pending transaction information.

Thanks @Leiya_Kenney for the suggestion! I have been using the same approach as suggested in the article:

subscription = web3.eth.subscribe('pendingTransactions', function (error, result) {})
    .on("data", function (transactionHash) {
        web3.eth.getTransaction(transactionHash)
        .then(function (transaction) {
            createNode(transaction.from, transaction.to);
        });
    })

However, for each transactionHash, it is using the rpc method web3.eth.getTransaction(transactionHash) to get the transaction details. This approach hits the rate limits quite quickly and each rpc call takes around 0.5 seconds for me. Is there a way to get details of pending transactions without having to use this rpc method?

Got it! You can subscribe to new pending transactions over the WebSocket, but it only returns the pending transaction hash, not the full body of the transaction. This is something we’re looking to add in the future but is not something we currently support. Hope that helps!

@Lucas_Zhang I’ve created an npm library for batching multiple requests and returning a promise. It may help you out: https://github.com/conspyrosy/web3-batch-request

1 Like