Skip to content

Commit

Permalink
add simulate registration override
Browse files Browse the repository at this point in the history
  • Loading branch information
storywithoutend committed Sep 17, 2024
1 parent 1d8aa24 commit 5bb8508
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import MobileFullWidth from '@app/components/@atoms/MobileFullWidth'
import { TextWithTooltip } from '@app/components/@atoms/TextWithTooltip/TextWithTooltip'
import { Card } from '@app/components/Card'
import { useExistingCommitment } from '@app/hooks/registration/useExistingCommitment'
import { useSimulateRegistration } from '@app/hooks/registration/useSimulateRegistration'
import { useDurationCountdown } from '@app/hooks/time/useDurationCountdown'
import useRegistrationParams from '@app/hooks/useRegistrationParams'
import { CenteredTypography } from '@app/transaction-flow/input/ProfileEditor/components/CenteredTypography'
Expand Down Expand Up @@ -126,6 +127,17 @@ const Transactions = ({ registrationData, name, callback, onStart }: Props) => {
commitKey,
})

const { isSuccess: canRegisterOveride } = useSimulateRegistration({
registrationParams,
query: {
enabled: true,
retry: true,
retryDelay: 10000,
},
})

const commitCompleteOrCanRegisterOveride = commitComplete || canRegisterOveride

const makeCommitNameFlow = useCallback(() => {
onStart()
createTransactionFlow(commitKey, {
Expand Down Expand Up @@ -228,7 +240,7 @@ const Transactions = ({ registrationData, name, callback, onStart }: Props) => {
callback={() => setCommitComplete(true)}
/>
<CenteredTypography>
{match([commitTx, commitComplete, duration])
{match([commitTx, commitCompleteOrCanRegisterOveride, duration])
.with([{ stage: 'complete' }, false, P._], () => (
<Trans
i18nKey="steps.transactions.subheading.commiting"
Expand All @@ -254,7 +266,7 @@ const Transactions = ({ registrationData, name, callback, onStart }: Props) => {
.otherwise(() => t('steps.transactions.subheading.default'))}
</CenteredTypography>
<ButtonContainer>
{match([commitComplete, registerTx, commitTx])
{match([commitCompleteOrCanRegisterOveride, registerTx, commitTx])
.with([true, { stage: 'failed' }, P._], () => (
<>
{ResetBackButton}
Expand Down
37 changes: 37 additions & 0 deletions src/hooks/registration/useSimulateRegistration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { usePublicClient, useSimulateContract, UseSimulateContractParameters } from 'wagmi'

import { ethRegistrarControllerRegisterSnippet } from '@ensdomains/ensjs/contracts'
import { makeRegistrationTuple, RegistrationParameters } from '@ensdomains/ensjs/utils'

import { calculateValueWithBuffer } from '@app/utils/utils'

import { usePrice } from '../ensjs/public/usePrice'

type UseSimulateRegistrationParameters = Pick<UseSimulateContractParameters, 'query'> & {
registrationParams: RegistrationParameters
}

export const useSimulateRegistration = ({
registrationParams,
query,
}: UseSimulateRegistrationParameters) => {
const client = usePublicClient()

const { data: price } = usePrice({
nameOrNames: registrationParams.name,
duration: registrationParams.duration,
})

const base = price?.base ?? 0n
const premium = price?.premium ?? 0n
const value = base + premium

return useSimulateContract({
abi: ethRegistrarControllerRegisterSnippet,
address: client.chain.contracts.ensEthRegistrarController.address,
functionName: 'register',
args: makeRegistrationTuple(registrationParams),
value: calculateValueWithBuffer(value),
query,
})
}

0 comments on commit 5bb8508

Please sign in to comment.