Skip to content
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

fix: clear stale pending tx #913

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useMemo } from 'react'
import { Popover } from '@headlessui/react'
import dayjs from 'dayjs'

import { NodeBlockDeadlineStatusTypes } from '../../../hooks/arbTokenBridge.types'
import { MergedTransaction } from '../../../state/app/state'
Expand All @@ -9,7 +8,7 @@ import { useNetworksAndSigners } from '../../../hooks/useNetworksAndSigners'
import { useClaimWithdrawal } from '../../../hooks/useClaimWithdrawal'
import { WithdrawalCountdown } from '../../common/WithdrawalCountdown'
import { ExternalLink } from '../../common/ExternalLink'
import { shortenTxHash } from '../../../util/CommonUtils'
import { isTxOlderThan7Days, shortenTxHash } from '../../../util/CommonUtils'
import { Button } from '../../common/Button'
import { Tooltip } from '../../common/Tooltip'
import { getExplorerUrl, getNetworkName } from '../../../util/networks'
Expand Down Expand Up @@ -306,11 +305,9 @@ function WithdrawalRowAction({
}

if (isError) {
const isTxOlderThan7Days = dayjs().diff(dayjs(tx.createdAt), 'days') > 7

return (
<>
{isTxOlderThan7Days ? (
{isTxOlderThan7Days(tx.createdAt) ? (
// show a dropdown menu with the button
<Popover>
<Popover.Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Transaction, txnTypeToLayer } from '../../hooks/useTransactions'
import { useActions, useAppState } from '../../state'
import { useInterval } from '../common/Hooks'
import { useNetworksAndSigners } from '../../hooks/useNetworksAndSigners'
import { isTxOlderThan7Days } from '../../util/CommonUtils'

export function PendingTransactionsUpdater(): JSX.Element {
const actions = useActions()
Expand All @@ -29,13 +30,14 @@ export function PendingTransactionsUpdater(): JSX.Element {
const checkAndUpdatePendingTransactions = useCallback(() => {
if (!arbTokenBridgeLoaded) return
const pendingTransactions = actions.app.getPendingTransactions()

if (pendingTransactions.length) {
console.info(
`Checking and updating ${pendingTransactions.length} pending transactions' statuses`
)

// eslint-disable-next-line consistent-return
return Promise.all(
Promise.all(
pendingTransactions.map((tx: Transaction) => getTransactionReceipt(tx))
).then((txReceipts: (TransactionReceipt | null)[]) => {
txReceipts.forEach((txReceipt: TransactionReceipt | null, i) => {
Expand All @@ -49,6 +51,16 @@ export function PendingTransactionsUpdater(): JSX.Element {
}
})
})

// If the Txs have been pending for over 7 days,
// they are cancelled so we should clear them from the history
const arePendingTxsAllOlderThan7Days = pendingTransactions.every(
pendingTx => isTxOlderThan7Days(pendingTx.timestampCreated)
)

if (arePendingTxsAllOlderThan7Days) {
arbTokenBridge?.transactions?.clearPendingTransactions()
}
}
}, [getTransactionReceipt, arbTokenBridge, arbTokenBridgeLoaded])
const { forceTrigger: forceTriggerUpdate } = useInterval(
Expand All @@ -59,7 +71,7 @@ export function PendingTransactionsUpdater(): JSX.Element {
useEffect(() => {
// force trigger update each time loaded change happens
forceTriggerUpdate()
}, [arbTokenBridgeLoaded])
}, [arbTokenBridgeLoaded, forceTriggerUpdate])

return <></>
}
6 changes: 6 additions & 0 deletions packages/arb-token-bridge-ui/src/util/CommonUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import dayjs from 'dayjs'

export function shortenAddress(address: string) {
const addressLength = address.length

Expand All @@ -18,3 +20,7 @@ export function shortenTxHash(txHash: string) {

export const isTestingEnvironment =
!!window.Cypress || process.env.NODE_ENV !== 'production'

export function isTxOlderThan7Days(txCreatedAt: string | null | undefined) {
brtkx marked this conversation as resolved.
Show resolved Hide resolved
return dayjs().diff(dayjs(txCreatedAt), 'days') > 7
brtkx marked this conversation as resolved.
Show resolved Hide resolved
}