From 8fc062973d9d552adeedc37221a62ff80090041e Mon Sep 17 00:00:00 2001 From: Arseniy Ivanov <138289+freeatnet@users.noreply.github.com> Date: Sun, 9 Jan 2022 22:35:20 +0300 Subject: [PATCH 1/3] Ensure the WS is present before registering a listener (#163) * 158: Provide working example for the README * Ensure the WS is present before registering a listener Co-authored-by: Aaron Barnard Co-authored-by: Taylor Dawson Co-authored-by: John Cairns --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- src/index.ts | 2 +- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7c5e720..ce1a36e 100644 --- a/README.md +++ b/README.md @@ -10,15 +10,21 @@ A lightweight JavaScript sdk to connect to the Blocknative backend Ethereum node ### Quick Start (client) +#### Transaction Monitor + ```javascript +import WebSocket from 'ws' import BlocknativeSdk from 'bnc-sdk' import Web3 from 'web3' -const web3 = new Web3(window.ethereum) +const wsapi_url = 'wss://api.blocknative.com/v0' + +const web3 = new Web3(wsapi_url) // create options object const options = { - dappId: 'Your dappId here', + dappId: process.env.BN_API_KEY, + ws: WebSocket, networkId: 1, transactionHandlers: [event => console.log(event.transaction)] } @@ -26,6 +32,11 @@ const options = { // initialize and connect to the api const blocknative = new BlocknativeSdk(options) +const txOptions = { + from: "0x7A132e43013cAA14a3744721EF179f1F3d2b3921", + to: "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D", +} + // 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 @@ -44,8 +55,41 @@ web3.eth.sendTransaction(txOptions).on('transactionHash', hash => { emitter.on('all', transaction => { console.log(`Transaction event: ${transaction.eventCode}`) }) +}) + ``` +#### Address Listener + +```javascript +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) + +// create options object +const options = { + dappId: process.env.BN_API_KEY, + ws: WebSocket, + networkId: 1 +} + +// 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 For detailed documentation head to [docs.blocknative.com](https://docs.blocknative.com/notify-sdk) diff --git a/src/index.ts b/src/index.ts index 31bc5af..c739671 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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() From 939742d8adf3a810382355e2656e316f3b1f913e Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Mon, 10 Jan 2022 06:46:13 +1100 Subject: [PATCH 2/3] Fix Docs --- README.md | 112 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 92 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ce1a36e..28ff723 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: '', + 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 @@ -56,7 +55,6 @@ web3.eth.sendTransaction(txOptions).on('transactionHash', hash => { console.log(`Transaction event: ${transaction.eventCode}`) }) }) - ``` #### Address Listener @@ -66,29 +64,103 @@ 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: '', + 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: '', + 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: '', + 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 From 3e0b3a9c0b3e1be1ec31cdf7281099a73ecf37b9 Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Mon, 10 Jan 2022 06:46:22 +1100 Subject: [PATCH 3/3] Increment version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d175f42..bd263de 100644 --- a/package.json +++ b/package.json @@ -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",