Skip to content

Commit

Permalink
Merge pull request #32 from blocknative/develop
Browse files Browse the repository at this point in the history
Release 0.2.0
  • Loading branch information
lnbc1QWFyb24 authored Oct 23, 2019
2 parents bf22d80 + be90f72 commit 4b632ee
Show file tree
Hide file tree
Showing 12 changed files with 459 additions and 376 deletions.
44 changes: 28 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import web3 from "./web3"
const options = {
dappId: "Your dappId here",
networkId: 1,
transactionEvents: event =>
transactionHandler: event =>
console.log("Transaction Event:", event.transaction)
}

Expand Down Expand Up @@ -46,7 +46,7 @@ import Notify from "bn-notify"
const options = {
dappId: "Your dappId here",
networkId: 1,
transactionEvents: event =>
transactionHandler: event =>
console.log("Transaction Event:", event.transaction)
}

Expand All @@ -59,7 +59,7 @@ const notify = Notify(options)
const options = {
dappId: String,
networkId: Number,
transactionEvents: Function
transactionHandler: Function
}
```

Expand All @@ -77,9 +77,9 @@ The Ethereum network id that your application runs on. The following values are
- `5` Goerli Test Network
- `42` Kovan Test Network

#### `transactionEvents` - [OPTIONAL]
#### `transactionHandler` - [OPTIONAL]

The function defined for the `transactionEvents` parameter will be called once for every status update for _every_ transaction that is associated with a watched address _or_ a watched transaction. This is useful as a global handler for all transactions and status updates. The callback is called with the following object:
The function defined for the `transactionHandler` parameter will be called once for every status update for _every_ transaction that is associated with a watched address _or_ a watched transaction. This is useful as a global handler for all transactions and status updates. The callback is called with the following object:

See the [Transaction Object](#transaction-object) section for more info on what is included in the `transaction` parameter.

Expand Down Expand Up @@ -160,24 +160,36 @@ You may want to trigger a notification for a custom event that may not be relate

```javascript
const notificationObject = {
eventCode: "dbUpdate"
type: "pending",
message: "Updating the database with your information",
autoDismiss: 4000
message: "Updating the database with your information"
}

const { update, dismiss } = notify.notification(
"databaseUpdate",
notificationObject
)
const { update, dismiss } = notify.notification(notificationObject)

//.... somewhere else in your code

if (dbUpdated) {
update({
eventCode: "dbUpdateComplete"
type: "success",
message: "Your info is up to date!"
})
} else {
update({
eventCode: "dbUpdateSlow"
type: "pending",
message: "Database update is taking longer than usual, hang in there!"
})
}
```

The `notification` function is called with two arguments:
The `notification` function is called with a notification object with the following parameters:

- `eventCode`: a string which is used to keep track of that event for your analytics dashboard
- `notificationObject`: a object that defines the notification with the parameters:
- `type`: a string that defines the style - ['hint' (gray), 'pending' (yellow), 'success' (green), 'error' (red)]
- `message`: a message string that is displayed on the notification
- `autoDismiss`: a number in milliseconds before the notification auto dismisses. Defaults to no auto dismissal
- `type`: a string that defines the style - ['hint' (gray), 'pending' (yellow), 'success' (green), 'error' (red)]
- `message`: a message string that is displayed on the notification
- `autoDismiss`: a number in milliseconds before the notification auto dismisses or `false` for no auto dismissal. `success` and `hint` types default to `4000`

Returned from the notification function is an object that has two functions defined on it:

Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bnc-notify",
"version": "0.1.2",
"version": "0.2.0",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"files": [
Expand All @@ -26,9 +26,8 @@
},
"dependencies": {
"bignumber.js": "^9.0.0",
"bnc-sdk": "0.1.1",
"bnc-sdk": "0.2.1",
"lodash.debounce": "^4.0.8",
"ow": "^0.13.2",
"regenerator-runtime": "^0.13.3",
"svelte": "^3.5.4",
"svelte-i18n": "^1.1.2-beta",
Expand Down
1 change: 0 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export default [
"bignumber.js",
"bnc-sdk",
"lodash.debounce",
"ow",
"svelte-i18n",
"uuid/v4",
"svelte",
Expand Down
2 changes: 1 addition & 1 deletion src/components/AutoDismiss.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
if (notification.autoDismiss) {
setTimeout(() => {
notifications.remove(notification);
notifications.remove(notification.id);
}, notification.autoDismiss);
}
</script>
35 changes: 22 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ const version = "0.0.1"
function init(initialize) {
validateInit(initialize)

const { dappId, networkId, transactionEvents } = initialize
const { dappId, networkId, transactionHandler } = initialize

const transactionListeners = [handleTransactionEvent]
const transactionHandlers = [handleTransactionEvent]

if (transactionEvents) {
transactionListeners.push(transactionEvents)
if (transactionHandler) {
transactionHandlers.push(transactionHandler)
}

const blocknative = new blocknativeSdk({
const blocknative = blocknativeSdk({
dappId,
networkId,
transactionListeners
transactionHandlers
})

// save config to app store
Expand Down Expand Up @@ -73,7 +73,7 @@ function init(initialize) {

function account(address) {
try {
const result = blocknative.account(address)
const result = blocknative.account(blocknative.clientIndex, address)
return result
} catch (error) {
throw new Error(error)
Expand All @@ -82,7 +82,7 @@ function init(initialize) {

function hash(hash, id) {
try {
const result = blocknative.transaction(hash, id)
const result = blocknative.transaction(blocknative.clientIndex, hash, id)
return result
} catch (error) {
throw new Error(error)
Expand All @@ -94,24 +94,34 @@ function init(initialize) {

const emitter = createEmitter()

const result = preflightTransaction(options, emitter, blocknative)
const result = preflightTransaction(
blocknative.clientIndex,
options,
emitter,
blocknative
)

return {
emitter,
result
}
}

function notification(eventCode, notificationObject) {
function notification(notificationObject) {
validateNotificationObject(notificationObject)

let key = 0

const id = uuid()
const startTime = Date.now()
const { eventCode = `customNotification${key++}` } = notificationObject

const dismiss = () => notifications.remove({ id, eventCode })
const dismiss = () => notifications.remove(id)

function update(eventCode, notificationUpdate) {
function update(notificationUpdate) {
validateNotificationObject(notificationUpdate)

const { eventCode = `customNotification${key++}` } = notificationUpdate
createNotification({ id, startTime, eventCode }, notificationUpdate)

return {
Expand All @@ -120,7 +130,6 @@ function init(initialize) {
}
}

// create notification
createNotification({ id, startTime, eventCode }, notificationObject)

return {
Expand Down
2 changes: 1 addition & 1 deletion src/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function createNotification(details, customization = {}) {
startTime,
eventCode,
message: formatter(...formatterOptions),
autoDismiss: typeToDismissTimeout(type),
autoDismiss: typeToDismissTimeout(customization.type || type),
...customization
}

Expand Down
8 changes: 4 additions & 4 deletions src/stores.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writable } from "svelte/store"
import { updateOrAdd } from "./utilities"
import { replaceOrAdd } from "./utilities"

export const app = writable({
version: null,
Expand All @@ -25,7 +25,7 @@ function createTransactionStore(initialState) {
const predicate = tx => tx.id === transaction.id

update(store => {
return updateOrAdd(store, predicate, transaction)
return replaceOrAdd(store, predicate, transaction)
})
}

Expand Down Expand Up @@ -57,8 +57,8 @@ function createNotificationStore(initialState) {
})
}

function remove({ id, eventCode }) {
update(store => store.filter(n => n.id !== id || n.eventCode !== eventCode))
function remove(id) {
update(store => store.filter(n => n.id !== id))
}

return {
Expand Down
10 changes: 8 additions & 2 deletions src/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ export function duplicateTransactionCandidate(transaction, contract) {
return duplicate
}

export function preflightTransaction(options, emitter, blocknative) {
export function preflightTransaction(
clientIndex,
options,
emitter,
blocknative
) {
return new Promise((resolve, reject) => {
// wrap in set timeout to put to the end of the event queue
setTimeout(async () => {
Expand Down Expand Up @@ -239,7 +244,8 @@ export function preflightTransaction(options, emitter, blocknative) {
})

if (hash && typeof hash === "string") {
const serverEmitter = blocknative.transaction(hash, id).emitter
const serverEmitter = blocknative.transaction(clientIndex, hash, id)
.emitter

serverEmitter.on("all", transaction => {
const listener =
Expand Down
23 changes: 9 additions & 14 deletions src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ export function argsEqual(args1, args2) {

export function timeString(time) {
const seconds = Math.floor(time / 1000)
return seconds >= 60 ? `${Math.floor(seconds / 60)} min` : `${seconds} sec`
const formattedSeconds = seconds < 0 ? 0 : seconds
return formattedSeconds >= 60
? `${Math.floor(formattedSeconds / 60)} min`
: `${formattedSeconds} sec`
}

export function formatTime(number) {
Expand All @@ -16,27 +19,19 @@ export function formatTime(number) {
})
}

export function removeUndefined(obj) {
return Object.keys(obj).reduce((newObj, key) => {
if (obj[key] !== undefined) {
newObj[key] = obj[key]
}

return newObj
}, {})
}

// will update object(merge new data) in list if it passes predicate, otherwise adds new object
export function updateOrAdd(list, predicate, data) {
export function replaceOrAdd(list, predicate, data) {
const clone = [...list]
const index = clone.findIndex(predicate)

if (index !== -1) {
clone[index] = { ...clone[index], ...removeUndefined(data) }
const { startTime } = clone[index]
const { startTime: serverStartTime } = data
clone[index] = { ...data, startTime: startTime || serverStartTime }
return clone
}

return [...list, removeUndefined(data)]
return [...list, data]
}

export function extractMessageFromError(error) {
Expand Down
Loading

0 comments on commit 4b632ee

Please sign in to comment.