Skip to content

Commit

Permalink
Merge pull request #165 from blocknative/release/3.7.1
Browse files Browse the repository at this point in the history
Release 3.7.1
  • Loading branch information
lnbc1QWFyb24 authored Jan 9, 2022
2 parents 9d21629 + 1b45ee4 commit 7fc29c3
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 22 deletions.
111 changes: 91 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A lightweight JavaScript sdk to connect to the Blocknative backend Ethereum node

`npm install bnc-sdk`

### Quick Start (client)
### Quick Start (Node.js)

#### Transaction Monitor

Expand All @@ -17,24 +17,23 @@ import WebSocket from 'ws'
import BlocknativeSdk from 'bnc-sdk'
import Web3 from 'web3'

const wsapi_url = 'wss://api.blocknative.com/v0'

const web3 = new Web3(wsapi_url)
const web3 = new Web3('<ws://some.local-or-remote.node:8546>')

// create options object
const options = {
dappId: process.env.BN_API_KEY,
ws: WebSocket,
networkId: 1,
transactionHandlers: [event => console.log(event.transaction)]
dappId: '<YOUR_API_KEY>',
networkId: 4,
ws: WebSocket
// un-comment if you would like to log all transaction events
// transactionHandlers: [event => console.log(event.transaction)]
}

// initialize and connect to the api
const blocknative = new BlocknativeSdk(options)

const txOptions = {
from: "0x7A132e43013cAA14a3744721EF179f1F3d2b3921",
to: "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
to: '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',
value: 1000000000000000
}

// initiate a transaction via web3.js
Expand All @@ -56,7 +55,6 @@ web3.eth.sendTransaction(txOptions).on('transactionHash', hash => {
console.log(`Transaction event: ${transaction.eventCode}`)
})
})

```

#### Address Listener
Expand All @@ -66,29 +64,102 @@ import WebSocket from 'ws'
import BlocknativeSdk from 'bnc-sdk'
import Web3 from 'web3'

const wsapi_url = 'wss://api.blocknative.com/v0'

const web3 = new Web3(wsapi_url)
const web3 = new Web3('<ws://some.local-or-remote.node:8546>')

// create options object
const options = {
dappId: process.env.BN_API_KEY,
ws: WebSocket,
networkId: 1
dappId: '<YOUR_API_KEY>',
networkId: 4,
ws: WebSocket
// un-comment if you would like to log all transaction events
// transactionHandlers: [event => console.log(event.transaction)]
}

// initialize and connect to the api
const blocknative = new BlocknativeSdk(options)

const address = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"
const address = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'

const { emitter, details } = blocknative.account(address)

emitter.on("all", transaction => {
console.log(transaction)
emitter.on('all', transaction => {
console.log(transaction)
})
```

### Quick Start (Browser)

#### Transaction Monitor

```javascript
import BlocknativeSdk from 'bnc-sdk'
import Web3 from 'web3'

const web3 = new Web3(window.ethereum)

// create options object
const options = {
dappId: '<YOUR_API_KEY>',
networkId: 4
// un-comment if you would like to log all transaction events
// transactionHandlers: [event => console.log(event.transaction)]
}

// initialize and connect to the api
const blocknative = new BlocknativeSdk(options)

const txOptions = {
to: '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',
value: 1000000000000000
}

// initiate a transaction via web3.js
web3.eth.sendTransaction(txOptions).on('transactionHash', hash => {
// call with the transaction hash of the transaction that you would like to receive status updates for
const { emitter } = blocknative.transaction(hash)

// listen to some events
emitter.on('txPool', transaction => {
console.log(`Sending ${transaction.value} wei to ${transaction.to}`)
})

emitter.on('txConfirmed', transaction => {
console.log('Transaction is confirmed!')
})

// catch every other event that occurs and log it
emitter.on('all', transaction => {
console.log(`Transaction event: ${transaction.eventCode}`)
})
})

#### Address Listener

```javascript
import BlocknativeSdk from 'bnc-sdk'
import Web3 from 'web3'
const web3 = new Web3(window.ethereum)
// create options object
const options = {
dappId: '<YOUR_API_KEY>',
networkId: 4
// un-comment if you would like to log all transaction events
// transactionHandlers: [event => console.log(event.transaction)]
}
// initialize and connect to the api
const blocknative = new BlocknativeSdk(options)
const address = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'
const { emitter, details } = blocknative.account(address)
emitter.on('all', transaction => {
console.log(transaction)
})
```

## Documentation

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bnc-sdk",
"version": "3.7.0",
"version": "3.7.1",
"description": "SDK to connect to the blocknative backend via a websocket connection",
"keywords": [
"ethereum",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ async function onReopen(this: any, handler: (() => void) | undefined) {
handler()
}

if (this._socket.ws.on) {
if (this._socket.ws && this._socket.ws.on) {
// need to re-register ping event since new connection
this._socket.ws.on('ping', () => {
this._heartbeat && this._heartbeat()
Expand Down

0 comments on commit 7fc29c3

Please sign in to comment.