5

I would like to verify if somebody send ETH to a particular address. How can I get the list of incoming (pending) transactions with web3 ?

I tried the example in the web3 documentation with subscribe(“pendingTransactions”):

http://web3js.readthedocs.io/en/1.0/web3-eth-subscribe.html#id7

var subscription = web3.eth.subscribe('pendingTransactions', function(error, result){
    if (!error)
        console.log(transaction);
})
.on("data", function(transaction){
});

But I get the following error: TypeError: web3.eth.subscribe is not a function

Is there a way to get that list ?

2
7

There is a filter function in web3 javascript API and you can get pending txs by it. A simple print script as follows:

web3.eth.filter("pending").watch(
    function(error,result){
        if (!error) {
            console.log(result);
        }
    }
)

Hope it helps~

2
  • 2
    Great works! Is it however possible to limit this to only pending transactions on a specific contract address? – riper Mar 20 '18 at 22:13
  • 2
    This doesn't work with the 1.0 API – Shamoon May 10 '18 at 19:54
3

You have to create web3 with websocket provider given by full-nodes. For example,

const webSocketProvider = new Web3.providers.WebsocketProvider('ws://127.0.0.1:8546');
const web3Ws = new Web3(webSocketProvider);

In this example, I create a web3 with websocket provider listening to full-nodes websocket on my localhost. After that, web3.eth.subscribe will be valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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