WebSocket API
Connect to the Blocknative service over WebSocket protocol
Creating a Socket Connection
A WebSocket connection should be created with the Blocknative service at wss://api.blocknative.com/v0
. A successful connection will receive a response object that contains a status parameter with a value of 'ok'
.
Required Parameters
The following parameters are required for all messages that are sent to the Blocknative service:
parameter | type | description |
| string | an ISO datetime string formatted |
| string | your API key |
| string | a version string usually used by the Blocknative SDK, but any version string can be used |
| object | object specifying a the |
| string | blockchain name, valid value is |
| string | blockchain network, valid values for support systems are: Ethereum (and EVM compatible) - |
Initialization
Once a connection has been created with the Blocknative WebSocket service, an initialization message must be sent before any other messages so that the API key can be validated.
You API Key is created/managed from the Mempool Explorer account management page at https://explorer.blocknative.com/account. Note that API Key is called dappId
in the Blocknative websocket protocol.
Subscriptions
You can subscribe to receive transaction state change events by either watching a transaction hash/id or an address.
Ethereum Transaction Hash
Address (Ethereum)
Configurations
A configuration can be sent to the service to filter and/or decode transactions.
Transaction decoding is only available for Ethereum transactions
There is a limit of 50 configurations per connection / API key. If you need to watch more than 50 addresses and also filter them, it is recommended that you subscribe to all of the addresses and then add a 'global'
scoped filter that will apply to all of those addresses.
parameter | type | description |
| string | A valid Ethereum address or |
| string[] | An array of valid filters. The Blocknative service uses |
| string[] | A valid JSON ABI that will be used to decode input data for transactions that occur on the contract address defined in |
| boolean | defines whether the service should automatically watch the address as defined in |
scope
establishes the context in which a filter is applied. The global
scope applies the filter to all transactions/addresses watched. Therefore a global
scope only supports generic transaction elements. A scope
set to an address can include filters, such as methodName,
that are specific to the contract at that address. Similarly, the abi
field only works for a contract specific scope
.
Connection Drops
In the event of a WebSocket connection drop, no state is persisted between connections, so upon reconnection there will need to be logic defined that will:
Send an initialization message
Send all previously subscribed addresses, transactions and configs
Transaction Events
Each message sent to the service will receive an echo of that message back, this echo may be ignored for most purposes.
Every payload contains an eventCode
parameter in the event
object which indicates the type of event. Detailed documentation for event codes can be found in the here.
Ethereum Transaction Payload
Address subscription payload additional properties
If the transaction event is linked to an address subscription it will have the following properties that are unique to address subscriptions:
| type | description |
| string | the address subscription that this transaction is linked to, allowing you to match the transaction to a subscription |
| string | a value of |
| string | the counter-party to the watched address |
Confirmed/failed transaction payload additional properties
Transactions with a status of confirmed
will have the following additional properties that are not included in other statuses:
| type | description |
| string | the time, in milliseconds, the transaction was pending before being included in a block |
| string | the number of blocks the transaction was pending before being included in a block |
| string | the hash of the block containing the confirmed (or failed) transaction |
| number | the number of the block containing the confirmed (or failed) transaction |
| number | the amount of gas used to confirm (or fail) the transaction |
Replaced transactions payload additional properties
Transactions that have been replaced and have a status of speedup
or cancel
will have the following additional properties that are not included in other statuses:
| type | description |
| string | the hash of the transaction that is being replaced |
Errors
Errors from the service are returned as messages with a status
= 'error'
and a reason string explaining the error.
Websocket payload is too large (60,000 bytes):
API key (dappId
) parameter is missing:
API key (dappId
) is invalid:
Attempt to watch an invalid address:
Address validity is based on the specific blockchain system
used (bitcoin or ethereum).
Amount of messages has exceeded limit, see Rate Limits section below.
Exceeded daily event limit for your API key, see Rate Limits section below.
IP has been rate limited, see Rate Limits section below.
Error received for using incorrect API version.
Supported Networks
See Mempool API Support Networks.
Rate Limits
Daily per API key event rate limits that apply to Webhook events also apply to WebSocket events. Details on those limits can be found here.
The Blocknative service also limits how many messages can be sent over a duration of time, limiting to 50 messages per second.
A strategy that the Blocknative SDK uses to handle these limits is to create a message queue that processes messages and ensures that messages are spaced adequately. This can be especially important to implement when re-sending addresses to be subscribed after a connection dropout.
If a rate limit error message is received you can use the retryMs
parameter to wait for that time period to continue sending messages. If a message was blocked due to a rate limit it will be included in the payload in the blockedMsg
parameter so you can re-send that message after the retry delay, ensuring that no messages are dropped.
Last updated