Skip to content

Commit

Permalink
Merge pull request #14 from blocknative/develop
Browse files Browse the repository at this point in the history
Release 0.1.2

- Add typescript definitions
- Handle watch account events
  • Loading branch information
lnbc1QWFyb24 authored Oct 3, 2019
2 parents 6d1457f + 8a7be45 commit 5092a79
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"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",
"files": [
"dist/esm/*",
"dist/cjs/*"
],
"types": "./types.d.ts",
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
Expand Down
22 changes: 17 additions & 5 deletions src/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 24 additions & 14 deletions src/messages.js
Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -50,39 +50,49 @@ 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
if (serverEcho(eventCode)) {
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 emitterResult = hashNotifier
? hashNotifier.emitter.emit(newState)
: false
emitterResult = hashNotifier && hashNotifier.emitter.emit(newState)
}

session.transactionListeners &&
session.transactionListeners.forEach(listener =>
Expand Down
2 changes: 1 addition & 1 deletion src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export let session = {
transactionListeners: null,
status: {
nodeSynced: true,
connected: null
connected: false
},
transactions: [],
accounts: []
Expand Down
9 changes: 6 additions & 3 deletions src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -92,3 +91,7 @@ export function serverEcho(eventCode) {
return false
}
}

export function last(arr) {
return arr && arr.reverse()[0]
}
29 changes: 29 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5092a79

Please sign in to comment.