Help with websockets

Hi, I am trying to listen for all transaction on an address using the websocket provided. But when I did a test transaction to check for the functionality, it didn’t work and also got no errors. Can someone help with what am doing wrong? Below is the code I used to listen for log events in node js.

const Web3 = require("web3");

let web3 = new Web3(
    new Web3.providers.WebsocketProvider(
        "wss://kovan.infura.io/ws/v3/ID"
    )
);

var subscription = web3.eth
    .subscribe(
        "logs",
        {
            address: "some example address",
        },
        (error, result) => {
            if (!error) console.log("Result - " + result);
        }
    )
    .on("data", (log) => console.log("Data - " + log))
    .on("changed", (log) => console.log("Changed - " + log))
    .on("connected", (log) => console.log("Subscription connected!"));

The connected event fires up on starting the server though.

Hi @Mano, unfortunately the logs sub type for eth_subscribe doesn’t return transactions from an address. Can you try the newPendingTransactions sub type and let us know if that helps? This will report back pending transactions, for mined transactions you will need to inspect the block.

1 Like

Oh, I see. Thanks @mike. I will try your suggestion and return back for any more questions.

The pendingTransactions event works but it doesn’t seem to have a filter parameter for address. How can I filter for a specific address? @mike

I modified the code to console log only the from address I specified, but it’s not picking up the pending transactions. Any ideas? @mike

var subscription = web3.eth
    .subscribe(
        "pendingTransactions",
        async (error, result) => {
            if (!error) {
                const trx = await web3.eth.getTransaction(result);
                if (trx) {
                    if (
                        trx.from ===
                        "some address"
                    )
                        console.log(trx.from);
                }
            }
        }
    )
    .on("connected", (log) => console.log("Subscription connected!"));

Hi @Mano I’d like to discuss your use-case with our team and get back to you. Will respond soon with some suggestions.

Hi @Mano are you trying to track contract transactions?

Hi @mike Yes. It’s a DAO contract.

Ok great, have you tried https://docs.ethers.io/v5/api/contract/contract/#Contract-queryFilter

Nope and am only using web3 with infura. Also, I was not using a contract address for testing, it was just an EOA @mike

Hi @Mano - this github issue has some ideas for you to try out using web3 for both past and future transactions. Let us know if that helps!

Hi @Leiya_Kenney Thanks for the reference. But I couldn’t find stuff that might help. It would be nice if there’s an websocket that listens for incoming transactions of an address.

Hi @Mano, I would try using something similar to the code found on this thread, but with using ‘pendingTransactions’ instead of ‘logs’ and entering the correct address where that fits in. Let us know how that works out for you.

Hi @Leiya_Kenney, Sorry for the delayed response. I tried the method you suggested but the pendingTransactions doesn’t seem to accept parameters to filter by address like logs, so it returned an error.

Hi @Mano - you’re absolutely correct; sorry about that! I believe this article may help you. It’s a tutorial on checking for transactions to a certain address using web3.js. I believe this is similar to what you’re trying to do, and definitely syncs up with this being much more difficult than it seems at first! Let me know if this helps.