0

I'm trying to get a subscription to logs from a "target" address, but I wasn't able to make filters works. Subscribe method works without filter but I wasn't able to print a result without it, I get the Subscription ID so I'm connected but 0 logs after that. The transaction used to test is a simple eth transfer on goerli.

import Web3 from 'web3';

let web3 = new Web3(new Web3.providers.WebsocketProvider("wss://node-name.ethereum-goerli.discover.quiknode.pro/myKey/"));
web3.eth.subscribe('logs', {
    address: '0x0000000000000000000000000000000000000000'
}, function (error, result) {
    if (!error)
        console.log(result);
})
    .on("connected", function (subscriptionId) {
        console.log("SubsID", subscriptionId);
    })
    .on("data", function (log) {
        console.log(log);
    })
    .on("changed", function (log) {
    });

2 Answers 2

2

I think the issue is that you have to add the "topics" that you want to subscribe to as well, you can find the topic hash from the "logs" tab of any transaction on etherscan like this: https://etherscan.io/tx/0xde4c388f96aec17194a8ffa18393ab7eab956a73163ab2c870ce64efe3deea5a#eventlog.

Here is a code example if you're looking for the transfer topic:

web3.eth.subscribe('logs', {
    address: '0x0000000000000000000000000000000000000000',
    // enter array of topics to subscribe to:
    topics: [
      // topic hash for "transfer" 
      '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
      // topic inputs example for "transfer" event takes "to", "from", and "tokenId"
      ['0x0000000000000000000000000000000000000000', null, null],
      // this example will give you all transfers from zero address to any address by leaving "to" and "tokenId" as null
    ],
});

and here is a link to the docs if you need further clarification: https://web3js.readthedocs.io/en/v1.8.1/web3-eth-subscribe.html#id16

2
  • 1
    Thanks! that will solve all my issues ❤👍 Commented Nov 21, 2022 at 9:30
  • 1
    no problem Mentasuave Commented Nov 21, 2022 at 15:45
0

If you are trying to subscribe data from any etheruem address, you can use Bitquery's GraphQL subscription API (Websocket).

For example, in the following Websocket, we are subscribing to Calls, events, and transfers for a given address.

https://ide.bitquery.io/Subscribing-to-specific-calls-transfers-events-on-Ethereum

Disclosure: I work for Bitquery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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