Skip to content

Commit

Permalink
Merge pull request #159 from blocknative/develop
Browse files Browse the repository at this point in the history
Release 1.3.1
  • Loading branch information
lnbc1QWFyb24 authored Aug 7, 2020
2 parents 7781629 + d3077d1 commit f8f5180
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 68 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bnc-notify",
"version": "1.3.0",
"version": "1.3.1",
"description": "Show web3 users realtime transaction notifications",
"keywords": [
"ethereum",
Expand Down Expand Up @@ -50,7 +50,7 @@
},
"dependencies": {
"bignumber.js": "^9.0.0",
"bnc-sdk": "^2.1.4",
"bnc-sdk": "^2.1.5",
"lodash.debounce": "^4.0.8",
"regenerator-runtime": "^0.13.3",
"uuid": "^3.3.3"
Expand Down
26 changes: 11 additions & 15 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type {
BitcoinTransactionLog,
EthereumTransactionLog
} from 'bnc-sdk/dist/types/src/interfaces'

export interface InitOptions extends ConfigOptions {
dappId: string
networkId: number
transactionHandler?: TransactionHandler
name?: string
apiUrl?: string
system?: System
}

export interface TransactionHandler {
Expand Down Expand Up @@ -166,6 +170,8 @@ export interface UpdateNotification {
}

export interface ConfigOptions {
system?: System
networkId?: number
mobilePosition?: 'bottom' | 'top'
desktopPosition?: 'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight'
darkMode?: boolean
Expand All @@ -179,7 +185,10 @@ export interface ConfigOptions {
export interface Hash {
(hash: string, id?: string):
| never
| { details: TransactionLog; emitter: Emitter }
| {
details: BitcoinTransactionLog | EthereumTransactionLog
emitter: Emitter
}
}

export interface Transaction {
Expand Down Expand Up @@ -214,19 +223,6 @@ export interface API {
config: Config
}

export interface TransactionLog {
hash: string
id: string
startTime: number
status: string
from?: string
to?: string
value?: number | string
gas?: string
gasPrice?: string
nonce?: number
}

export interface EmitterListener {
(state: TransactionData): boolean | void | CustomNotificationObject
}
Expand Down
53 changes: 41 additions & 12 deletions src/notify.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'regenerator-runtime/runtime'
import BlocknativeSdk from 'bnc-sdk'
import { get } from 'svelte/store'

import uuid from 'uuid/v4'
import { locale, dictionary, getClientLocale } from 'svelte-i18n'
Expand All @@ -9,14 +11,11 @@ import { app, notifications } from './stores'
import { handleTransactionEvent, preflightTransaction } from './transactions'
import { createNotification } from './notifications'

import { getBlocknative } from './services'

import type {
InitOptions,
TransactionHandler,
AppStore,
API,
TransactionLog,
Emitter,
TransactionOptions,
CustomNotificationObject,
Expand All @@ -25,7 +24,7 @@ import type {
LocaleMessages
} from './interfaces'

export type {
export {
InitOptions,
TransactionHandler,
TransactionEvent,
Expand All @@ -51,7 +50,6 @@ export type {
Notification,
Config,
API,
TransactionLog,
EmitterListener,
Emitter,
NotificationDetails,
Expand Down Expand Up @@ -97,7 +95,7 @@ function init(options: InitOptions): API {
transactionHandlers.push(transactionHandler)
}

const blocknative = getBlocknative({
let blocknative = new BlocknativeSdk({
dappId,
networkId,
transactionHandlers,
Expand Down Expand Up @@ -155,10 +153,7 @@ function init(options: InitOptions): API {
}
}

function hash(
hash: string,
id?: string
): never | { details: TransactionLog; emitter: Emitter } {
function hash(hash: string, id?: string) {
try {
const result = blocknative.transaction(hash, id)
return result
Expand All @@ -174,7 +169,9 @@ function init(options: InitOptions): API {

const emitter = createEmitter()

const result = preflightTransaction(options, emitter).catch(err => err)
const result = preflightTransaction(blocknative, options, emitter).catch(
err => err
)

return {
emitter,
Expand Down Expand Up @@ -230,11 +227,43 @@ function init(options: InitOptions): API {
function config(options: ConfigOptions): void {
validateConfig(options)

const { notifyMessages, ...otherOptions } = options
const {
notifyMessages,
networkId: newNetworkId,
system: newSystem,
...otherOptions
} = options

const { networkId, system, dappId, transactionHandler, name, apiUrl } = get(
app
)

// networkId or system has changed
if (
(newNetworkId && newNetworkId !== networkId) ||
(newSystem && newSystem !== system)
) {
// close existing SDK connection
blocknative.destroy()

// create new connection with new values
blocknative = new BlocknativeSdk({
dappId,
networkId: newNetworkId || networkId,
transactionHandlers: transactionHandler
? [handleTransactionEvent, transactionHandler]
: [handleTransactionEvent],
name: name || 'Notify',
apiUrl,
system: newSystem || system
})
}

app.update((store: AppStore) => {
return {
...store,
networkId: newNetworkId || networkId,
system: newSystem || system,
...otherOptions,
notifyMessages: notifyMessages
? { ...store.notifyMessages, ...notifyMessages }
Expand Down
20 changes: 0 additions & 20 deletions src/services.ts

This file was deleted.

28 changes: 14 additions & 14 deletions src/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { transactions, app } from './stores'
import { createNotification } from './notifications'
import { argsEqual, extractMessageFromError, localNetwork } from './utilities'
import { validateNotificationObject } from './validation'
import { getBlocknative } from './services'

import type {
TransactionData,
PreflightEvent,
Expand All @@ -19,7 +19,10 @@ import type {
let transactionQueue: TransactionData[]
transactions.subscribe((store: TransactionData[]) => (transactionQueue = store))

export function handlePreFlightEvent(preflightEvent: PreflightEvent) {
export function handlePreFlightEvent(
blocknative,
preflightEvent: PreflightEvent
) {
const {
eventCode,
contractCall,
Expand All @@ -29,8 +32,6 @@ export function handlePreFlightEvent(preflightEvent: PreflightEvent) {
status
} = preflightEvent

const blocknative = getBlocknative()

blocknative.event({
categoryCode: contractCall ? 'activeContract' : 'activeTransaction',
eventCode,
Expand Down Expand Up @@ -116,6 +117,7 @@ export function duplicateTransactionCandidate(
}

export function preflightTransaction(
blocknative,
options: TransactionOptions,
emitter: Emitter
): Promise<string> {
Expand All @@ -131,8 +133,6 @@ export function preflightTransaction(
txDetails
} = options

const blocknative = getBlocknative()

//=== if `balance` or `estimateGas` or `gasPrice` is not provided, then sufficient funds check is disabled === //
//=== if `txDetails` is not provided, then duplicate transaction check is disabled === //
//== if dev doesn't want notify to intiate the transaction and `sendTransaction` is not provided, then transaction rejected notification is disabled ==//
Expand Down Expand Up @@ -164,7 +164,7 @@ export function preflightTransaction(
if (transactionCost.gt(new BigNumber(balance))) {
const eventCode = 'nsfFail'

handlePreFlightEvent({
handlePreFlightEvent(blocknative, {
eventCode,
contractCall,
balance,
Expand All @@ -186,7 +186,7 @@ export function preflightTransaction(
) {
const eventCode = 'txRepeat'

handlePreFlightEvent({
handlePreFlightEvent(blocknative, {
eventCode,
contractCall,
balance,
Expand All @@ -205,7 +205,7 @@ export function preflightTransaction(
if (transactionQueue.find(tx => tx.status === 'awaitingApproval')) {
const eventCode = 'txAwaitingApproval'

handlePreFlightEvent({
handlePreFlightEvent(blocknative, {
eventCode,
contractCall,
balance,
Expand All @@ -223,7 +223,7 @@ export function preflightTransaction(
if (awaitingApproval) {
const eventCode = 'txConfirmReminder'

handlePreFlightEvent({
handlePreFlightEvent(blocknative, {
eventCode,
contractCall,
balance,
Expand All @@ -233,7 +233,7 @@ export function preflightTransaction(
}
}, txApproveReminderTimeout)

handlePreFlightEvent({
handlePreFlightEvent(blocknative, {
eventCode: 'txRequest',
status: 'awaitingApproval',
contractCall,
Expand All @@ -255,7 +255,7 @@ export function preflightTransaction(
} catch (error) {
const { eventCode, errorMsg } = extractMessageFromError(error)

handlePreFlightEvent({
handlePreFlightEvent(blocknative, {
eventCode,
status: 'failed',
contractCall,
Expand Down Expand Up @@ -287,7 +287,7 @@ export function preflightTransaction(
) {
const eventCode = 'txStallPending'

handlePreFlightEvent({
handlePreFlightEvent(blocknative, {
eventCode,
contractCall,
balance,
Expand All @@ -308,7 +308,7 @@ export function preflightTransaction(
) {
const eventCode = 'txStallConfirmed'

handlePreFlightEvent({
handlePreFlightEvent(blocknative, {
eventCode,
contractCall,
balance,
Expand Down
16 changes: 16 additions & 0 deletions src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ export function validateConfig(config: ConfigOptions): void {
validateType({ name: 'config', value: config, type: 'object' })

const {
networkId,
system,
mobilePosition,
desktopPosition,
darkMode,
Expand All @@ -313,6 +315,20 @@ export function validateConfig(config: ConfigOptions): void {

invalidParams(otherParams, validInitKeys, 'config / initialize')

validateType({
name: 'networkId',
value: networkId,
type: 'number',
optional: true
})

validateType({
name: 'system',
value: system,
type: 'string',
optional: true
})

validateType({
name: 'mobilePosition',
value: mobilePosition,
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"resolveJsonModule": true,
"target": "ESNEXT",
"declaration": true,
"declarationDir": "dist/types"
"declarationDir": "dist/types",
"isolatedModules": false
},
"include": ["src/**/*"],
"exclude": ["node_modules/*", "dist/*"]
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1274,10 +1274,10 @@ bindings@^1.5.0:
dependencies:
file-uri-to-path "1.0.0"

bnc-sdk@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/bnc-sdk/-/bnc-sdk-2.1.4.tgz#23267198f5a48e800d9c2406f6d04a767cab5643"
integrity sha512-aU7DYweE+6tfTvZE7NOOfQsieU2Zyrav6o/xwuLt+uKGvrkblIeg1aqBW1yAQBEg4LCHEygX6TwZk8VznDAh3g==
bnc-sdk@^2.1.5:
version "2.1.5"
resolved "https://registry.yarnpkg.com/bnc-sdk/-/bnc-sdk-2.1.5.tgz#7f40bcf98eb0238882f5436c0e860e60be2867c0"
integrity sha512-rtwOGKjal1LQyYrdESdOfCK5L2ocS3tjoWtNacm3rkb+xjDusVnUpF/NgudJpCnv3Mwu9YDWjsLKIPKjwbJL7A==
dependencies:
crypto-es "^1.2.2"
sturdy-websocket "^0.1.12"
Expand Down

0 comments on commit f8f5180

Please sign in to comment.