From f9636ec7fdfa0988985026bd547f0911aa66e433 Mon Sep 17 00:00:00 2001 From: storywithoutend Date: Wed, 18 Sep 2024 00:03:22 +0800 Subject: [PATCH 1/2] add simulate registration with override --- .../registration/steps/Transactions.tsx | 18 +++++++-- .../registration/useSimulateRegistration.ts | 37 +++++++++++++++++++ 2 files changed, 52 insertions(+), 3 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..53444abcf 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, { @@ -225,10 +237,10 @@ const Transactions = ({ registrationData, name, callback, onStart }: Props) => { disabled={!commitTimestamp} startTimestamp={commitTimestamp} size="large" - callback={() => setCommitComplete(true)} + callback={() => setCommitComplete(false)} /> - {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, + }) +} From 1d8aa249fa0bdb5b3a72ae1cead8f840bb273430 Mon Sep 17 00:00:00 2001 From: storywithoutend Date: Wed, 18 Sep 2024 00:04:33 +0800 Subject: [PATCH 2/2] Revert "add simulate registration with override" This reverts commit f9636ec7fdfa0988985026bd547f0911aa66e433. --- .../registration/steps/Transactions.tsx | 18 ++------- .../registration/useSimulateRegistration.ts | 37 ------------------- 2 files changed, 3 insertions(+), 52 deletions(-) delete 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 53444abcf..6fa915441 100644 --- a/src/components/pages/profile/[name]/registration/steps/Transactions.tsx +++ b/src/components/pages/profile/[name]/registration/steps/Transactions.tsx @@ -11,7 +11,6 @@ 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' @@ -127,17 +126,6 @@ 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, { @@ -237,10 +225,10 @@ const Transactions = ({ registrationData, name, callback, onStart }: Props) => { disabled={!commitTimestamp} startTimestamp={commitTimestamp} size="large" - callback={() => setCommitComplete(false)} + callback={() => setCommitComplete(true)} /> - {match([commitTx, commitCompleteOrCanRegisterOveride, duration]) + {match([commitTx, commitComplete, duration]) .with([{ stage: 'complete' }, false, P._], () => ( { .otherwise(() => t('steps.transactions.subheading.default'))} - {match([commitCompleteOrCanRegisterOveride, registerTx, commitTx]) + {match([commitComplete, registerTx, commitTx]) .with([true, { stage: 'failed' }, P._], () => ( <> {ResetBackButton} diff --git a/src/hooks/registration/useSimulateRegistration.ts b/src/hooks/registration/useSimulateRegistration.ts deleted file mode 100644 index 962e311ea..000000000 --- a/src/hooks/registration/useSimulateRegistration.ts +++ /dev/null @@ -1,37 +0,0 @@ -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, - }) -}