newTxs and pendingTxs

Name: newTxs , pendingTxs

Options

Key

Description

Values

include

Fields to include in the transaction stream. The subscription plan determines the list of available fields.

tx_hash,tx_contents[Default: all]

duplicates

Whether or not to include transactions

already published in the feed.

True,False[Default]

include_from_blockchain

Whether or not to include transactions received first from the connected blockchain node. (Mainly used for testing)

True[Default],False

filters

You can specify filters in SQL-Like format to only receive certain transactions.

Users can customize the filters.

The BDN supports subscribing to two transaction streams:

  1. newTxs is a stream of all new transactions as they are propagated in the BDN.

  2. pendingTxs is a stream of all new transactions as they enter the Ethereum TxPool.

For expedience, all transactions received through the BDN are immediately published to the newTxs feed. By design, the Gateway/Cloud-API do not perform the same detail of transaction validation that the Ethereum nodes do, and cannot completely guarantee that all transactions propagated are valid (e.g. the Gateway/Cloud-API do not check for double spends). Therefore, these transactions have had basic validations done (e.g. checksums and other sanity checks) but may not be accepted into the TxPool.

The Gateway/Cloud-API can then leverage the Ethereum nodes for further validation of the transaction (e.g. check that it will be accepted into the TxPool), and publish results to the pendingTxs feed. Users planning to use pendingTxs with a Gateway feed should enable validation against their local Ethereum node.

It is expected that newTxsstream will perform faster than pendingTxs. The performance difference can be significant (10-100ms). Users interested in timely transaction information could find newTxs stream more appealing, while those who rely on strict correctness of the transactions data should utilize the pendingTxsstream.

The newTxs stream can send transactions that have been (a) previously confirmed hours or days prior, or (b) replaced by a higher priced transaction with the same nonce. It is recommended that users with latency sensitive applications track the latest nonce for each account and use it to filter out stale messages.

The fields allowed in the include section depends on the user's subscription plan:

Plan

Available Fields

Introductory

tx_hash

Developer

tx_hash, tx_contents.from, tx_contents.to, tx_contents.value, tx_contents.nonce, tx_contents.gas, tx_contents.gas_price

Professional, Enterprise

tx_hash, tx_contents.input, tx_contents.v, tx_contents.r, tx_contents.s, tx_contents.from, tx_contents.to, tx_contents.value, tx_contents.nonce, tx_contents.gas, tx_contents.gas_price

The transaction feed will publish the data for each transaction in a separate message. Users that are only interested in a subset of transactions can utilize the feed's filtering options.

Examples

Requests (Cloud-API)

Notes:

  • Follow the examples below based on your subscription plan. We suggest you to use "try...catch" to better handle exceptions caused by potential disconnections. The examples below contain the minimum code required for all transactions stream subscriptions.

  • Enterprise plan users can choose to work directly with wss://eth.feed.blxrbdn.com:28333 for the best performance.

  • Non-Enterprise plan users should use wss://api.blxrbdn.com/ws.

  • See Cloud-API IPs to work directly with IP ( wss://<IP>/ws)

​

Python
Node.js
Golang
wscat
Python
from bloxroute_cli.provider.cloud_wss_provider import CloudWssProvider
​
# Enterprise users can follow line 4-6 to use wss://eth.feed.blxrbdn.com:28333
async with CloudWssProvider(
ssl_dir="/usr/bloxroute/certificate/external_gateway/registration_only"
) as ws:
​
# Non Enterprise users should follow line 9-12 to use wss://api.blxrbdn.com/ws
# async with WsProvider(
# uri="wss://api.blxrbdn.com/ws",
# headers={"Authorization": <YOUR-AUTHORIZATION-HEADER>}
# ) as ws:
​
subscription_id = await ws.subscribe("newTxs", {"include": ["tx_hash"]})
while True:
next_notification = await ws.get_next_subscription_notification_by_id(subscription_id)
print(next_notification) # or process it generally
await ws.unsubscribe(subscription_id)
Node.js
var fs = require('fs');
const WebSocket = require('ws');
​
// Enterprise users can follow line 5-12 to use wss://eth.feed.blxrbdn.com:28333
const ws = new WebSocket(
'wss://eth.feed.blxrbdn.com:28333',
{
cert: fs.readFileSync('/usr/bloxroute/certificate/external_gateway/registration_only/external_gateway_cert.pem'),
key: fs.readFileSync('/usr/bloxroute/certificate/external_gateway/registration_only/external_gateway_key.pem'),
rejectUnauthorized: false,
}
);
​
// Non Enterprise users should follow line 15-23 to use wss://api.blxrbdn.com/ws
// const ws = new WebSocket(
// "wss://api.blxrbdn.com/ws",
// {
// headers: {
// "Authorization" : <YOUR-AUTHORIZATION-HEADER>
// },
// rejectUnauthorized: false,
// }
// );
​
function proceed() {
ws.send(`{"jsonrpc": "2.0", "id": 1, "method": "subscribe", "params": ["newTxs", {"include": ["tx_hash"]}]}`);
}
​
​
function handle(nextNotification) {
console.log(nextNotification.toString()); // or process it generally
}
​
ws.on('open', proceed);
ws.on('message', handle);
​
Golang
package main
​
import (
"crypto/tls"
"fmt"
"github.com/gorilla/websocket"
"net/http"
)
​
func main() {
// Enterprise users can follow line 12-27 to use wss://eth.feed.blxrbdn.com:28333
cert, err := tls.LoadX509KeyPair(
"/usr/bloxroute/certificate/external_gateway/registration_only/external_gateway_cert.pem",
"/usr/bloxroute/certificate/external_gateway/registration_only/external_gateway_key.pem",
)
if err != nil {
fmt.Println(err)
return
}
​
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: true,
}
dialer := websocket.DefaultDialer
dialer.TLSClientConfig = tlsConfig
wsSubscriber, _, err := dialer.Dial("wss://eth.feed.blxrbdn.com:28333", nil)
// Non Enterprise users should follow line 30-35 to use wss://api.blxrbdn.com/ws
// tlsConfig := &tls.Config{
// InsecureSkipVerify: true,
// }
// dialer := websocket.DefaultDialer
// dialer.TLSClientConfig = tlsConfig
// wsSubscriber, _, err := dialer.Dial("wss://api.blxrbdn.com/ws", http.Header{"Authorization": []string{<YOUR-AUTHORIZATION-HEADER>}})
​
if err != nil {
fmt.Println(err)
return
}
​
subRequst := `{"id": 1, "method": "subscribe", "params": ["newTxs", {"include": ["tx_hash"]}]}`
err = wsSubscriber.WriteMessage(websocket.TextMessage, []byte(subRequst))
if err != nil {
fmt.Println(err)
return
}
​
for {
_, nextNotification, err := wsSubscriber.ReadMessage()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(nextNotification)) // or process it generally
}
}
​
wscat
# Enterprise users can follow line 2-4 to use wss://eth.feed.blxrbdn.com:28333
wscat -c wss://eth.feed.blxrbdn.com:28333 --no-check --cert /usr/bloxroute/certificate/external_gateway/registration_only/external_gateway_cert.pem --key /usr/bloxroute/certificate/external_gateway/registration_only/external_gateway_key.pem
> {"id": 1, "method": "subscribe", "params": ["newTxs", {"include": ["tx_hash"]}]}
< ......
​
# Non Enterprise users should follow line 7-9 to use wss://api.blxrbdn.com/ws
# wscat -c wss://api.blxrbdn.com/ws --header "Authorization: <YOUR-AUTHORIZATION-HEADER>"
# > {"id": 1, "method": "subscribe", "params": ["newTxs", {"include": ["tx_hash"]}]}
# < ......
​

Requests (Gateway-API)

Notes:

  • We assume that the Gateway IP is 127.0.0.1 with default ws port 28333 in the examples below.

Python
Node.js
Golang
wscat
Python
from bloxroute_cli.provider.ws_provider import WsProvider
ws_uri = "ws://127.0.0.1:28333"
while True:
try:
async with WsProvider(ws_uri) as ws:
subscription_id = await ws.subscribe("newTxs", {"include": ["tx_hash"]})
while True:
next_notification = await ws.get_next_subscription_notification_by_id(subscription_id)
print(next_notification) # or process it generally
except Exception as e:
print(f"Connection broken to feed, {str(e)}, retrying.")
await ws.unsubscribe(subscription_id)
​
Node.js
const WebSocket = require('ws');
​
const ws = new WebSocket(
'ws://127.0.0.1:28333',
);
​
​
function proceed() {
ws.send(`{"jsonrpc": "2.0", "id": 1, "method": "subscribe", "params": ["newTxs", {"include": ["tx_hash"]}]}`);
}
​
​
function handle(nextNotification) {
console.log(nextNotification.toString()); // or process it generally
}
​
ws.on('open', proceed);
ws.on('message', handle);
​
Golang
package main
​
import (
"fmt"
"github.com/gorilla/websocket"
)
​
func main() {
dialer := websocket.DefaultDialer
wsSubscriber, _, err := dialer.Dial("ws://127.0.0.1:28333", nil)
if err != nil {
fmt.Println(err)
return
}
​
subRequst := `{"id": 1, "method": "subscribe", "params": ["newTxs", {"include": ["tx_hash"]}]}`
err = wsSubscriber.WriteMessage(websocket.TextMessage, []byte(subRequst))
if err != nil {
fmt.Println(err)
return
}
​
for {
_, nextNotification, err := wsSubscriber.ReadMessage()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(nextNotification)) // or process it generally
}
}
​
wscat
wscat -c ws://127.0.0.1:28333
> {"id": 1, "method": "subscribe", "params": ["newTxs", {"include": ["tx_hash"]}]}
< ......
​

​

Response (Tx Events)

Transaction Event
Transaction Event
# This is an Example for Professional and Enterprise plan:
<<< {"jsonrpc": "2.0", "id": null, "method": "subscribe",
"params": {"subscription": "909e4bae-2c48-43f3-a007-f17d4c8a3ce8",
"result": {"txHash": "0x14f3...f3de",
"txContents": { "from": "0xa7a78...d3849e",
"gas": "0x5208",
"gasPrice": "0x12a05f2000",
"hash": "0x14f3d...4f3de",
"input": "0xef300...107a79e915",
"nonce": "0x1a",
"to": "0x2a0c0d...0c208",
"value": "0x0",
"v": "0x26",
"r": "0x56f7c...38c3d27",
"s": "0x46ef6...dc0cc"}}}}
​