From 5bb85088d61cb2049fc109b1ff5bf19ed9829af4 Mon Sep 17 00:00:00 2001 From: storywithoutend Date: Wed, 18 Sep 2024 01:09:14 +0800 Subject: [PATCH] add simulate registration override --- .../registration/steps/Transactions.tsx | 16 +++++++- .../registration/useSimulateRegistration.ts | 37 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/hooks/registration/useSimulateRegistration.ts diff --git a/src/components/pages/profile/[name]/registration/steps/Transactions.tsx b/src/components/pages/profile/[name]/registration/steps/Transactions.tsx index 6fa915441..71640cd03 100644 --- a/src/components/pages/profile/[name]/registration/steps/Transactions.tsx +++ b/src/components/pages/profile/[name]/registration/steps/Transactions.tsx @@ -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' @@ -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, { @@ -228,7 +240,7 @@ const Transactions = ({ registrationData, name, callback, onStart }: Props) => { callback={() => setCommitComplete(true)} /> - {match([commitTx, commitComplete, duration]) + {match([commitTx, commitCompleteOrCanRegisterOveride, duration]) .with([{ stage: 'complete' }, false, P._], () => ( { .otherwise(() => t('steps.transactions.subheading.default'))} - {match([commitComplete, registerTx, commitTx]) + {match([commitCompleteOrCanRegisterOveride, registerTx, commitTx]) .with([true, { stage: 'failed' }, P._], () => ( <> {ResetBackButton} diff --git a/src/hooks/registration/useSimulateRegistration.ts b/src/hooks/registration/useSimulateRegistration.ts new file mode 100644 index 000000000..962e311ea --- /dev/null +++ b/src/hooks/registration/useSimulateRegistration.ts @@ -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 & { + 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, + }) +}