-
Notifications
You must be signed in to change notification settings - Fork 0
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
added transaction tracking #51
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import { refreshAccount } from 'utils/account'; | ||
import { checkBatch } from './checkBatch'; | ||
import { TransactionsTrackerType } from '../../trackTransactions.types'; | ||
import { getPendingStoreTransactions } from '../getPendingStoreTransactions'; | ||
import { getPendingStoreTrackedTransactions } from '../getPendingStoreTrackedTransactions'; | ||
|
||
export async function checkTransactionStatus( | ||
props: TransactionsTrackerType & { | ||
shouldRefreshBalance?: boolean; | ||
} | ||
) { | ||
const { pendingSessions } = getPendingStoreTransactions(); | ||
const { pendingTrackedSessions: pendingSessions } = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not keep sessions, since we don't have any other sessions |
||
getPendingStoreTrackedTransactions(); | ||
if (Object.keys(pendingSessions).length > 0) { | ||
for (const [sessionId, { transactions }] of Object.entries( | ||
pendingSessions | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { | ||
pendingTrackedSessionsSelector, | ||
pendingTrackedTransactionsSelector | ||
} from 'store/selectors/trackedTransactionsSelector'; | ||
import { TrackedTransactionsSliceType } from 'store/slices/trackedTransactions/trackedTransactionsSlice.types'; | ||
import { getState } from 'store/store'; | ||
import { SignedTransactionType } from 'types/transactions.types'; | ||
|
||
export interface UseGetPendingTrackedTransactionsReturnType { | ||
pendingTrackedTransactions: SignedTransactionType[]; | ||
pendingTrackedSessions: TrackedTransactionsSliceType; | ||
hasPendingTrackedTransactions: boolean; | ||
} | ||
|
||
export function getPendingStoreTrackedTransactions(): UseGetPendingTrackedTransactionsReturnType { | ||
const pendingTrackedTransactions = | ||
pendingTrackedTransactionsSelector(getState()); | ||
const pendingTrackedSessions = pendingTrackedSessionsSelector(getState()); | ||
const hasPendingTrackedTransactions = pendingTrackedTransactions.length > 0; | ||
|
||
return { | ||
pendingTrackedTransactions, | ||
pendingTrackedSessions, | ||
hasPendingTrackedTransactions | ||
}; | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,34 @@ | ||
import { getTransactionsByHashes as defaultGetTxByHash } from 'apiCalls/transactions/getTransactionsByHashes'; | ||
import { getTransactionsByHashes } from 'apiCalls/transactions/getTransactionsByHashes'; | ||
import { websocketEventSelector } from 'store/selectors/accountSelectors'; | ||
import { getStore } from 'store/store'; | ||
import { checkTransactionStatus } from './helpers/checkTransactionStatus'; | ||
import { getPollingInterval } from './helpers/getPollingInterval'; | ||
import { TransactionsTrackerType } from './trackTransactions.types'; | ||
import { | ||
websocketConnection, | ||
WebsocketConnectionStatusEnum | ||
} from '../initApp/websocket/websocket.constants'; | ||
|
||
/** | ||
* Tracks transactions using websocket or polling | ||
* @param props - optional object with additional websocket parameters | ||
* @returns cleanup function | ||
*/ | ||
export async function trackTransactions(props?: TransactionsTrackerType) { | ||
export async function trackTransactions() { | ||
const store = getStore(); | ||
const pollingInterval = getPollingInterval(); | ||
// eslint-disable-next-line no-undef | ||
let pollingIntervalTimer: NodeJS.Timeout | null = null; | ||
let timestamp = websocketEventSelector(store.getState())?.timestamp; | ||
|
||
// Check if websocket is completed | ||
const isWebsocketCompleted = | ||
websocketConnection.status === WebsocketConnectionStatusEnum.COMPLETED; | ||
|
||
// Assign getTransactionsByHash based on props or use default | ||
const getTransactionsByHash = | ||
props?.getTransactionsByHash ?? defaultGetTxByHash; | ||
|
||
// Function that handles message (checking transaction status) | ||
const recheckStatus = () => { | ||
checkTransactionStatus({ | ||
shouldRefreshBalance: isWebsocketCompleted, | ||
getTransactionsByHash, | ||
...props | ||
getTransactionsByHash: getTransactionsByHashes | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. strange name difference. & why not use it as a default in checkTransactionStatus and allow optional override There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed it and used as default in checkTransactionStatus |
||
}; | ||
|
||
// recheck on page initial page load | ||
recheckStatus(); | ||
|
||
if (isWebsocketCompleted) { | ||
|
@@ -60,7 +50,6 @@ export async function trackTransactions(props?: TransactionsTrackerType) { | |
} | ||
} | ||
|
||
// Return cleanup function for clearing the interval | ||
function cleanup() { | ||
if (pollingIntervalTimer) { | ||
clearInterval(pollingIntervalTimer); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
CustomToastType, | ||
ToastsEnum | ||
} from 'store/slices/toast/toastSlice.types'; | ||
import { getStore } from 'store/store'; | ||
import { getUnixTimestamp } from 'utils'; | ||
|
||
export const addCustomToast = ( | ||
customToasts: CustomToastType, | ||
currentToastId?: string | ||
) => { | ||
getStore().setState(({ toasts: state }) => { | ||
const toastId = | ||
currentToastId || `custom-toast-${state.customToasts.length + 1}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add toast 0 --> custom-toast-1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
const existingToastIndex = state.customToasts.findIndex( | ||
(toast) => toast.toastId === toastId | ||
); | ||
|
||
if (existingToastIndex !== -1) { | ||
state.customToasts[existingToastIndex] = { | ||
...state.customToasts[existingToastIndex], | ||
...customToasts, | ||
type: ToastsEnum.custom, | ||
toastId | ||
} as CustomToastType; | ||
return; | ||
} | ||
|
||
state.customToasts.push({ | ||
...customToasts, | ||
type: ToastsEnum.custom, | ||
toastId | ||
}); | ||
}); | ||
}; | ||
|
||
export const removeCustomToast = (toastId: string) => { | ||
getStore().setState(({ toasts: state }) => { | ||
state.customToasts = state.customToasts.filter( | ||
(toast) => toast.toastId !== toastId | ||
); | ||
}); | ||
}; | ||
|
||
export const addTransactionToast = (toastId: string) => { | ||
getStore().setState(({ toasts: state }) => { | ||
state.transactionToasts.push({ | ||
type: ToastsEnum.transaction, | ||
startTimestamp: getUnixTimestamp(), | ||
toastId: | ||
toastId || `transaction-toast-${state.transactionToasts.length + 1}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check vulenrability here if there is one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i did the same thing as in custom case |
||
}); | ||
}); | ||
}; | ||
|
||
export const removeTransactionToast = (toastId: string) => { | ||
getStore().setState(({ toasts: state }) => { | ||
state.transactionToasts = state.transactionToasts.filter((toast) => { | ||
return toast.toastId !== toastId; | ||
}); | ||
}); | ||
}; |
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.
disableToasts boolean default undefined ?
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.
i change it to default {enableToasts:true}