Skip to content

Commit 808183d

Browse files
committed
Add block waiting logic for commit completion in Transactions component
1 parent 7175858 commit 808183d

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

src/components/pages/profile/[name]/registration/steps/Transactions.tsx

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react'
22
import { Trans, useTranslation } from 'react-i18next'
33
import styled, { css } from 'styled-components'
44
import { match, P } from 'ts-pattern'
5-
import { useAccount } from 'wagmi'
5+
import { useAccount, useWatchBlocks } from 'wagmi'
66

77
import { makeCommitment } from '@ensdomains/ensjs/utils'
88
import { Button, CountdownCircle, Dialog, Heading, Spinner } from '@ensdomains/thorin'
@@ -222,6 +222,21 @@ const Transactions = ({ registrationData, name, callback, onStart }: Props) => {
222222
const commitTx = getLatestTransaction(commitKey)
223223
const registerTx = getLatestTransaction(registerKey)
224224
const [resetOpen, setResetOpen] = useState(false)
225+
const [commitCompleteBlockNumber, setCommitCompleteBlockNumber] = useState<bigint | undefined>()
226+
const [blocksWaitedSinceCommit, setBlocksWaitedSinceCommit] = useState<bigint>(0n)
227+
228+
// Watch blocks to track when enough blocks have passed since commit completion
229+
// This is needed when reverseRecord is set to ensure proper state settlement
230+
useWatchBlocks({
231+
onBlock: (block) => {
232+
if (commitTx?.stage === 'complete' && !commitCompleteBlockNumber && block.number) {
233+
setCommitCompleteBlockNumber(block.number)
234+
}
235+
if (commitCompleteBlockNumber && block.number) {
236+
setBlocksWaitedSinceCommit(block.number - commitCompleteBlockNumber)
237+
}
238+
},
239+
})
225240

226241
const registrationParams = useRegistrationParams({
227242
name,
@@ -355,14 +370,14 @@ const Transactions = ({ registrationData, name, callback, onStart }: Props) => {
355370
})
356371
}, [commitKey, createTransactionFlow, name, onStart, registrationParams])
357372

358-
const makeRegisterNameFlow = () => {
373+
const makeRegisterNameFlow = useCallback(() => {
359374
createTransactionFlow(registerKey, {
360375
transactions: [createTransactionItem('registerName', registrationParams)],
361376
requiresManualCleanup: true,
362377
autoClose: true,
363378
resumeLink: `/register/${name}`,
364379
})
365-
}
380+
}, [registerKey, createTransactionFlow, name, registrationParams])
366381

367382
const showCommitTransaction = () => {
368383
resumeTransactionFlow(commitKey)
@@ -423,6 +438,21 @@ const Transactions = ({ registrationData, name, callback, onStart }: Props) => {
423438
endDate: commitTimestamp ? new Date(commitTimestamp + ONE_DAY * 1000) : undefined,
424439
})
425440

441+
// When reverseRecord is set, wait for 2 blocks after commit to ensure proper state settlement
442+
const BLOCKS_TO_WAIT_FOR_REVERSE_RECORD = 2n
443+
const shouldWaitForBlocks = registrationData.reverseRecord && commitTx?.stage === 'complete'
444+
const hasWaitedEnoughBlocks = blocksWaitedSinceCommit >= BLOCKS_TO_WAIT_FOR_REVERSE_RECORD
445+
const isBlockWaitComplete = !shouldWaitForBlocks || hasWaitedEnoughBlocks
446+
447+
const handleRegisterClick = () => {
448+
if (!isBlockWaitComplete) return
449+
if (!registerTx) {
450+
makeRegisterNameFlow()
451+
} else {
452+
showRegisterTransaction()
453+
}
454+
}
455+
426456
return (
427457
<StyledCard>
428458
<Dialog variant="blank" open={resetOpen} onDismiss={() => setResetOpen(false)}>
@@ -562,7 +592,8 @@ const Transactions = ({ registrationData, name, callback, onStart }: Props) => {
562592
<MobileFullWidth>
563593
<Button
564594
data-testid="finish-button"
565-
onClick={!registerTx ? makeRegisterNameFlow : showRegisterTransaction}
595+
disabled={!isBlockWaitComplete}
596+
onClick={handleRegisterClick}
566597
>
567598
{t('steps.transactions.completeRegistration')}
568599
</Button>

0 commit comments

Comments
 (0)