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
65 changes: 58 additions & 7 deletions packages/shared/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type WalletState = {
balanceOverview: Writable<BalanceOverview>
accounts: Writable<WalletAccount[]>
accountsLoaded: Writable<boolean>
confirmedInternalMessageIds: Writable<{ [key: string]: number }>
}

type BalanceTimestamp = {
Expand Down Expand Up @@ -95,6 +96,7 @@ export const wallet = writable<WalletState>({
}),
accounts: writable<WalletAccount[]>([]),
accountsLoaded: writable<boolean>(false),
confirmedInternalMessageIds: writable<{ [key: string]: number }>({})
})

export const resetWallet = () => {
Expand Down Expand Up @@ -361,12 +363,13 @@ export const initialiseListeners = () => {
onSuccess(response) {
const accounts = get(wallet).accounts
const account = get(accounts).find((account) => account.id === response.payload.accountId)
const message = response.payload.message
const messageKey = response.payload.confirmed ? 'confirmed' : 'failed'

const message = response.payload.message
const confirmed = response.payload.confirmed;
const essence = message.payload.data.essence

if (response.payload.confirmed && !essence.data.internal) {

if (confirmed && !essence.data.internal) {
const { balanceOverview } = get(wallet);
const overview = get(balanceOverview);

Expand All @@ -380,8 +383,10 @@ export const initialiseListeners = () => {
);
}

// Update state
const accountMessage = account.messages.find((_message) => _message.id === message.id)
accountMessage.confirmed = response.payload.confirmed

accounts.update((storedAccounts) => {
return storedAccounts.map((storedAccount) => {
if (storedAccount.id === account.id) {
Expand All @@ -402,11 +407,57 @@ export const initialiseListeners = () => {
})
})

const notificationMessage = localize(`notifications.${messageKey}`)
.replace('{{value}}', formatUnit(message.payload.data.essence.data.value))
.replace('{{account}}', account.alias)
// Notify user
const messageKey = confirmed ? 'confirmed' : 'failed'

const _notify = (senderAccountAlias: string | null = null) => {
let notificationMessage

if (senderAccountAlias) {
notificationMessage = localize(`notifications.${messageKey}Internal`)
.replace('{{value}}', formatUnit(message.payload.data.essence.data.value))
.replace('{{senderAccount}}', senderAccountAlias)
.replace('{{receiverAccount}}', account.alias)
} else {
notificationMessage = localize(`notifications.${messageKey}`)
.replace('{{value}}', formatUnit(message.payload.data.essence.data.value))
.replace('{{account}}', account.alias)
}

showSystemNotification({ type: "info", message: notificationMessage })
}

const { confirmedInternalMessageIds } = get(wallet)
const messageIds = get(confirmedInternalMessageIds)

// If this event is emitted because a message failed, then this message will only exist on the sender account
// Therefore, show the notification (no need to group).
if (!confirmed) {
_notify()
} else {
// If this is an external message, notify (no need to group)
if (!essence.data.internal) {
_notify();
} else {
// If this is an internal message, check if we have already receive confirmation state of this message
if (Object.keys(messageIds).includes(message.id)) {
_notify(
get(accounts).find((account) => account.index === messageIds[message.id]).alias
);

confirmedInternalMessageIds.update((ids) => {
delete ids[message.id]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to create a new object here? since the update shouldn't mutate the object AFAIK

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it is fine. It updates all refs and also preserves reactivity.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think we could get away with this with a simple JS object instead of a svelte store, but this looks good.


return ids;
})
} else {
// Otherwise, add the message id and do not notify yet
messageIds[message.id] = account.index
}
}
}


showSystemNotification({ type: "info", message: notificationMessage })
},
onError(error) {
console.error(error)
Expand Down
1 change: 1 addition & 0 deletions packages/shared/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@
"notifications": {
"valueTx": "Receiving {{value}} to {{account}}",
"confirmed": "Outgoing {{value}} from {{account}} has confirmed",
"confirmedInternal": "{{value}} from {{senderAccount}} to {{receiverAccount}} has confirmed",
"failed": "Outgoing {{value}} from {{account}} has failed",
"downloadingUpdate": "Downloading update",
"updateReady": "Update ready",
Expand Down