-
Notifications
You must be signed in to change notification settings - Fork 52
Add browser notifications for wallet transactions #1095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jinhojang6
wants to merge
29
commits into
main
Choose a base branch
from
wallet-notifications
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
cb724a7
feat(wallet): add notifications permission to manifest
jinhojang6 73085df
feat(wallet): add Chrome notifications helper
jinhojang6 e4bed24
fix(wallet): await chrome.notifications.create and fix vi.mock ordering
jinhojang6 1e58673
feat(wallet): add background transaction monitor
jinhojang6 c3e7f74
feat(wallet): register tx-monitor alarm in background service worker
jinhojang6 f1172e4
fix(wallet): cast storage mock through unknown for TypeScript
jinhojang6 83989a6
feat(wallet): notify user when transaction is sent
jinhojang6 b4a74f3
feat(wallet): add settings page with notification toggle
jinhojang6 aad359f
feat(wallet): add settings navigation and route guard
jinhojang6 2a74301
fix(wallet): notify confirmation from popup cleanup hook to handle po…
jinhojang6 1afee5e
fix(wallet): notification quality fixes (precision, disable, dedup, e…
jinhojang6 d0af15f
chore(wallet): regenerate onboarding route token after settings route…
jinhojang6 9776c18
fix(wallet): update pending transaction handling to use async/await f…
jinhojang6 583fff4
chore: add changeset for browser notifications for wallet transactions
jinhojang6 8489c3e
refactor(wallet): streamline pending transactions handling and improv…
jinhojang6 50ec9f0
refactor(wallet): enhance settings page layout and add back navigation
jinhojang6 3a34085
fix(wallet): improve error handling for notifications and transaction…
jinhojang6 f0d18bd
fix(wallet): add hasPendingTransactions check before processing pendi…
jinhojang6 b9e3add
refactor(wallet): optimize pending transactions handling and receipt …
jinhojang6 cc88d85
refactor(wallet): consolidate transaction hash extraction logic into …
jinhojang6 fe50c3a
refactor(notifications): unify notification creation logic and simpli…
jinhojang6 69f43be
feat(wallet): dynamic tx monitor alarm - start on pending, clear when…
jinhojang6 710a580
test(wallet): remove outdated tx-monitor tests
jinhojang6 33eda97
Merge branch 'main' into wallet-notifications
jinhojang6 effa64c
Merge branch 'main' into wallet-notifications
jinhojang6 6a1b96e
refactor(tx-monitor): extract interval constant for alarm creation
jinhojang6 1458916
fix: pass network parameter to fetchNetworkNonce in getNonceHex function
jinhojang6 54acb45
feat(notifications): implement notification permission check and stor…
jinhojang6 3c0dac0
fix(settings): add gap between notification toggle and description
jinhojang6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'wallet': patch | ||
| --- | ||
|
|
||
| add browser notifications for wallet transactions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
apps/wallet/src/hooks/use-notification-permission-check.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { useEffect } from 'react' | ||
|
|
||
| import { useToast } from '@status-im/components' | ||
| import { storage } from '@wxt-dev/storage' | ||
|
|
||
| import { NOTIFICATION_PROMPTED_KEY } from '../lib/storage-keys' | ||
|
|
||
| export function useNotificationPermissionCheck() { | ||
| const toast = useToast() | ||
|
|
||
| useEffect(() => { | ||
| storage.getItem<boolean>(NOTIFICATION_PROMPTED_KEY).then(prompted => { | ||
| if (prompted) return | ||
|
|
||
| storage.setItem(NOTIFICATION_PROMPTED_KEY, true) | ||
| toast.positive( | ||
| 'To receive transaction alerts, make sure notifications are enabled for this browser in System Settings.', | ||
| ) | ||
| }) | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, []) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { storage } from '@wxt-dev/storage' | ||
|
|
||
| import { NOTIFICATIONS_ENABLED_KEY } from './storage-keys' | ||
|
|
||
| async function isEnabled(): Promise<boolean> { | ||
| try { | ||
| return (await storage.getItem<boolean>(NOTIFICATIONS_ENABLED_KEY)) ?? true | ||
| } catch (error) { | ||
| console.warn('Failed to read notification setting', error) | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| async function createNotification( | ||
| title: string, | ||
| message: string, | ||
| ): Promise<boolean> { | ||
| try { | ||
| if (!(await isEnabled())) return false | ||
| chrome.notifications.create({ | ||
| type: 'basic', | ||
| iconUrl: chrome.runtime.getURL('icons/128.png'), | ||
| title, | ||
| message, | ||
| }) | ||
| return true | ||
| } catch (error) { | ||
| console.warn('Failed to create notification', error) | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| export async function notifyTransactionSent( | ||
| amount: string, | ||
| asset: string, | ||
| ): Promise<boolean> { | ||
| return createNotification('Transaction Sent', `Sending ${amount} ${asset}…`) | ||
| } | ||
|
|
||
| export async function notifyTransactionConfirmed( | ||
| amount: string, | ||
| asset: string, | ||
| ): Promise<boolean> { | ||
| return createNotification( | ||
| 'Transaction Confirmed', | ||
| `${amount} ${asset} sent successfully`, | ||
| ) | ||
| } | ||
|
|
||
| export async function notifyTransactionFailed( | ||
| amount: string, | ||
| asset: string, | ||
| ): Promise<boolean> { | ||
| return createNotification( | ||
| 'Transaction Failed', | ||
| `${amount} ${asset} transaction failed`, | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export const NOTIFICATIONS_ENABLED_KEY = 'local:notifications:enabled' as const | ||
| export const TX_NOTIFIED_KEY = 'local:tx-monitor:notified' as const | ||
| export const PENDING_TXS_KEY = | ||
| 'local:pending-transactions:transactions' as const | ||
| export const NOTIFICATION_PROMPTED_KEY = 'local:notifications:prompted' as const |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| export function extractTxHash(hash: unknown): string | null { | ||
| if (typeof hash === 'string') return hash | ||
| if (hash && typeof hash === 'object') { | ||
| const obj = hash as Record<string, unknown> | ||
| if ('result' in obj && typeof obj.result === 'string') return obj.result | ||
| if ('txid' in obj && typeof obj.txid === 'string') return obj.txid | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| export function truncateAddress(address: string, chars = 5): string { | ||
| return `${address.slice(0, chars)}...${address.slice(-chars)}` | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { storage } from '@wxt-dev/storage' | ||
|
|
||
| import { | ||
| notifyTransactionConfirmed, | ||
| notifyTransactionFailed, | ||
| } from './notifications' | ||
| import { PENDING_TXS_KEY, TX_NOTIFIED_KEY } from './storage-keys' | ||
| import { extractTxHash } from './tx-helpers' | ||
|
|
||
| type PendingTx = { | ||
| hash: unknown | ||
| value: number | ||
| asset: string | ||
| displayAmount?: string | ||
| } | ||
|
|
||
| async function fetchReceipt( | ||
| txHash: string, | ||
| ): Promise<{ status: string } | null> { | ||
| try { | ||
| const url = `${import.meta.env.WXT_STATUS_API_URL}/api/trpc/rpc.proxy` | ||
| const res = await fetch(url, { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify({ | ||
| json: { | ||
| method: 'eth_getTransactionReceipt', | ||
| params: [txHash], | ||
| id: 1, | ||
| chainId: 1, | ||
| }, | ||
| }), | ||
| }) | ||
| const body = (await res.json()) as { | ||
| result: { data: { json: { result: { status: string } | null } } } | ||
| } | ||
| return body.result.data.json.result | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| export const TX_MONITOR_ALARM = 'tx-monitor' | ||
| // 0.5 minutes = 30 seconds, minimum allowed by Chrome MV3 | ||
| const TX_MONITOR_INTERVAL_MINUTES = 0.5 | ||
|
|
||
| export async function startTxMonitor(): Promise<void> { | ||
| const existing = await chrome.alarms.get(TX_MONITOR_ALARM) | ||
| if (!existing) { | ||
| chrome.alarms.create(TX_MONITOR_ALARM, { | ||
| periodInMinutes: TX_MONITOR_INTERVAL_MINUTES, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| export async function checkPendingTransactions(): Promise<void> { | ||
| const pendingTxs = (await storage.getItem<PendingTx[]>(PENDING_TXS_KEY)) ?? [] | ||
|
|
||
| if (pendingTxs.length === 0) { | ||
| await chrome.alarms.clear(TX_MONITOR_ALARM) | ||
| const notified = await storage.getItem<string[]>(TX_NOTIFIED_KEY) | ||
| if (Array.isArray(notified) && notified.length > 0) { | ||
| await storage.setItem(TX_NOTIFIED_KEY, []) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| const notified = (await storage.getItem<string[]>(TX_NOTIFIED_KEY)) ?? [] | ||
| const notifiedSet = new Set(notified) | ||
| const newlyNotified: string[] = [] | ||
| const settledHashes = new Set<string>() | ||
|
|
||
| const receipts = await Promise.all( | ||
| pendingTxs.map(tx => { | ||
| const txHash = extractTxHash(tx.hash) | ||
| return txHash | ||
| ? fetchReceipt(txHash).then(r => ({ tx, txHash, receipt: r })) | ||
| : null | ||
| }), | ||
| ) | ||
|
|
||
| for (const entry of receipts) { | ||
| if (!entry || !entry.receipt) continue | ||
|
|
||
| const { tx, txHash, receipt } = entry | ||
| const alreadyNotified = notifiedSet.has(txHash) | ||
| const amount = tx.displayAmount ?? String(tx.value) | ||
| const asset = tx.asset ?? 'ETH' | ||
|
|
||
| if (receipt.status === '0x1') { | ||
| if (!alreadyNotified) { | ||
| const fired = await notifyTransactionConfirmed(amount, asset) | ||
| if (fired) { | ||
| newlyNotified.push(txHash) | ||
| notifiedSet.add(txHash) | ||
| } | ||
| } | ||
| settledHashes.add(txHash) | ||
| } else if (receipt.status === '0x0') { | ||
| if (!alreadyNotified) { | ||
| const fired = await notifyTransactionFailed(amount, asset) | ||
| if (fired) { | ||
| newlyNotified.push(txHash) | ||
| notifiedSet.add(txHash) | ||
| } | ||
| } | ||
| settledHashes.add(txHash) | ||
| } | ||
| } | ||
|
|
||
| if (settledHashes.size > 0) { | ||
| const remaining = pendingTxs.filter(tx => { | ||
| const txHash = extractTxHash(tx.hash) | ||
| return !txHash || !settledHashes.has(txHash) | ||
| }) | ||
| await storage.setItem(PENDING_TXS_KEY, remaining) | ||
|
|
||
| if (remaining.length === 0) { | ||
| await chrome.alarms.clear(TX_MONITOR_ALARM) | ||
| } | ||
| } | ||
|
|
||
| if (newlyNotified.length > 0) { | ||
| await storage.setItem(TX_NOTIFIED_KEY, [...notified, ...newlyNotified]) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.