eth_subscribe("newPendingTransactions") | Ethereum

Ethereum API method that allows developers to receive real-time notifications regarding new pending transactions on the Ethereum blockchain. The application will receive notifications whenever new pending transactions are identified.

Parameters

  • string β€” keyword identifying the type of event to subscribe to, newPendingTransactions in this case.
  • boolean β€” if true, it returns the detail of each transaction. If false, only the hashes of the transactions.

Response

  • subscription β€” the subscription ID.
  • object β€” a transaction response object:
    • blockHash β€” the block hash. Identifies the block in which the transaction was included. This field is null for transactions that have not yet been included in a block.
    • blockNumber β€” the number of the block in which the transaction was included. This field is null for transactions that have not yet been included in a block.
    • from β€” the address of the sender who initiated the transaction.
    • gas β€” the units of gas included in the transaction by the sender.
    • gasPrice β€” the price of gas in Wei included in the transaction by the sender.
    • maxFeePerGas β€” the maximum amount the sender of the transaction is willing to pay per unit of gas for the transaction to be executed.
    • maxPriorityFeePerGas β€” the maximum priority fee the sender of the transaction is willing to pay per unit of gas.
    • hash β€” the hash that uniquely identifies the transaction.
    • input β€” the optional input data sent with the transaction, usually used to interact with smart contracts.
    • nonce β€” a counter identifying the transaction's number sent by the sender wallet. It essentially identifies how many transactions an account has made. Used to ensure each transaction is executed only once.
    • to β€” the address of the recipient of the transaction if it was a transaction to an address. For contract creation transactions, this field is null.
    • transactionIndex β€” the index of the transaction within the block. It is null for transactions that have not yet been included in a block.
    • value β€” the value of the native token transferred along with the transaction, in Wei.
    • type β€” the type of the transaction. 0 indicates a regular transfer; 2 indicates a contract creation or smart contract function call.
    • accessList β€” a list of authorized addresses and storage keys the transaction plans to interact with.
    • v β€” the recovery parameter in the Ethereum Signature Algorithm (ECDSA).
    • r β€” the first component of the signature in the Ethereum Signature Algorithm (ECDSA).
    • s β€” the second component of the signature in the Ethereum Signature Algorithm (ECDSA).

eth_subscribe("newPendingTransactions") 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)

> {"id":1,"jsonrpc":"2.0","method":"eth_subscribe","params":["newPendingTransactions", true]}
const WebSocket = require('ws');

const webSocket = new WebSocket('CHAINSTACK_WSS_URL');

async function subscribeToNewPendingTransactions() {
  
  const request = {
    id: 1,
    jsonrpc: '2.0',
    method: 'eth_subscribe',
    params: ['newPendingTransactions', true],
  };

  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);
  }
}

subscribeToNewPendingTransactions();

This will generate a continuous stream of data displaying new pending transactions as they are added to the mempool.

Use eth_unsubscribe to remove the subscription.

Use case

The eth_subscribe("newPendingTransactions") method in Ethereum is used to subscribe to the network and receive real-time updates on all new pending transactions on the network. This can be useful in a variety of real-world scenarios, including:

  • Trading applications. A trading application can use this method to keep track of any new transactions that are being made on the network. This can be particularly useful for high-frequency trading, where speed is crucial. By subscribing to this method, the trading application can receive real-time updates on new trades, allowing it to quickly respond and execute trades.

  • Monitoring tools. Monitoring tools can use this method to keep track of any suspicious activity on the network. By subscribing to this method, monitoring tools can receive real-time updates on any new pending transactions and quickly take action if necessary.

  • Payment processors. Payment processors can use the eth_subscribe("newPendingTransactions") method to monitor incoming transactions from their customers. This can be particularly useful for businesses that rely on blockchain technology to process payments, as it allows them to track incoming payments in real time. By subscribing to this method, payment processors can receive instant notifications when new payments are received, which can help streamline their payment processing workflows.