eth_subscribe("logs") | Polygon

Polygon API method that allows developers to subscribe to real-time updates about new event logs on the Polygon blockchain. The application will receive notifications whenever new logs matching the filter are emitted.

Parameters

  • string β€” keyword identifying the type of event to subscribe to, logs in this case.

  • object β€” (optional) the event filter options:

    • address β€” the contract address from which the logs should be fetched. It can be a single address or an array of addresses.

    • topics β€” an array of DATA topics. The event topics for which the logs should be fetched. It can be a single topic or an array of topics.

      πŸ“˜

      Read Tracking some Bored Apes: The Ethereum event logs tutorial to learn more about topics and event logs.

Response

  • subscription β€” the subscription ID.
  • array β€” an array of log objects that match the specified filter:
    • address β€” the contract address from which the event originated.
    • topics β€” an array of 32-byte data fields containing indexed event parameters.
    • data β€” the non-indexed data that was emitted along with the event.
    • blocknumber β€” the block number in which the event was included. null if it is pending.
    • transactionhash β€” the hash of the transaction that triggered the event. null if pending.
    • transactionindex β€” the integer index of the transaction within the block's list of transactions. null if it is pending.
    • blockhash β€” the hash of the block in which the event was included. null if it is pending.
    • logindex β€” the integer identifying the index of the event within the block's list of events. null if pending.
    • removed β€” the boolean value indicating if the event was removed from the blockchain due to a chain reorganization. True if the log was removed. False if it is a valid log.

eth_subscribe("logs") code examples

πŸ“˜

Note that subscriptions require a WebSocket connection and WebSocket cat for you to use this method in the console.

Install WebSocket cat with:

npm install -g wscat

$ wscat -c YOUR_CHAINSTACK_WEBSOCKET_ENDPOINT
# Wait for the connection to be established

Connected (press CTRL+C to quit)

> {"jsonrpc":"2.0","id": 1, "method": "eth_subscribe", "params": ["logs", {"address":"0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]}]}
const WebSocket = require('ws');

const webSocket = new WebSocket('CHAINSTACK_WSS_URL');

async function subscribeToNewLogs() {
  
  const request = {
    id: 1,
    jsonrpc: '2.0',
    method: 'eth_subscribe',
    params: ['logs', {"address":"0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]}],
  };

  const onOpen = (event) => {
    webSocket.send(JSON.stringify(request));
  };

  const onMessage = (event) => {
    const response = JSON.parse(event.data);
    console.log(response);
  };

  try {
    webSocket.addEventListener('open', onOpen);
    webSocket.addEventListener('message', onMessage);
  } catch (error) {
    console.error(error);
  }
}

subscribeToNewLogs();

This will generate a continuous stream of data displaying new event logs matching the filter.

Use eth_unsubscribe to remove the subscription.

Use case

The eth_subscribe("logs") method in Polygon is used to subscribe to the logs generated by smart contracts on the blockchain. These logs contain information about events that have occurred on the blockchain, such as details of a smart contract function and outputs of transactions.

A practical and real-world scenario for using the eth_subscribe("logs") method could be in the context of a decentralized exchange (DEX). In the context of a DEX, trades are executed using smart contracts that run on the Polygon blockchain.

When a trade is executed on a DEX, a smart contract is called to process the transaction. This smart contract generates a log that contains information about the trade, such as the amount of cryptocurrency being traded, the parties involved, and so on.

By using the eth_subscribe("logs") method, a user or application can subscribe to the logs generated by the DEX's smart contracts. This can be useful for tracking trades in real time, monitoring the performance of the DEX, and generating reports on trading activity.

For example, a trader might use the eth_subscribe("logs") method to monitor the logs generated by a DEX in order to identify trends in trading activity. They might look for patterns in the volume and price of trades, which could help them make more informed trading decisions.

Similarly, an analytics platform might use the eth_subscribe("logs") method to collect data on trading activity across multiple DEXs. By analyzing this data, they could generate insights into the performance of different DEXs, which could be useful for investors and traders looking to make informed decisions about where to trade.