Skip to content

Commit

Permalink
update useSimulateRegistration for legacy eth controller
Browse files Browse the repository at this point in the history
  • Loading branch information
storywithoutend committed Jan 10, 2025
1 parent 442ccde commit e07aac0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 23 deletions.
2 changes: 2 additions & 0 deletions e2e/specs/stateless/registerName.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ test('should not allow registering a premium name for less than 28 days', async
expect(page.getByText('28 days registration', { exact: true })).toBeVisible()
})

await page.pause()
await page.getByTestId('payment-choice-ethereum').click()
await expect(page.getByTestId('invoice-item-2-amount')).toBeVisible()
await page.getByTestId('next-button').click()
Expand Down Expand Up @@ -1172,6 +1173,7 @@ test.describe('Error handling', () => {
})

await test.step('transaction: commit', async () => {
await page.pause()
await expect(page.getByText('Open Wallet')).toBeVisible()
await transactionModal.confirm()
await expect(page.getByText(`Your "Start timer" transaction was successful`)).toBeVisible()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@ensdomains/address-encoder": "1.1.1",
"@ensdomains/content-hash": "^3.0.0-beta.5",
"@ensdomains/ens-contracts": "1.2.0-beta.0",
"@ensdomains/ensjs": "4.0.3-alpha.0",
"@ensdomains/ensjs": "4.0.3-alpha.11",
"@ensdomains/thorin": "0.6.50",
"@metamask/post-message-stream": "^6.1.2",
"@metamask/providers": "^14.0.2",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 58 additions & 6 deletions src/hooks/registration/useSimulateRegistration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { Address } from 'viem'
import { usePublicClient, useSimulateContract, UseSimulateContractParameters } from 'wagmi'

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

import { isLegacyRegistration } from '@app/utils/registration/isLegacyRegistration'
import { makeLegacyRegistrationParams } from '@app/utils/registration/makeLegacyRegistrationParams'
import { calculateValueWithBuffer } from '@app/utils/utils'

import { usePrice } from '../ensjs/public/usePrice'
Expand All @@ -11,6 +21,46 @@ type UseSimulateRegistrationParameters = Pick<UseSimulateContractParameters, 'qu
registrationParams: RegistrationParameters
}

type UseSimulateEthRegistrarControllerRegisterReturnType = UseSimulateContractParameters<
typeof ethRegistrarControllerRegisterSnippet,
'register'
>

type UseSimulateLegacyEthRegistrarControllerRegisterReturnType = UseSimulateContractParameters<
typeof legacyEthRegistrarControllerRegisterWithConfigSnippet,
'registerWithConfig'
>

type MakeSimulateRegistrationParamsReturnType =
| UseSimulateEthRegistrarControllerRegisterReturnType
| UseSimulateLegacyEthRegistrarControllerRegisterReturnType

export const makeSimulateRegistrationParams = ({
registrationParams,
ensEthRegistrarControllerAddress,
legacyEthRegistrarControllerAddress,
}: {
registrationParams: RegistrationParameters
ensEthRegistrarControllerAddress: Address
legacyEthRegistrarControllerAddress: Address
}): MakeSimulateRegistrationParamsReturnType => {
if (isLegacyRegistration(registrationParams)) {
return {
abi: legacyEthRegistrarControllerRegisterWithConfigSnippet,
address: legacyEthRegistrarControllerAddress,
functionName: 'registerWithConfig',
args: makeLegacyRegistrationWithConfigTuple(makeLegacyRegistrationParams(registrationParams)),
}
}

return {
abi: ethRegistrarControllerRegisterSnippet,
address: ensEthRegistrarControllerAddress,
functionName: 'register',
args: makeRegistrationTuple(registrationParams),
}
}

export const useSimulateRegistration = ({
registrationParams,
query,
Expand All @@ -27,10 +77,12 @@ export const useSimulateRegistration = ({
const value = base + premium

return useSimulateContract({
abi: ethRegistrarControllerRegisterSnippet,
address: client.chain.contracts.ensEthRegistrarController.address,
functionName: 'register',
args: makeRegistrationTuple(registrationParams),
...makeSimulateRegistrationParams({
registrationParams,
ensEthRegistrarControllerAddress: client.chain.contracts.ensEthRegistrarController.address,
legacyEthRegistrarControllerAddress:
client.chain.contracts.legacyEthRegistrarController.address,
}),
value: calculateValueWithBuffer(value),
query,
})
Expand Down
17 changes: 6 additions & 11 deletions src/utils/registration/makeLegacyRegistrationParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Address } from 'viem'

import { LegacyRegistrationParameters, RegistrationParameters } from '@ensdomains/ensjs/utils'
import {
LegacyRegistrationWithConfigParameters,
RegistrationParameters,
} from '@ensdomains/ensjs/utils'

import { isEthCoin } from '../coin'
import { emptyAddress } from '../constants'
Expand All @@ -12,15 +15,7 @@ export const makeLegacyRegistrationParams = ({
duration,
secret,
resolverAddress = emptyAddress,
}: RegistrationParameters): LegacyRegistrationParameters => {
if (resolverAddress === emptyAddress)
return {
name,
owner,
duration,
secret,
}

}: RegistrationParameters): LegacyRegistrationWithConfigParameters => {
const address = (records?.coins?.find(({ coin }) => isEthCoin(coin))?.value as Address) || owner

return {
Expand All @@ -30,5 +25,5 @@ export const makeLegacyRegistrationParams = ({
secret,
resolverAddress,
address,
}
} as LegacyRegistrationWithConfigParameters
}

0 comments on commit e07aac0

Please sign in to comment.