Skip to content

Commit

Permalink
Merge pull request #171 from blocknative/develop
Browse files Browse the repository at this point in the history
Release 1.4.0
  • Loading branch information
lnbc1QWFyb24 authored Sep 28, 2020
2 parents f8f5180 + 39c324c commit 5a30c98
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 141 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bnc-notify",
"version": "1.3.1",
"version": "1.4.0",
"description": "Show web3 users realtime transaction notifications",
"keywords": [
"ethereum",
Expand Down
15 changes: 6 additions & 9 deletions src/components/AutoDismiss.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<script lang="ts">
import type {
CustomNotificationObject,
NotificationObject
} from '../interfaces'
import { notifications } from '../stores'
export let notification: {
id: string
type: string
key: string
startTime?: number
eventCode: string
message: string
autoDismiss?: number
} = { id: '', type: '', key: '', message: '', eventCode: '' }
export let notification: NotificationObject & CustomNotificationObject
if (notification.autoDismiss && notification.id) {
setTimeout(() => {
Expand Down
14 changes: 5 additions & 9 deletions src/components/NotificationContent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import Clock from '../elements/Clock.svelte'
import Time from '../elements/Time.svelte'
import Timer from '../elements/Timer.svelte'
import type {
CustomNotificationObject,
NotificationObject
} from '../interfaces'
function timeString(time: number): string {
const seconds = Math.floor(time / 1000)
Expand All @@ -28,15 +32,7 @@
})
}
export let notification: {
id: string
type: string
key: string
startTime?: number
eventCode?: string
message: string
autoDismiss?: number
}
export let notification: NotificationObject & CustomNotificationObject
let currentTime = Date.now()
Expand Down
55 changes: 20 additions & 35 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import type {
} from 'bnc-sdk/dist/types/src/interfaces'

export interface InitOptions extends ConfigOptions {
dappId: string
networkId: number
dappId?: string
transactionHandler?: TransactionHandler
name?: string
apiUrl?: string
Expand Down Expand Up @@ -43,7 +42,7 @@ export interface TransactionData {
asset?: string
blockHash?: string
blockNumber?: number
contractCall?: ContractObject
contractCall?: ContractCall | DecodedContractCall
counterparty?: string
eventCode?: string
from?: string
Expand Down Expand Up @@ -88,7 +87,7 @@ export interface BitcoinInputOutput {
}

export interface NotificationObject {
id?: string
id: string
type: NotificationType
key: string
startTime?: number
Expand All @@ -97,18 +96,23 @@ export interface NotificationObject {
autoDismiss?: number
}

export interface ContractObject {
export interface ContractCall {
methodName: string
params: string[]
}

export interface DecodedContractCall {
contractAddress?: string
contractType?: string
methodName: string
params: object
methodName: string
}

export interface AppStore {
version: string
dappId: string
dappId?: string
name?: string
networkId: number
networkId?: number
nodeSynced: boolean
mobilePosition: 'bottom' | 'top'
desktopPosition: 'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight'
Expand Down Expand Up @@ -137,21 +141,21 @@ export interface LocaleMessages {
}

export interface TransactionOptions {
sendTransaction: () => Promise<string>
estimateGas: () => Promise<string>
gasPrice: () => Promise<string>
balance: string
contractCall: ContractObject
txDetails: {
sendTransaction?: () => Promise<string>
estimateGas?: () => Promise<string>
gasPrice?: () => Promise<string>
balance?: string
contractCall?: ContractCall
txDetails?: {
to?: string
value: string
from?: string
value: string
}
}

export interface PreflightEvent {
eventCode: string
contractCall?: ContractObject
contractCall?: ContractCall
balance: string
txDetails?: {
to?: string
Expand Down Expand Up @@ -245,22 +249,3 @@ export interface NotificationDetails {
value?: string
asset?: string
}

export interface WritableStore {
set: (newValue: any) => void
update: (newValue: any) => void
subscribe: (callback: (store: any) => any) => () => void
}

export interface TransactionStore {
subscribe: (callback: (store: any) => any) => void
updateQueue: (transaction: TransactionData) => void
add: (transaction: TransactionData) => void
}

export interface NotificationStore {
subscribe: (callback: (store: any) => any) => void
add: (notification: NotificationObject) => void
remove: (id: string, eventCode: string) => void
update: (updater: (store: any) => any) => void
}
72 changes: 48 additions & 24 deletions src/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export {
CustomNotificationObject,
BitcoinInputOutput,
NotificationObject,
ContractObject,
AppStore,
ContractCall,
DecodedContractCall,
NotifyMessages,
LocaleMessages,
TransactionOptions,
Expand All @@ -52,10 +52,7 @@ export {
API,
EmitterListener,
Emitter,
NotificationDetails,
WritableStore,
TransactionStore,
NotificationStore
NotificationDetails
} from './interfaces'

import {
Expand All @@ -79,35 +76,32 @@ function init(options: InitOptions): API {

validateInit(options)

const {
dappId,
system,
networkId,
transactionHandler,
name,
apiUrl,
clientLocale
} = options
const { system, transactionHandler, apiUrl, ...appOptions } = options
const { dappId, networkId, name, clientLocale } = appOptions

const transactionHandlers: TransactionHandler[] = [handleTransactionEvent]

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

let blocknative = new BlocknativeSdk({
dappId,
networkId,
transactionHandlers,
name: name || 'Notify',
apiUrl,
system
})
let blocknative

if (dappId) {
blocknative = new BlocknativeSdk({
dappId,
networkId,
transactionHandlers,
name: name || 'Notify',
apiUrl,
system
})
}

// save config to app store
app.update((store: AppStore) => ({
...store,
...options,
...appOptions,
version,
clientLocale:
clientLocale ||
Expand Down Expand Up @@ -145,6 +139,12 @@ function init(options: InitOptions): API {
function account(
address: string
): { details: { address: string }; emitter: Emitter } | never {
if (!blocknative) {
throw new Error(
'A dappId needs to be passed in when intializing Notify to use the account function'
)
}

try {
const result = blocknative.account(address)
return result
Expand All @@ -154,6 +154,12 @@ function init(options: InitOptions): API {
}

function hash(hash: string, id?: string) {
if (!blocknative) {
throw new Error(
'A dappId needs to be passed in when intializing Notify to use the hash function'
)
}

try {
const result = blocknative.transaction(hash, id)
return result
Expand All @@ -165,6 +171,12 @@ function init(options: InitOptions): API {
function transaction(
options: TransactionOptions
): { result: Promise<string>; emitter: Emitter } {
if (!blocknative) {
throw new Error(
'A dappId needs to be passed in when intializing Notify to use the transaction function'
)
}

validateTransactionOptions(options)

const emitter = createEmitter()
Expand All @@ -180,6 +192,12 @@ function init(options: InitOptions): API {
}

function unsubscribe(addressOrHash: string) {
if (!blocknative) {
throw new Error(
'A dappId needs to be passed in when intializing Notify to use the unsubscribe function'
)
}

blocknative.unsubscribe(addressOrHash)
}

Expand Down Expand Up @@ -243,6 +261,12 @@ function init(options: InitOptions): API {
(newNetworkId && newNetworkId !== networkId) ||
(newSystem && newSystem !== system)
) {
if (!blocknative) {
throw new Error(
'A dappId needs to be passed in when intializing Notify to be able to connect to a system and network'
)
}

// close existing SDK connection
blocknative.destroy()

Expand Down
37 changes: 22 additions & 15 deletions src/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { replaceOrAdd } from './utilities'
import { defaultNotifyMessages } from './i18n'

import type {
WritableStore,
TransactionData,
TransactionStore,
NotificationObject,
NotificationStore
CustomNotificationObject,
AppStore
} from './interfaces'

export const app: WritableStore = writable({
export const app = writable<AppStore>({
version: '',
name: '',
dappId: '',
networkId: 1,
nodeSynced: true,
Expand All @@ -24,10 +24,10 @@ export const app: WritableStore = writable({
clientLocale: 'en',
notifyMessages: defaultNotifyMessages
})
export const accounts: WritableStore = writable([])
export const contracts: WritableStore = writable([])
export const transactions: TransactionStore = createTransactionStore([])
export const notifications: NotificationStore = createNotificationStore([])
export const accounts = writable([])
export const contracts = writable([])
export const transactions = createTransactionStore([])
export const notifications = createNotificationStore([])

function createTransactionStore(initialState: TransactionData[]) {
const { subscribe, update } = writable(initialState)
Expand All @@ -51,13 +51,16 @@ function createTransactionStore(initialState: TransactionData[]) {
}
}

function createNotificationStore(initialState: NotificationObject[]) {
function createNotificationStore(
initialState: (NotificationObject & CustomNotificationObject)[]
) {
const { subscribe, update } = writable(initialState)

function add(notification: NotificationObject) {
update((store: NotificationObject[]) => {
function add(notification: NotificationObject & CustomNotificationObject) {
update((store: (NotificationObject & CustomNotificationObject)[]) => {
const existingNotification = store.find(
(n: NotificationObject) => n.id === notification.id
(n: NotificationObject & CustomNotificationObject) =>
n.id === notification.id
)

// if notification is a hint type or there are no existing notifications with same id, then just add it.
Expand All @@ -67,16 +70,20 @@ function createNotificationStore(initialState: NotificationObject[]) {

// otherwise filter out all notifications with the same id and then add the new notification
return [
...store.filter((n: NotificationObject) => n.id !== notification.id),
...store.filter(
(n: NotificationObject & CustomNotificationObject) =>
n.id !== notification.id
),
notification
]
})
}

function remove(id: string, eventCode: string) {
update((store: NotificationObject[]) =>
update((store: (NotificationObject & CustomNotificationObject)[]) =>
store.filter(
(n: NotificationObject) => n.id !== id || n.eventCode !== eventCode
(n: NotificationObject & CustomNotificationObject) =>
n.id !== id || n.eventCode !== eventCode
)
)
}
Expand Down
Loading

0 comments on commit 5a30c98

Please sign in to comment.