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

2 Answers 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, 2018 at 22:13
  • 2
    This doesn't work with the 1.0 API
    – Shamoon
    May 10, 2018 at 19:54
5

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 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.