Filtering Stream Data from the bloXroute EVM API

bloXroute Team
4 min readNov 3, 2022

--

By: Chris Cacace, Technical Product Manager

bloXroute enables users to obtain important data faster than the competition. This is done by connecting to Streams. In this article, we will show how to connect to a data stream and how to use filters to get insight into specific on-chain events.

To follow along, first, follow our documentation to set up an account. You will need your authorization header in order to run these scripts. You may follow along with this article even on our free Introductory tier.

For this article, we will connect through bloXroute’s Cloud API to start receiving streams of data. Streams can also be accessed via a self-hosted Gateway for the lowest latency. Once we are connected, we will apply two basic filters from which many nuanced strategies could be executed.

Setting up the Stream

To begin, let’s set up the Stream using our Cloud API :

  1. Open a terminal/command prompt and connect with streams using this command. We will be using the “virginia” region in the United States. See the documentation for a full list of Cloud-API IPs and choose the endpoint that is optimally located for your own machine.

To begin a connection, run this command:

wscat -c wss://virginia.eth.blxrbdn.com/ws -H "Authorization: [YourAuthHeader]"

2. There are many available data streams available to bloXroute users depending on the need. We will be working with the newTxs stream as opposed to the pendingTxs stream because we want data the very instant it is picked up by the BDN.

To receive all streaming data, enter this command:

{"id": 1, "method": "subscribe", "params": ["newTxs", {"include": []}]}

3. You should receive a stream of all transactions as shown below:

Applying Filters

Filters enable us to only receive certain types of transactions based on a set of parameters from the streaming data. To learn more about filters, please see our documentation.

For our first filter, we will monitor any asset transfers to an Ethereum address. You might use this if you were interested in a specific’s wallet activity.

Open another terminal/command prompt and connect with streams using this command:

wscat -c wss://virginia.eth.blxrbdn.com/ws -H "Authorization: [YourAuthHeader]"

Next, we will create a filter to monitor any transfers to this example Ethereum address —

0x0d059dA360c63c2cdc5C7c9E6AC0e2eF9E3C1cB3

Enter the following command:

{"jsonrpc": "2.0", "id": 1, "method": "subscribe", "params": ["newTxs", {"include": ["tx_hash"], "filters": "{to} == '0x0d059dA360c63c2cdc5C7c9E6AC0e2eF9E3C1cB3'"}]}

3. Finally, we will send a small amount of Ethereum to this example address. The filter behaves as depicted below:

The resulting stream information is as follows:

{"method":"subscribe","params":{"subscription":"98753bbf-39d7-440b-ad38-b219821a82f0","result":{"txHash":"0x7ceb6369bdd86eb8a76b858c7ae192aec73e23d2c2b06939d7181a2b8336c566"}},"jsonrpc":"2.0"}

You can find more information about this ETH transfer here: https://etherscan.io/tx/0x7ceb6369bdd86eb8a76b858c7ae192aec73e23d2c2b06939d7181a2b8336c566

This was a filter to collect data on a very basic transaction, but you can create more advanced filters that will monitor the execution of any Ethereum, BNB, or Polygon smart contract interaction.

For our second filter, we will monitor the execution of an Ethereum smart contract. Each smart contract is made up of functions which can be identified by their methodID. We will use bloXroute’s filter to get specific data by filtering based on the methodID.

Let’s look at an NFT minting contract — 0x6c311baD20A8752f8318a9352B24629430535688.

Observing the contract at https://etherscan.io/address/0x6c311bad20a8752f8318a9352b24629430535688#writeContract, we can see 31 distinct contract functions each with their own methodID.

Specifically, we can see the functions:

1. addKeyGranter  0x564aa99d
...
13. Purchase 0x3f33133a

Using filters, we would be able to monitor any activity on this smart contract. For simplicity’s sake, let’s monitor the first function — “addKeyGranter”.

To do this:

  1. Open another terminal/command prompt and connect with Streams using this command
wscat -c wss://virginia.eth.blxrbdn.com/ws -H "Authorization: [YourAuthHeader]"

2. Next, we will create a filter to monitor the contract interactions through this methodID — 0x564aa99d

{"jsonrpc": "2.0", "id": 1, "method": "subscribe", "params": ["newTxs", {"include": ["tx_hash"], "filters": "{method_id} == 0x564aa99d"}]}

3. Finally, we will run that “addKeyGranter” function through the etherscan GUI. The filter behaves as depicted below:

The resulting stream information is as follows:

{"method":"subscribe","params":{"subscription":"d64ac077-7383-4152-b423-154e98191d74","result":{"txHash":"0x97d3200a6c3959b124f6672b6b6503533f132505ac6987605d17cde1b3310306"}},"jsonrpc":"2.0"}

You can find more information about this contract interaction here: https://etherscan.io/tx/0x97d3200a6c3959b124f6672b6b6503533f132505ac6987605d17cde1b3310306

With this level of flexibility, literally, any on-chain activity can be tracked and responded to based on a set of parameters.

Moving forward with Streams and Filters

The bloXroute Blockchain Distribution Network (BDN) allows users to get streaming data faster than the competition. Setting up Streams and applying Filters is the first step in implementing strategies for traders or for the monitoring of any on-chain activity.

--

--

bloXroute Team
bloXroute Team

Written by bloXroute Team

Scaling blockchains to thousands of on-chain transactions per second. Today.

No responses yet