Skip to content

Commit

Permalink
feat: safenet tx monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
schmanu committed Feb 21, 2025
1 parent d7c5251 commit d963964
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
55 changes: 55 additions & 0 deletions apps/web/src/features/safenet/utils/safenetTxMonitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { SAFENET_API_URL } from '@/config/constants'
import { txDispatch, TxEvent } from '@/services/tx/txEvents'
import { type SafenetTransactionDetails } from '@/store/safenet'

const SAFENET_TIMEOUT = 5 * 60_000 // 5 minutes
const POLLING_INTERVAL = 5_000

export const waitForSafenetTx = async (
txId: string,
safeTxHash: string,
chainId: string,
nonce: number,
safeAddress: string,
) => {
let intervalId: NodeJS.Timeout
let failAfterTimeoutId: NodeJS.Timeout

intervalId = setInterval(async () => {
const txDetails = await fetch(`${SAFENET_API_URL}/api/v1/tx/details/${chainId}/${safeTxHash}`)
.then((resp) => {
if (resp.ok) {
return resp.json()
} else {
return undefined
}
})
.then((resp) => {
if (resp) {
return resp as SafenetTransactionDetails
}
})

if (txDetails && txDetails.fulfillmentTxHash) {
txDispatch(TxEvent.PROCESSED, {
safeAddress,
nonce,
txHash: txDetails.fulfillmentTxHash,
txId,
})
clearInterval(intervalId)
clearTimeout(failAfterTimeoutId)
}
}, POLLING_INTERVAL)

failAfterTimeoutId = setTimeout(() => {
txDispatch(TxEvent.FAILED, {
nonce,
txId,
error: new Error(
`Transaction not processed in ${SAFENET_TIMEOUT / 60_000} minutes. Be aware that it might still be relayed.`,
),
}),
clearInterval(intervalId)
}, SAFENET_TIMEOUT)
}
4 changes: 3 additions & 1 deletion apps/web/src/hooks/useTxPendingStatuses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { isTransactionListItem } from '@/utils/transaction-guards'
import useSafeInfo from './useSafeInfo'
import { SimpleTxWatcher } from '@/utils/SimpleTxWatcher'
import useIsSafenetEnabled from '@/features/safenet/hooks/useIsSafenetEnabled'
import { waitForSafenetTx } from '@/features/safenet/utils/safenetTxMonitor'

const FINAL_PENDING_STATUSES = [TxEvent.SIGNATURE_INDEXED, TxEvent.SUCCESS, TxEvent.REVERTED, TxEvent.FAILED]

Expand Down Expand Up @@ -66,7 +67,8 @@ export const useTxMonitor = (): void => {
}

if (isSubmitting && pendingTx.isSafenet) {
// TODO: new function to poll safenet tx details from processor API and dispatch tx status to processing once it returns a txHash
const safeTxHash = txId.slice(-66)
waitForSafenetTx(txId, safeTxHash, pendingTx.chainId, pendingTx.nonce, pendingTx.safeAddress)
}
}
// `provider` is updated when switching chains, re-running this effect
Expand Down

0 comments on commit d963964

Please sign in to comment.