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

Feat(Safenet): pending tx status #5046

Draft
wants to merge 4 commits into
base: safenet
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat: safenet tx monitor
  • Loading branch information
schmanu committed Feb 21, 2025
commit d9639645bf5173ab7a31731fbcf459d581f3805b
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
Loading