Blocknative sdk
A lightweight JavaScript sdk to connect to the Blocknative backend Ethereum node infrastructure via a websocket connection for realtime transaction updates.
Usage
Installation
npm i bnc-sdk
Quick Start
// create options objectconst options = dappId: 'Your dappId here' networkId: '1' transactionHandlers: console // initialize and connect to the apiconst blocknative = // get the client index from initializationconst clientIndex = blocknative // initiate a transaction via web3.jsconst hash = await web3eth // call with the transaction hash of the transaction that you would like to receive status updates forconst transaction = blocknative // grab the emitterconst emitter = transactionemitter // listen to some eventsemitter emitter // catch every other event that occurs and log itemitter
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 create a free account.
networkId
- [REQUIRED]
The Ethereum network id that your application runs on. The following values are valid:
1
Main Network3
Ropsten Test Network4
Rinkeby Test Network5
Goerli Test Network42
Kovan Test Network
transactionHandlers
- [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: { 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
(Client/Browser Environment)
// create options objectconst options = dappId: 'Your dappId here' networkId: 1 transactionHandlers: console // initialize and connect to the apiconst blocknative =
(Server/Node.js Environment)
// create options objectconst options = dappId: 'Your dappId here' networkId: 1 transactionHandlers: console ws: ws // initialize and connect to the apiconst blocknative =
Register a Transaction
Now that your application is successfully connected via a websocket connection to the Blocknative backend, you can now register transactions that you would like updates for. Once you have initiated a transaction and have received the transaction hash, you can pass it in to the transaction
function. The transaction function requires the clientIndex
that is a parameter on the instantiated blocknative
object as the first parameter. This is needed to make sure that the sdk
instance gets the correct notifications.
// initiate a transaction via web3.jsconst hash = await web3eth // call with the transaction hash of the transaction that you would like to receive status updates forconst emitter // emitter object to listen for status updates details // initial transaction details which are useful for internal tracking: hash, timestamp, eventCode} = blocknative
Check out the Emitter Section for details on the emitter
object
This will tell the Blocknative backend to watch for status updates for that transaction hash. The return object from successful calls to transaction
will include an event emitter that you can use to listen for particular events for that transaction and the initial details of that transaction.
Register a Account
You can also register an account address to listen to any incoming and outgoing transactions that occur on that address. The address function requires the clientIndex
that is a parameter on the instantiated blocknative
object as the first parameter. This is needed to make sure that the sdk
instance gets the correct notifications.
// get the current accounts list of the user via web3.jsconst accounts = await web3eth // grab the primary accountconst address = accounts0 // call with the address of the account that you would like to receive status updates forconst emitter // emitter object to listen for status updates details // initial account details which are useful for internal tracking: address} = blocknative
Check out the Emitter Section for details on the emitter
object
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:
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:
blocknativeevent 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 eventemitter
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 transactionHandlers
functions will be 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 // transaction hash to: String // the address the transaction is being sent to from: String // the address the transaction is being sent from gas: Number // the gas in wei gasPrice: String // the gasPrice in wei nonce: Number // the nonce of the transaction value: String // the value being sent eventCode: String // the event code for this status blockHash: String // the hash of the block that this transaction was included in blockNumber: Number // the block number of the block that this transaction was included in input: String // hex string of the input data transactionIndex: Number // same as the nonce r: String // transaction signature s: String // transaction signature v: String // transaction signature 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 // the address of the contract that has been called contractType: String // the contract type eg: ERC20, ERC721 methodName: String // the name of the method that was called 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 fortxPool
: Transaction is in the mempool and is pendingtxConfirmed
: Transaction has been minedtxFailed
: Transaction has failedtxSpeedUp
: A new transaction has been submitted with the same nonce and a higher gas price, replacing the original transactiontxCancel
: 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