Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.7.0",
"version": "1.7.0-0.1.0",
"description": "Show web3 users realtime transaction notifications",
"keywords": [
"ethereum",
Expand Down
1 change: 1 addition & 0 deletions src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function eventToType(eventCode: string | undefined): NotificationType {
case 'txConfirmReminder':
case 'txStallPending':
case 'txStallConfirmed':
case 'txStuck':
return 'hint'
case 'txError':
case 'txSendFail':
Expand Down
6 changes: 4 additions & 2 deletions src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const defaultNotifyMessages: any = {
txSent: 'Your transaction has been sent to the network',
txStallPending:
'Your transaction has stalled and has not entered the transaction pool',
txStuck: 'Your transaction is stuck due to a nonce gap',
txPool: 'Your transaction has started',
txStallConfirmed:
"Your transaction has stalled and hasn't been confirmed",
Expand All @@ -27,9 +28,9 @@ export const defaultNotifyMessages: any = {
txPool:
'Your account is {verb} {formattedValue} {asset} {preposition} {counterpartyShortened}',
txSpeedUp:
'Your account is {verb} {formattedValue} {asset} {preposition} {counterpartyShortened}',
'Transaction for {formattedValue} {asset} {preposition} {counterpartyShortened} has been sped up',
txCancel:
'Your account is {verb} {formattedValue} {asset} {preposition} {counterpartyShortened}',
'Transaction for {formattedValue} {asset} {preposition} {counterpartyShortened} has been canceled',
txConfirmed:
'Your account successfully {verb} {formattedValue} {asset} {preposition} {counterpartyShortened}',
txFailed:
Expand All @@ -55,6 +56,7 @@ export const defaultNotifyMessages: any = {
txSent: 'Su transacción ha sido enviada a la red.',
txStallPending:
'Su transacción se ha estancado y no ha ingresado al grupo de transacciones',
txStuck: 'Su transacción está atascada debido a una brecha de nonce',
txPool: 'Su transacción ha comenzado',
txStallConfirmed:
'Su transacción se ha estancado y no ha sido confirmada.',
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface TransactionData {
monitorId?: string
monitorVersion?: string
nonce?: number
replaceHash?: string
r?: string
s?: string
status?: string
Expand Down
18 changes: 14 additions & 4 deletions src/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ export const notifications = createNotificationStore([])
function createTransactionStore(initialState: TransactionData[]) {
const { subscribe, update } = writable(initialState)

function updateQueue(transaction: TransactionData) {
const predicate = (tx: TransactionData) => tx.id === transaction.id

function updateQueue(
transaction: TransactionData,
predicate: (tx: TransactionData) => boolean
) {
update((store: TransactionData[]) => {
return replaceOrAdd(store, predicate, transaction)
})
Expand Down Expand Up @@ -88,10 +89,19 @@ function createNotificationStore(
)
}

function updateId(oldId: string, newId: string) {
update((store: (NotificationObject & CustomNotificationObject)[]) =>
store.map((n: NotificationObject & CustomNotificationObject) =>
n.id === oldId ? { ...n, id: newId } : n
)
)
}

return {
subscribe,
add,
remove,
update
update,
updateId
}
}
23 changes: 18 additions & 5 deletions src/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import BigNumber from 'bignumber.js'
import uuid from 'uuid/v4'
import { get } from 'svelte/store'

import { transactions, app } from './stores'
import { transactions, app, notifications } from './stores'
import { createNotification } from './notifications'
import { argsEqual, extractMessageFromError, localNetwork } from './utilities'
import { validateNotificationObject } from './validation'
Expand Down Expand Up @@ -73,13 +73,26 @@ export function handleTransactionEvent(event: {
emitterResult: boolean | void | CustomNotificationObject
}) {
const { transaction, emitterResult } = event
transactions.updateQueue(transaction)
if (!transaction.id) {
transaction.id = transaction.hash || transaction.txid
}

const predicate = (tx: TransactionData) =>
transaction.replaceHash
? tx.id === transaction.replaceHash
: tx.id === transaction.id

transactions.updateQueue(transaction, predicate)

// update notification id if replaced
if (transaction.replaceHash) {
notifications.updateId(transaction.replaceHash, transaction.hash)
}

// create notification if dev hasn't opted out and not connected to a local network
if (emitterResult !== false && !localNetwork(get(app).networkId)) {
const transactionObj = transactionQueue.find(
(tx: TransactionData) => tx.id === transaction.id
)
const transactionObj = transactionQueue.find(predicate)

if (transactionObj) {
createNotification(transactionObj, emitterResult)
}
Expand Down
11 changes: 10 additions & 1 deletion src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ export function replaceOrAdd(
const index = clone.findIndex(predicate)

if (index !== -1) {
const { startTime, contractCall } = clone[index]
const { startTime, contractCall, status } = clone[index]

// if current transaction is a speedup or cancel and new status is pending, ignore update
if (
data.status === 'pending' &&
(status === 'speedup' || status === 'cancel')
) {
return clone
}

const { startTime: serverStartTime } = data
const contractCallMerge = contractCall ? { ...contractCall } : {}

Expand Down