Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscribing to multiple blockchain events causes every listener to be fired for every registered event #6202

Closed
vekexasia opened this issue Jun 14, 2023 · 3 comments · Fixed by #6210
Assignees
Labels
4.x 4.0 related Bug Addressing a bug

Comments

@vekexasia
Copy link

Lets consider this code:

const headSubs = await this.web3.eth.subscribe('newHeads');

headSubs.on('data', async blockHead => {
  console.log('block', blockHead);
});

const pendingTxsSub = await this.web3.eth.subscribe('pendingTransactions');

pendingTxsSub.on('data', async(tx) => {
  console.log('tx', tx);
});

Expected behavior

I'd expect pendingTxsSub to spit just txs and headSubs to only console.log new block headers.

Actual behavior

block 0x787c1245c067b4943d9585e8021e4003a1ddd17d3e2588bc95a7b0a6332b38bd
tx 0x787c1245c067b4943d9585e8021e4003a1ddd17d3e2588bc95a7b0a6332b38bd
block 0x24ce987ea13f1ea88051167a7d888455cb658a6ffb852603b23032a6fd78b1c3
tx 0x24ce987ea13f1ea88051167a7d888455cb658a6ffb852603b23032a6fd78b1c3
block 0xdb893c7e70a6f82eef78caf0927030c39488e9fb5110542b904ede3864cc240b
tx 0xdb893c7e70a6f82eef78caf0927030c39488e9fb5110542b904ede3864cc240b
block 0xbae1bafb682d0f6f3ad6fc3506c57528f1d4a95bcaad6ab5c4b841a16eafdbe4
tx 0xbae1bafb682d0f6f3ad6fc3506c57528f1d4a95bcaad6ab5c4b841a16eafdbe4

Steps to reproduce the behavior

  1. Check code above
  2. Run
  3. Check console.log

Environment

Node 18.12.0, Web3 4.0.1

@Muhammad-Altabba Muhammad-Altabba added Needs Clarification Requires additional input 4.x 4.0 related labels Jun 14, 2023
@Muhammad-Altabba
Copy link
Contributor

Thanks @vekexasia for opening an issue.

What is the network you are connected to?

The output you are receiving is the expected one if you are connected to Ganach or dev network that creates a new block for every transaction.

So, try to connect to mainnet for example, and listen to events there. You will see the behaviour you are expecting.

@vekexasia
Copy link
Author

mainnet @Muhammad-Altabba

@Muhammad-Altabba
Copy link
Contributor

Thanks @vekexasia ,
I could verify the issue and we will work on it shortly. And feel free to open an MR to fix it if you like to.

And here is how to reproduce the issue:

const { Web3 } = require("web3");

async function main() {
  const web3 = new Web3("wss://mainnet.infura.io/ws/v3/YOUR_KEY");

  const headSubs = await web3.eth.subscribe("newHeads");

  headSubs.on("data", async (blockHead) => {
    console.log("block", blockHead);
  });

  const pendingTxsSub = await web3.eth.subscribe("pendingTransactions");

  pendingTxsSub.on("data", async (tx) => {
    console.log("tx", tx);
  });
}

main();

@Muhammad-Altabba Muhammad-Altabba added Bug Addressing a bug and removed Needs Clarification Requires additional input labels Jun 14, 2023
@Muhammad-Altabba Muhammad-Altabba self-assigned this Jun 15, 2023
@Muhammad-Altabba Muhammad-Altabba linked a pull request Jun 18, 2023 that will close this issue
17 tasks
@Muhammad-Altabba Muhammad-Altabba changed the title Subscribe to 2 events with single web3 instance seem to not work properly. Subscribing to multiple blockchain events causes the listeners to be fired for every registered event Jun 18, 2023
@Muhammad-Altabba Muhammad-Altabba changed the title Subscribing to multiple blockchain events causes the listeners to be fired for every registered event Subscribing to multiple blockchain events causes every listener to be fired for every registered event Jun 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.x 4.0 related Bug Addressing a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants