Notify SDK

Access Blocknative notification messages with our simple web socket library

Open source Github repo: https://github.com/blocknative/sdk

Quick Start

It should take less than 5 minutes to get going with Blocknative Notify.

Create a Blocknative Account

Go to the Account Dashboard at https://account.blocknative.com/ and setup an account with an email address. You will receive an email to confirm your account.

Create your API Key

On the Account Dashboard, create an API key with your choice of name using the API Keys link (https://account.blocknative.com/api-keys). API keys segment analytics data. Consider using different API keys for development, staging, and production releases.

API key creation in Account Dashboard

Install the widget using npm

npm install bnc-sdk

Initialize the Library

Use the minimum required config. There are more options available in our documentation.

import sdk from "bn-sdk"
// create options object
var sdkConfig = {
dappId: apiKey, // [String] The API key created by step one above
networkId: networkId // [Integer] The Ethereum network ID your Dapp uses.
transactionCallback: event => console.log(event.transaction)
};
// initialize and connect to the api
var blocknative = sdk.init(sdkConfig);

Track transactions

Send a transaction using web3.js and get the transaction hash while its processing. Let notify know the hash and it will track its progress through the mempool and into a block.

// get current account
const myAccounts = await web3.eth.getAccounts();
const myAddress = accounts[0];
// send the transaction using web3.js and get the hash
web3.eth.sendTransaction({
from: myAddress,
to: "0x792ec62e6840bFcCEa00c669521F678CE1236705",
value: "1000000"})
.on('transactionHash', function(hash){
// call with the transaction hash of the transaction
// that you would like to receive status updates for
const transaction = blocknative.transaction(hash)
// grab the emitter
const emitter = transaction.emitter
// catch every other event that occurs and log it
emitter.on("all", transaction => {
console.log(`Transaction event: ${transaction.eventCode}`)
})
};

And you are live!

Configuration Options

The following options object needs to be passed when initializing and connecting

const options = {
dappId: String,
networkId: Number,
transactionHandlers: Array,
ws: Function
}

dappId - [REQUIRED]

Your unique apiKey that identifies your application. You can generate a dappId by visiting the Blocknative account page and creating a free account.

networkId - [REQUIRED]

The Ethereum network id that your application runs on. The following values are valid:

  • 1 Main Network

  • 3 Ropsten Test Network

  • 4 Rinkeby Test Network

  • 5 Goerli Test Network

  • 42 Kovan Test Network

transactionCallback - [OPTIONAL]

An array of functions that will each be called once for every status update for every transaction that is associated with this connection on a watched address or a watched transaction. This is useful as a global handler for all transactions and status updates. Each callback is called with the following object:

const options = {
// other options
transactionHandlers: [
event => {
const {
transaction, // transaction object
emitterResult // data that is returned from the transaction event listener defined on the emitter
} = event
}
]
}

See the Transaction Object section for more info on what is included in the transaction parameter.

ws - [OPTIONAL]

If you are running the sdk in a server environment, there won't be a native websocket instance available for the sdk to use so you will need to pass one in. You can use any websocket library that you prefer as long as it correctly implements the websocket specifications. We recommend ws.

Initialize and Connect

Import and initialize the SDK with the configuration options described above for client and server environments.

Client/Browser Environment

import blocknativeSdk from 'bn-sdk'
// create options object
const options = {
dappId: 'Your dappId here',
networkId: 1,
transactionHandlers: [event => console.log(event.transaction)]
}
// initialize and connect to the api
const blocknative = blocknativeSdk(options)

Server/Node.js Environment

import blocknativeSdk from 'bn-sdk'
import ws from 'ws'
// create options object
const options = {
dappId: 'Your dappId here',
networkId: 1,
transactionHandlers: [event => console.log(event.transaction)],
ws: ws
}
// initialize and connect to the api
const blocknative = blocknativeSdk(options)

Notifications

Watch a Transaction Hash

Now that your application is successfully connected via a websocket connection to the Blocknative backend, you can register transactions to watch for updates (notifications). Once you have initiated a transaction and have received the transaction hash, you can pass it in to the transaction function:

// initiate a transaction via web3.js
const hash = await web3.eth.sendTransaction(txOptions)
// call with the transaction hash of the transaction that you would like to receive status updates for
const {
emitter, // emitter object to listen for status updates
details // initial transaction details which are useful for internal tracking: hash, timestamp, eventCode
} = blocknative.transaction(hash)

The emitter is a Javascript emitter object to listen for status updates. See the Emitter Section for details on how to use the emitter object to handle specific transaction state changes.

The details object contains the initial transaction details which are useful for internal tracking.

If the library was initialized with a transactionCallback() function, that function will also be called on each status change for the watch transaction.

Watch an Account Address

You can also register an account address to listen to any incoming and outgoing transactions that occur on that address using the account method:

// get the current accounts list of the user via web3.js
const accounts = await web3.eth.getAccounts()
// grab the primary account
const address = accounts[0]
// call with the address of the account that you would like to receive status updates for
const {
emitter, // emitter object to listen for status updates
details // initial account details which are useful for internal tracking: address
} = blocknative.account(address)

This will tell the Blocknative backend to watch for any transactions that occur involving this address and any updates to the transaction status over time. The return object from successful calls to account will include an event emitter that you can use to listen for those events and a details object which includes the address that is being watched:

The emitter is a Javascript emitter object to listen for status updates. See the Emitter Section for details on how to use the emitter object to handle specific transaction state changes.

The details object contains the initial transaction details which are useful for internal tracking.

If the library was initialized with a transactionCallback() function, that function will also be called on each status change for the watch transaction.

Log an Event

You may want to log an event that isn't associated with a transaction for analytics purposes. Events are collated and displayed in the developer portal and are segmented by your dappId. To log an event, simple call event with a categoryCode and an eventCode, both of which can be any String that you like:

blocknative.event({
categoryCode: String, // [REQUIRED] - The general category of the event
eventCode: String // [REQUIRED] - The specific event
})

Emitter

The emitter object is returned from calls to account and transaction and is used to listen to status updates via callbacks registered for specific event codes.

// register a callback for a txPool event
emitter.on("txPool", transaction => {
console.log("Transaction is pending")
})

The first parameter is the eventCode string of the event that you would like to register a callback for. For a list of the valid event codes, see the section on event codes.

The second parameter is the callback that you would like to register to handle that event and will be called with a transaction object that includes all of the relevant details for that transaction. See the Transaction Object section for more info on what is included.

Any data that is returned from the listener callback for transaction emitters will be included in the object that the global transactionCallback is called with under the emitterResult property.

Transaction Object

The callback that is registered for events on the emitter will be called with the following transaction object:

{
status: String, // current status of the transaction
hash: String,
to: String,
from: String,
gas: Number,
gasPrice: String,
nonce: Number,
value: String,
eventCode: String,
blockHash: String,
blockNumber: Number,
input: String,
transactionIndex: Number,
r: String,
s: String,
v: String,
counterParty: String, // address of the counterparty of the transaction when watching an account
direction: String, // the direction of the transaction in relation to the account that is being watched ("incoming" or "outgoing")
watchedAddress: String, // the address of the account being watched
originalHash: String, // if a speedup or cancel status, this will be the hash of the original transaction
asset: String, // the asset that was transfered
contractCall: { // if transaction was a contract call otherwise undefined
contractAddress: String,
contractType: String,
methodName: String,
params: {
// params that the contract method was called with
}
}
}

Event Codes

The following is a list of event codes that are valid, and the events that they represent:

  • all: Will be called for all events that are associated with that emitter. If a more specific listener exists for that event, then that will be called instead. This is useful to catch any remaining events that you haven't specified a handler for

  • txPool: Transaction is in the mempool and is pending

  • txConfirmed: Transaction has been mined

  • txFailed: Transaction has failed

  • txSpeedUp: A new transaction has been submitted with the same nonce and a higher gas price, replacing the original transaction

  • txCancel: A new transaction has been submitted with the same nonce, a higher gas price, a value of zero and sent to an external address (not a contract)

  • txDropped: Transaction was dropped from the mempool without being added to a block

Get Started Today

Sign up for a free Blocknative Account at https://account.blocknative.com/ with your work email address.

If you have any questions, connect with the team on our discord