1

I am new to the world of web3 and trying to learn how to listen for events. I've been googling and reading for a while and can't seem to get something working. I'm able to subscribe to pendingTransactions and newBlockHeader but can't seem to get logs to work. Below is the closest I have gotten.

Right now the goal is to listen for logs at a specific address. Any help would be appreciated and an answer leading to a solution will garner an award.

const Web3 = require('web3');

const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws'));
var subscription = web3.eth.subscribe('logs', {
    address: '0x1E0447b19BB6EcFdAe1e4AE1694b0C3659614e4e',
    fromBlock : 7500000,
}, function (error, result) {
    if (!error)
        console.log(result);
})
    .on("data", function (log) {
        console.log(log);
    })
    .on("changed", function (log) {
    });

// unsubscribes the subscription
subscription.unsubscribe(function (error, success) {
    if (success)
        console.log('Successfully unsubscribed!');
});

returns: (node:17575) UnhandledPromiseRejectionWarning: Error: Provider error: Subscription with ID null does not exist.

1

In the code you provided, you create the subscription, then immediately try to unsubscribe and get an exception. More than likely that's because an async process in the form of a promise hadn't completed, and when it did, you'd already unsubscribed.

Regardless, that's not what you really want to do anyway. You want to subscribe and get some data, right?

So, if you remove that subscription.unsubscribe block and run it, you'll get something like the following:

Error: Node error: {"code":-32005,"message":"query returned more than 10000 results"}
at Function.validate (/Users/cliff/Documents/web3-subscribe/node_modules/web3-providers/dist/web3-providers.cjs.js:114:18)

This is coming from your callback. It's telling you you got beaucoup results; too many actually. Etherscan tells me that block was mined:

87 days 16 hrs ago (Apr-04-2019 06:08:57 AM +UTC)

So, let's pick a fresher block. I see that 8060000 was mined just:

7 hrs 39 mins ago (Jun-30-2019 03:19:12 PM +UTC)

So I drop that into the fromBlock option and voila:

{ address: '0x1E0447b19BB6EcFdAe1e4AE1694b0C3659614e4e',
  blockHash: '0x85f9ddae65c8ccbff59585ad59ac6b0c753d2481eb9af8cd1a4f1a2c85ab7aaf',
  blockNumber: 8060013,
  data: '0x000000000000000000000000a8b39829ce2246f89b31c013b8cde15506fb9a76',
  logIndex: 107,
  removed: false,
  topics: 
   [ '0x91b01baeee3a24b590d112613814d86801005c7ef9353e7fc1eaeaf33ccf83b0' ],
  transactionHash: '0xe1f20cdc9a64c695b76cf0f5ae9440a41c451c89d7d387866a70d24aeeaab821',
  transactionIndex: 141,
  id: 'log_0x17ed98b92ffa29cf131111e89823af125901166111d43417507d4e101e08153b' }
{ address: '0x1E0447b19BB6EcFdAe1e4AE1694b0C3659614e4e',
  blockHash: '0x85f9ddae65c8ccbff59585ad59ac6b0c753d2481eb9af8cd1a4f1a2c85ab7aaf',
  blockNumber: 8060013,
  data: '0x000000000000000000000000a8b39829ce2246f89b31c013b8cde15506fb9a76',
  logIndex: 107,
  removed: false,
  topics: 
   [ '0x91b01baeee3a24b590d112613814d86801005c7ef9353e7fc1eaeaf33ccf83b0' ],
  transactionHash: '0xe1f20cdc9a64c695b76cf0f5ae9440a41c451c89d7d387866a70d24aeeaab821',
  transactionIndex: 141,
  id: 'log_0x17ed98b92ffa29cf131111e89823af125901166111d43417507d4e101e08153b' }

.
.
.

{ address: '0x1E0447b19BB6EcFdAe1e4AE1694b0C3659614e4e',
  blockHash: '0x59f6e3509b5ce466db760266342b742ad76f049081e5eb0a1a7db96de4b8ed19',
  blockNumber: 8062016,
  data: '0x2f1fb51f7edffae129489b1a765aab19877c9282615f8e792bb87726b8097a2a0000000000000000000000000ece224fbc24d40b446c6a94a142dc41fae76f2d',
  logIndex: 34,
  removed: false,
  topics: 
   [ '0xab38cdc4a831ebe6542bf277d36b65dbc5c66a4d03ec6cf56ac38de05dc30098',
     '0x00000000000000000000000071f35825a3b1528859dfa1a64b24242bc0d12990' ],
  transactionHash: '0x9cfd31b7e57843365664e9841daaaa3d841b073a686dd94d1a171abc2ad62d26',
  transactionIndex: 11,
  id: 'log_0x5a6e7afc1511be62f321d2af5663aea42321e33253c1f3a49f1d693a085558c8' }
{ address: '0x1E0447b19BB6EcFdAe1e4AE1694b0C3659614e4e',
  blockHash: '0x59f6e3509b5ce466db760266342b742ad76f049081e5eb0a1a7db96de4b8ed19',
  blockNumber: 8062016,
  data: '0x2f1fb51f7edffae129489b1a765aab19877c9282615f8e792bb87726b8097a2a0000000000000000000000000ece224fbc24d40b446c6a94a142dc41fae76f2d',
  logIndex: 34,
  removed: false,
  topics: 
   [ '0xab38cdc4a831ebe6542bf277d36b65dbc5c66a4d03ec6cf56ac38de05dc30098',
     '0x00000000000000000000000071f35825a3b1528859dfa1a64b24242bc0d12990' ],
  transactionHash: '0x9cfd31b7e57843365664e9841daaaa3d841b073a686dd94d1a171abc2ad62d26',
  transactionIndex: 11,
  id: 'log_0x5a6e7afc1511be62f321d2af5663aea42321e33253c1f3a49f1d693a085558c8' }

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.