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: Disable execute button for expired swaps #3650

Merged
merged 2 commits into from
May 3, 2024
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
4 changes: 2 additions & 2 deletions src/components/transactions/TxDetails/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { POLLING_INTERVAL } from '@/config/constants'
import useIsExpiredSwap from '@/features/swap/hooks/useIsExpiredSwap'
import useIntervalCounter from '@/hooks/useIntervalCounter'
import React, { type ReactElement } from 'react'
import type { TransactionDetails, TransactionSummary } from '@safe-global/safe-gateway-typescript-sdk'
Expand All @@ -12,7 +13,6 @@ import useChainId from '@/hooks/useChainId'
import useAsync from '@/hooks/useAsync'
import {
isAwaitingExecution,
isExpiredSwap,
isModuleExecutionInfo,
isMultiSendTxInfo,
isMultisigDetailedExecutionInfo,
Expand Down Expand Up @@ -67,7 +67,7 @@ const TxDetailsBlock = ({ txSummary, txDetails }: TxDetailsProps): ReactElement
safeTxHash = txDetails.detailedExecutionInfo.safeTxHash
}

const expiredSwap = isExpiredSwap(txSummary.txInfo)
const expiredSwap = useIsExpiredSwap(txSummary.txInfo)

return (
<>
Expand Down
4 changes: 2 additions & 2 deletions src/features/swap/hooks/useIsExpiredSwap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react'
import type { TransactionInfo } from '@safe-global/safe-gateway-typescript-sdk'
import useInterval from '@/hooks/useInterval'
import { isSwapTxInfo } from '@/utils/transaction-guards'
import { isExpiredSwap as isSwapInfoExpired, isSwapTxInfo } from '@/utils/transaction-guards'

const INTERVAL_IN_MS = 10_000

Expand All @@ -16,7 +16,7 @@ const useIsExpiredSwap = (txInfo: TransactionInfo) => {
const isExpiredSwap = () => {
if (!isSwapTxInfo(txInfo)) return

setIsExpired(Date.now() > txInfo.validUntil * 1000)
setIsExpired(Date.now() > txInfo.validUntil * 1000 && isSwapInfoExpired(txInfo))
}

useInterval(isExpiredSwap, INTERVAL_IN_MS)
Expand Down
15 changes: 12 additions & 3 deletions src/hooks/__tests__/useInterval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ describe('useInterval', () => {
jest.useFakeTimers()
})

it('should run the callback function once immediately', () => {
let mockValue = 0
const mockCallback = jest.fn(() => mockValue++)

renderHook(() => useInterval(mockCallback, 100))

expect(mockValue).toEqual(1)
})

it('should run the callback function with every interval', async () => {
let mockValue = 0
const mockCallback = jest.fn(() => mockValue++)

renderHook(() => useInterval(mockCallback, 100))

expect(mockValue).toEqual(0)
expect(mockValue).toEqual(1)

act(() => {
jest.advanceTimersByTime(100)
})
expect(mockValue).toEqual(1)
expect(mockValue).toEqual(2)

act(() => {
jest.advanceTimersByTime(100)
})
expect(mockValue).toEqual(2)
expect(mockValue).toEqual(3)
})
})
3 changes: 3 additions & 0 deletions src/hooks/useInterval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const useInterval = (callback: () => void, time: number) => {
useEffect(() => {
const interval = setInterval(() => callbackRef.current?.(), time)

// Call the function once initially
callbackRef.current?.()

return () => clearInterval(interval)
}, [time])
}
Expand Down
Loading