diff --git a/package.json b/package.json index 03265d5..7bc7cc7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bnc-notify", - "version": "1.3.0", + "version": "1.3.1", "description": "Show web3 users realtime transaction notifications", "keywords": [ "ethereum", @@ -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" diff --git a/src/interfaces.ts b/src/interfaces.ts index 899e06f..558e8fd 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -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 { @@ -166,6 +170,8 @@ export interface UpdateNotification { } export interface ConfigOptions { + system?: System + networkId?: number mobilePosition?: 'bottom' | 'top' desktopPosition?: 'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight' darkMode?: boolean @@ -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 { @@ -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 } diff --git a/src/notify.ts b/src/notify.ts index bc9d0f1..8ecc8e8 100644 --- a/src/notify.ts +++ b/src/notify.ts @@ -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' @@ -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, @@ -25,7 +24,7 @@ import type { LocaleMessages } from './interfaces' -export type { +export { InitOptions, TransactionHandler, TransactionEvent, @@ -51,7 +50,6 @@ export type { Notification, Config, API, - TransactionLog, EmitterListener, Emitter, NotificationDetails, @@ -97,7 +95,7 @@ function init(options: InitOptions): API { transactionHandlers.push(transactionHandler) } - const blocknative = getBlocknative({ + let blocknative = new BlocknativeSdk({ dappId, networkId, transactionHandlers, @@ -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 @@ -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, @@ -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 } diff --git a/src/services.ts b/src/services.ts deleted file mode 100644 index 744c969..0000000 --- a/src/services.ts +++ /dev/null @@ -1,20 +0,0 @@ -import BlocknativeSdk from 'bnc-sdk' - -import type { TransactionHandler, System } from './interfaces' - -let blocknative: any - -export function getBlocknative(options?: { - dappId: string - networkId: number - transactionHandlers: TransactionHandler[] - name: string - apiUrl?: string - system?: System -}): any { - if (!blocknative && options) { - blocknative = new BlocknativeSdk(options) - } - - return blocknative -} diff --git a/src/transactions.ts b/src/transactions.ts index 4efd5a6..1b56b2c 100644 --- a/src/transactions.ts +++ b/src/transactions.ts @@ -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, @@ -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, @@ -29,8 +32,6 @@ export function handlePreFlightEvent(preflightEvent: PreflightEvent) { status } = preflightEvent - const blocknative = getBlocknative() - blocknative.event({ categoryCode: contractCall ? 'activeContract' : 'activeTransaction', eventCode, @@ -116,6 +117,7 @@ export function duplicateTransactionCandidate( } export function preflightTransaction( + blocknative, options: TransactionOptions, emitter: Emitter ): Promise { @@ -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 ==// @@ -164,7 +164,7 @@ export function preflightTransaction( if (transactionCost.gt(new BigNumber(balance))) { const eventCode = 'nsfFail' - handlePreFlightEvent({ + handlePreFlightEvent(blocknative, { eventCode, contractCall, balance, @@ -186,7 +186,7 @@ export function preflightTransaction( ) { const eventCode = 'txRepeat' - handlePreFlightEvent({ + handlePreFlightEvent(blocknative, { eventCode, contractCall, balance, @@ -205,7 +205,7 @@ export function preflightTransaction( if (transactionQueue.find(tx => tx.status === 'awaitingApproval')) { const eventCode = 'txAwaitingApproval' - handlePreFlightEvent({ + handlePreFlightEvent(blocknative, { eventCode, contractCall, balance, @@ -223,7 +223,7 @@ export function preflightTransaction( if (awaitingApproval) { const eventCode = 'txConfirmReminder' - handlePreFlightEvent({ + handlePreFlightEvent(blocknative, { eventCode, contractCall, balance, @@ -233,7 +233,7 @@ export function preflightTransaction( } }, txApproveReminderTimeout) - handlePreFlightEvent({ + handlePreFlightEvent(blocknative, { eventCode: 'txRequest', status: 'awaitingApproval', contractCall, @@ -255,7 +255,7 @@ export function preflightTransaction( } catch (error) { const { eventCode, errorMsg } = extractMessageFromError(error) - handlePreFlightEvent({ + handlePreFlightEvent(blocknative, { eventCode, status: 'failed', contractCall, @@ -287,7 +287,7 @@ export function preflightTransaction( ) { const eventCode = 'txStallPending' - handlePreFlightEvent({ + handlePreFlightEvent(blocknative, { eventCode, contractCall, balance, @@ -308,7 +308,7 @@ export function preflightTransaction( ) { const eventCode = 'txStallConfirmed' - handlePreFlightEvent({ + handlePreFlightEvent(blocknative, { eventCode, contractCall, balance, diff --git a/src/validation.ts b/src/validation.ts index 24ce0f2..69b97af 100644 --- a/src/validation.ts +++ b/src/validation.ts @@ -300,6 +300,8 @@ export function validateConfig(config: ConfigOptions): void { validateType({ name: 'config', value: config, type: 'object' }) const { + networkId, + system, mobilePosition, desktopPosition, darkMode, @@ -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, diff --git a/tsconfig.json b/tsconfig.json index 119a8e6..220a523 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,8 @@ "resolveJsonModule": true, "target": "ESNEXT", "declaration": true, - "declarationDir": "dist/types" + "declarationDir": "dist/types", + "isolatedModules": false }, "include": ["src/**/*"], "exclude": ["node_modules/*", "dist/*"] diff --git a/yarn.lock b/yarn.lock index 90856dc..76a0628 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"