-
Notifications
You must be signed in to change notification settings - Fork 42
[496] Features: Push notification, Incoming Transaction, Websockets #1739
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
Draft
ifaouibadi
wants to merge
9
commits into
develop
Choose a base branch
from
features/push-notification
base: develop
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.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3408128
feat: websocket client
ifaouibadi 3c01797
feat: push notification integration
ifaouibadi 5a0df6f
feat: incoming transactions listeners
ifaouibadi 99c11e4
feat: improved network watcher
ifaouibadi 3f98130
feat: notifications filters
ifaouibadi eca7ad2
fix: websocket client & push notification
ifaouibadi 82e7001
fix: notification click handler & support fungible tokens incoming tr…
ifaouibadi 809cbc3
feat: handle websocket reconnect
ifaouibadi e1414fc
fix: incoming transaction notification
ifaouibadi 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { useRouter } from 'vue-router'; | ||
| import { useI18n } from 'vue-i18n'; | ||
| import { Encoded } from '@aeternity/aepp-sdk-13'; | ||
| import { TX_FUNCTIONS, TX_TYPE_MDW, roundAmountToPrecision } from '../popup/utils'; | ||
| import { IDefaultComposableOptions, ITransaction } from '../types'; | ||
| import { useAccounts } from './accounts'; | ||
| import { useNotifications } from './notifications'; | ||
| import { useGetter } from './vuex'; | ||
| import { ROUTE_TX_DETAILS } from '../popup/router/routeNames'; | ||
| import WebSocketClient from '../lib/WebSocketClient'; | ||
|
|
||
| export function useIncomingTransactions({ store }: IDefaultComposableOptions) { | ||
| const router = useRouter(); | ||
| const { t } = useI18n(); | ||
|
|
||
| const { addWalletNotification } = useNotifications({ store }); | ||
| const { isLocalAccountAddress, getAccountByAddress, accounts } = useAccounts({ store }); | ||
| const getTxAmountTotal = useGetter<any>('getTxAmountTotal'); | ||
| const getTxSymbol = useGetter<any>('getTxSymbol'); | ||
|
ifaouibadi marked this conversation as resolved.
Outdated
ifaouibadi marked this conversation as resolved.
Outdated
|
||
| let unWatchAccountsUpdates: (() => void)[] = []; | ||
|
|
||
| /** | ||
| * @param {Encoded.AccountAddress} address - the address to format. | ||
| * @returns {string} - returns the account's name if it exists, | ||
| * otherwise it returns the translated string "Account" + the account's index +1. | ||
| */ | ||
| function getAccountNameByAddress(address: Encoded.AccountAddress): string { | ||
| const { idx, name } = getAccountByAddress(address) || {}; | ||
|
|
||
| if (name) { | ||
| return name; | ||
| } | ||
|
|
||
| if (idx) { | ||
| return `${t('pages.account.heading')} ${idx + 1}`; | ||
| } | ||
|
|
||
| return address; | ||
| } | ||
|
|
||
| /** | ||
| * This function starts listening for incoming transactions on the 'Transactions' channel. | ||
| */ | ||
| function startListeningForIncomingTransactions() { | ||
| const receivedTransactions = new Set<string>(); | ||
|
|
||
| unWatchAccountsUpdates = accounts.value.map( | ||
| (account) => WebSocketClient.subscribeForAccountUpdates( | ||
| account.address, | ||
| (transaction: ITransaction) => { | ||
| if (!transaction?.tx) return; | ||
| const { hash, tx } = transaction; | ||
| let { senderId, recipientId } = tx; | ||
|
|
||
| if ( | ||
| TX_TYPE_MDW[tx.type] === TX_TYPE_MDW.ContractCallTx | ||
| && tx.function === TX_FUNCTIONS.transfer | ||
| ) { | ||
| senderId = tx.callerId; | ||
| recipientId = tx.arguments.find((arg) => arg.type === 'address')?.value; | ||
| } else if (TX_TYPE_MDW[tx.type] !== TX_TYPE_MDW.SpendTx) { | ||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| recipientId | ||
| && isLocalAccountAddress(recipientId as Encoded.AccountAddress) | ||
| && !receivedTransactions.has(hash) | ||
| ) { | ||
| receivedTransactions.add(hash); | ||
|
|
||
| const amount = roundAmountToPrecision(getTxAmountTotal.value(transaction)); | ||
| const title = t('pages.notifications.incomingTransaction'); | ||
| const text = `${getAccountNameByAddress(recipientId as Encoded.AccountAddress)} ${t('pages.transactions.received')} ${amount} ${getTxSymbol.value(transaction)}`; | ||
|
|
||
| addWalletNotification({ | ||
| id: hash, | ||
| text, | ||
| title, | ||
| buttonLabel: t('pages.notifications.viewTransaction'), | ||
| path: router.resolve({ | ||
| name: ROUTE_TX_DETAILS, | ||
| params: { | ||
| hash, | ||
| transactionOwner: recipientId, | ||
| }, | ||
| }).href, | ||
| sender: senderId, | ||
| receiver: recipientId, | ||
| hasIncomingTransaction: true, | ||
| pushNotification: true, | ||
| }); | ||
| } | ||
| }, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| function stopListeningForIncomingTransactions() { | ||
| unWatchAccountsUpdates.forEach((unWatch) => unWatch()); | ||
| } | ||
|
|
||
| return { | ||
| startListeningForIncomingTransactions, | ||
| stopListeningForIncomingTransactions, | ||
| }; | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes done in this file are not necessary as non of the files are using them.