From 9558309392c4388a8391ec868241381cd44a0995 Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Tue, 1 Oct 2019 21:16:22 +1000 Subject: [PATCH 1/6] Return value from emit call --- src/utilities.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/utilities.js b/src/utilities.js index 41786f0..9972da1 100644 --- a/src/utilities.js +++ b/src/utilities.js @@ -31,12 +31,11 @@ export function createEmitter() { }, emit: function(state) { if (this.listeners[state.eventCode]) { - this.listeners[state.eventCode](state) - return + return this.listeners[state.eventCode](state) } if (this.listeners.all) { - this.listeners.all(state) + return this.listeners.all(state) } } } From e46735e7a0ccd18f40dc65eb80fd2e9bd13971de Mon Sep 17 00:00:00 2001 From: Chris Meisl Date: Tue, 1 Oct 2019 10:54:38 -0600 Subject: [PATCH 2/6] remove unused cmd in ci --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 4dd81c9..5cc696e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -47,7 +47,7 @@ jobs: - run: NPM_USER=$NPM_USERNAME NPM_EMAIL=$NPM_EMAIL NPM_PASS=$NPM_PASSWORD npm-cli-login - run: npm publish - run: gzip -9 /home/circleci/project/dist/iife/sdk.js - - run: mv /home/circleci/project/dist/iife/sdk.js.gz /home/circleci/project/dist/iife/sdk.js # - run: mv /home/circleci/project/dist/iife/sdk.min.js.gz /home/circleci/project/dist/iife/sdk.min.js + - run: mv /home/circleci/project/dist/iife/sdk.js.gz /home/circleci/project/dist/iife/sdk.js - run: ls -al - run: echo export VERSION=`awk '/version/{gsub(/("|",)/,"",$2);print $2};' package.json | sed 's/\./-/g'` >> $BASH_ENV - run: mkdir /home/circleci/project/deploy-temp From 736113ca4485db7409a5e9308feed495e741a2b0 Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Wed, 2 Oct 2019 11:18:59 +1000 Subject: [PATCH 3/6] Handle account watch events --- src/account.js | 22 +++++++++++++++++----- src/index.js | 13 +++++++++++++ src/messages.js | 43 +++++++++++++++++++++++++++++-------------- src/utilities.js | 4 ++++ 4 files changed, 63 insertions(+), 19 deletions(-) diff --git a/src/account.js b/src/account.js index 5226cb8..76543c3 100644 --- a/src/account.js +++ b/src/account.js @@ -6,17 +6,29 @@ import { sendMessage } from "./messages" function account(address) { validateAddress(address) + // lowercase the address + address = address.toLowerCase() + // create emitter for transaction const emitter = createEmitter() // create eventCode for transaction const eventCode = "accountAddress" - // put in queue - session.accounts.push({ - address, - emitter - }) + const existingAddressWatcher = session.accounts.find( + account => account.address === address + ) + + if (existingAddressWatcher) { + // add to existing emitters array + existingAddressWatcher.emitters.push(emitter) + } else { + // put in accounts queue + session.accounts.push({ + address, + emitters: [emitter] + }) + } // logEvent to server sendMessage({ diff --git a/src/index.js b/src/index.js index a6c7a08..ab90c6c 100644 --- a/src/index.js +++ b/src/index.js @@ -65,6 +65,19 @@ function sdk(options) { eventCode: "checkDappId", connectionId }) + + // re-register all accounts to be watched by server upon + // re-connection as they don't get transferred over automatically + // to the new connection like tx hashes do + session.accounts.forEach(account => { + sendMessage({ + eventCode: "accountAddress", + categoryCode: "watch", + account: { + address: account.address + } + }) + }) } session.socket.onmessage = handleMessage diff --git a/src/messages.js b/src/messages.js index 617ba2a..9720aba 100644 --- a/src/messages.js +++ b/src/messages.js @@ -1,5 +1,5 @@ import { session } from "./state" -import { createEventLog, networkName, serverEcho } from "./utilities" +import { createEventLog, networkName, serverEcho, last } from "./utilities" export function sendMessage(msg) { session.socket.send(createEventLog(msg)) @@ -50,6 +50,8 @@ export function handleMessage(msg) { if (event && event.transaction) { const { transaction, eventCode, contractCall } = event + + // flatten in to one object const newState = { ...transaction, eventCode, contractCall } // ignore server echo messages @@ -57,32 +59,45 @@ export function handleMessage(msg) { return } - //handle change of hash in speedup and cancel events + // handle change of hash in speedup and cancel events if (eventCode === "txSpeedUp" || eventCode === "txCancel") { session.transactions = session.transactions.map(tx => { if (tx.hash === transaction.originalHash) { + // reassign hash parameter in transaction queue to new hash tx.hash = transaction.hash } return tx }) } - const addressNotifier = session.accounts.find(function(a) { - return ( - a.address.toLowerCase() === - (transaction.watchedAddress && transaction.watchedAddress.toLowerCase()) + const watchedAddress = + transaction.watchedAddress && transaction.watchedAddress.toLowerCase() + + let emitterResult + + if (watchedAddress) { + const addressNotifier = session.accounts.find( + account => account.address === watchedAddress ) - }) - addressNotifier && addressNotifier.emitter.emit(newState) + const results = + addressNotifier && + addressNotifier.emitters.map(emitter => emitter.emit(newState)) - const hashNotifier = session.transactions.find(function(t) { - return t.id === transaction.id || t.hash === transaction.hash - }) + // the emitter result that affects notifications in notify is the result from the latest emitter + emitterResult = last(results) + } else { + const hashNotifier = session.transactions.find( + tx => tx.id === transaction.id || tx.hash === transaction.hash + ) + + const results = + hashNotifier && + hashNotifier.emitters.map(emitter => emitter.emit(newState)) - const emitterResult = hashNotifier - ? hashNotifier.emitter.emit(newState) - : false + // the emitter result that affects notifications in notify is the result from the latest emitter + emitterResult = last(results) + } session.transactionListeners && session.transactionListeners.forEach(listener => diff --git a/src/utilities.js b/src/utilities.js index 9972da1..00edd20 100644 --- a/src/utilities.js +++ b/src/utilities.js @@ -91,3 +91,7 @@ export function serverEcho(eventCode) { return false } } + +export function last(arr) { + return arr && arr.reverse()[0] +} From c1e0d7c3d963268fc7ba5da3fdaa1c792374b23d Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Wed, 2 Oct 2019 15:09:01 +1000 Subject: [PATCH 4/6] Fix hash emitter bug --- src/messages.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/messages.js b/src/messages.js index 9720aba..eadb91d 100644 --- a/src/messages.js +++ b/src/messages.js @@ -91,12 +91,7 @@ export function handleMessage(msg) { tx => tx.id === transaction.id || tx.hash === transaction.hash ) - const results = - hashNotifier && - hashNotifier.emitters.map(emitter => emitter.emit(newState)) - - // the emitter result that affects notifications in notify is the result from the latest emitter - emitterResult = last(results) + emitterResult = hashNotifier && hashNotifier.emitter.emit(newState) } session.transactionListeners && From 9c09a528adc74178be709a6f680eb348e99fed9a Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Thu, 3 Oct 2019 12:06:21 +1000 Subject: [PATCH 5/6] Add typescript definitions --- package.json | 1 + src/state.js | 2 +- types.d.ts | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 types.d.ts diff --git a/package.json b/package.json index 1fe27be..8e54df1 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "dist/esm/*", "dist/cjs/*" ], + "types": "./types.d.ts", "devDependencies": { "@babel/core": "^7.5.5", "@babel/preset-env": "^7.5.5", diff --git a/src/state.js b/src/state.js index 5ee2548..7a520de 100644 --- a/src/state.js +++ b/src/state.js @@ -8,7 +8,7 @@ export let session = { transactionListeners: null, status: { nodeSynced: true, - connected: null + connected: false }, transactions: [], accounts: [] diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 0000000..a1ef49c --- /dev/null +++ b/types.d.ts @@ -0,0 +1,29 @@ +export as namespace blocknativeSdk + +export = sdk + +interface transactionCallback { + (transactionEvent: object): void +} + +interface optionsObject { + networkId: number + dappId: string + transactionListeners?: transactionCallback[] + apiUrl?: string + ws?: any +} + +interface status { + nodeSynced: boolean; + connected: boolean; +} + +interface sdkApi { + transaction: (hash: string, id?: string) => any; + account: (address: string) => any; + event: (eventObj: object) => void; + status: status; +} + +declare function sdk(options: optionsObject): sdkApi From 8a7be45f20b4cd5804c5400932a73362bd7c184b Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Thu, 3 Oct 2019 14:54:35 +1000 Subject: [PATCH 6/6] Release 0.1.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8e54df1..00c8826 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bnc-sdk", - "version": "0.1.1", + "version": "0.1.2", "description": "SDK to connect to the blocknative backend via a websocket connection", "main": "dist/cjs/index.js", "module": "dist/esm/index.js",