0

Using web3, I try to listen to events.PoolCreated

But I'm getting Error: connection not open on send()

on both infura and getblock RPC nodes, was anyone able to listen to this events using web3 with a websocket provider?

I checked the ABI, I copy pasted it from the contract, when I print out the contract events I get:

{
  FeeAmountEnabled: [Function: bound ],
  '0xc66a3fdf07232cdd185febcc6579d408c241b47ae2f9907d84be655141eeaecc': [Function: bound ],
  'FeeAmountEnabled(uint24,int24)': [Function: bound ],
  OwnerChanged: [Function: bound ],
  '0xb532073b38c83145e3e5135377a08bf9aab55bc0fd7c1179cd4fb995d2a5159c': [Function: bound ],
  'OwnerChanged(address,address)': [Function: bound ],
  PoolCreated: [Function: bound ],
  '0x783cca1c0412dd0d695e784568c96da2e9c22ff989357a2e8b1d9b2b4e6b7118': [Function: bound ],
  'PoolCreated(address,address,uint24,int24,address)': [Function: bound ],
  allEvents: [Function: bound ]
}

Which are the correct events,

What am I doing wrong? why does this happen?

5
  • looks like your client is disconnected
    – Nulik
    Jul 5, 2021 at 4:01
  • It disconnects... I'm asking how to fix it.
    – Ohad
    Jul 6, 2021 at 10:56
  • contact Infura and ask them. My own node never disconnects me
    – Nulik
    Jul 6, 2021 at 11:21
  • can you try to listed to this event and see if it works?
    – Ohad
    Jul 8, 2021 at 13:38
  • yes, i'm able to listen to these events Nov 17, 2021 at 12:15

2 Answers 2

1

Dev Advocate at Chainstack here.

As far as I know, Infura (and getblock), at this point, don't allow for subscriptions over WebSocket.

I don't wanna be the devil's advocate, but Chainstack allows for listening to events over WSS, and you can use this code in web3.js:

const Web3 = require("web3");
const NODE_URL =
  "YOUR_CHAINSTACK_WSS";
const web3 = new Web3(NODE_URL);

// Only log transfer events from the UniswapV3Factory smart contract.
const logsFilter = {
  address: "0x1F98431c8aD98523631AE4a59f267346ea31F984",
  topics: [
    "0x783cca1c0412dd0d695e784568c96da2e9c22ff989357a2e8b1d9b2b4e6b7118",
  ], // PoolCreated event
};

async function subscribeToLogs() {
  try {
    // Create a new subscription to the 'pendingTransactions' event
    const subscription = await web3.eth.subscribe("logs", logsFilter);

    // Attach event listeners to the subscription object
    subscription.on("connected", handleConnected);
    subscription.on("data", handleLogs);
    subscription.on("error", handleError);
  } catch (error) {
    console.error(`Error subscribing to new logs: ${error}`);
  }
}

/* Fallback functions to react to the different events */

// Event listener that logs a message when the subscription is connected
function handleConnected(subscriptionId) {
  console.log(`New subscription: ${subscriptionId}`);
}

// Event listener that logs the filtered events
async function handleLogs(logs) {
  console.log(logs);
}

// Event listener that logs any errors that occur
function handleError(error) {
  console.error(`Error receiving new blocks: ${error}`);
}

subscribeToLogs();

You can find more info on the web3.js subscriptions and how to get an endpoint in the docs!

subscribe("logs") | Ethereum

And feel free to reach out if you have any question!

0

The error "connection not open on send()" can occur when there is a problem with the WebSocket connection to the GetBlock RPC node.

Make sure you have the correct WebSocket provider URL and check your network connectivity.

Also, ensure that you are using the latest version of the Web3 library. If the issue persists, provide more details for further assistance.

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.