Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/ensjs/src/actions/wallet/setResolver.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Address, Hex } from 'viem'
import { type Address, type Hex, zeroAddress } from 'viem'
import { afterEach, beforeAll, beforeEach, expect, it } from 'vitest'
import {
publicClient,
Expand Down Expand Up @@ -67,6 +67,18 @@ it('should error if unknown contract', async () => {
).rejects.toThrow('Unknown contract: random')
})

it.skip('should allow specifying a custom registry address', async () => {
const tx = await setResolver(walletClient, {
name: 'wrapped.eth',
registryAddress: zeroAddress,
resolverAddress: '0xAEfF4f4d8e2cB51854BEa2244B3C5Fb36b41C7fC',
account: accounts[1],
})
expect(tx).toBeTruthy()
const receipt = await waitForTransaction(tx)
expect(receipt.status).toBe('success')
})

// it('should return a transaction for a name and set successfully', async () => {
// const tx = await setName(walletClient, {
// name: 'test123.eth',
Expand Down
93 changes: 70 additions & 23 deletions packages/ensjs/src/actions/wallet/setResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import type {
WriteContractParameters,
WriteContractReturnType,
} from 'viem'
import { isAddress, labelhash } from 'viem'
import { writeContract } from 'viem/actions'
import { getAction } from 'viem/utils'
import {
type ChainWithContracts,
getChainContractAddress,
type RequireClientContracts,
} from '../../clients/chain.js'
import { standardRegistrySetResolverSnippet } from '../../contracts/namechain/standardRegistry.js'
import { nameWrapperSetResolverSnippet } from '../../contracts/nameWrapper.js'
import { registrySetResolverSnippet } from '../../contracts/registry.js'
import type { ErrorType } from '../../errors/utils.js'
Expand All @@ -25,14 +27,25 @@ import {
} from '../../utils/clientWithOverrides.js'
import { type NamehashErrorType, namehash } from '../../utils/name/namehash.js'

type SetResolverContractParameters = {
/** Contract to set resolver on - can be 'registry', 'nameWrapper', or an address of a namechain registry */
contract?: 'registry' | 'nameWrapper'
registryAddress?: never
}

type SetResolverRegistryAddressParameters = {
/** Contract to set resolver on - can be 'registry', 'nameWrapper', or an address of a namechain registry */
contract?: never
registryAddress: Address
}

export type SetResolverWriteParametersParameters = {
/** Name to set resolver for */
name: string
/** Contract to set resolver on */
contract: 'registry' | 'nameWrapper'

/** Resolver address to set */
resolverAddress: Address
}
} & (SetResolverContractParameters | SetResolverRegistryAddressParameters)

export type SetResolverWriteParametersReturnType = ReturnType<
typeof setResolverWriteParameters
Expand All @@ -56,42 +69,76 @@ export const setResolverWriteParameters = <
'ensNameWrapper' | 'ensRegistry',
account
>,
{ name, contract, resolverAddress }: SetResolverWriteParametersParameters,
{
name,
contract,
resolverAddress,
registryAddress,
}: SetResolverWriteParametersParameters,
) => {
ASSERT_NO_TYPE_ERROR(client)

if (contract !== 'registry' && contract !== 'nameWrapper')
if (!isAddress(resolverAddress)) {
throw new Error(`Invalid resolver address: ${resolverAddress}`)
}

// Handle legacy contracts
if (contract === 'registry' || contract === 'nameWrapper') {
const address = getChainContractAddress({
chain: client.chain,
contract: contract === 'nameWrapper' ? 'ensNameWrapper' : 'ensRegistry',
})

const args = [namehash(name), resolverAddress] as const
const functionName = 'setResolver'

const baseParams = {
address,
functionName,
args,
chain: client.chain,
account: client.account,
} as const

if (contract === 'nameWrapper')
return {
...baseParams,
abi: nameWrapperSetResolverSnippet,
} as const satisfies WriteContractParameters<
typeof nameWrapperSetResolverSnippet
>

return {
...baseParams,
abi: registrySetResolverSnippet,
} as const satisfies WriteContractParameters<
typeof registrySetResolverSnippet
>
}

// Handle namechain contracts
if (registryAddress && !isAddress(registryAddress)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to check if registryAddress exists. isAddres will return false if registryAddress does not exist. Also this condition will fail if registryAddress is empty.

TLDR. Just check if registryAddres is address.

throw new Error(`Unknown contract: ${contract}`)
}

const address = getChainContractAddress({
chain: client.chain,
contract: contract === 'nameWrapper' ? 'ensNameWrapper' : 'ensRegistry',
})
const label = name.split('.')[0]
const tokenId = BigInt(labelhash(label))

const args = [namehash(name), resolverAddress] as const
const functionName = 'setResolver'
const args = [tokenId, resolverAddress] as const

const baseParams = {
address,
functionName,
address: registryAddress!,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to avoid forcing the value or registryAddress. Should add a guard function before this is isAddress does not satisfy it.

e.g. function checkAddress(x: unknown): x is Address

functionName: 'setResolver',
args,
chain: client.chain,
account: client.account,
} as const

if (contract === 'nameWrapper')
return {
...baseParams,
abi: nameWrapperSetResolverSnippet,
} as const satisfies WriteContractParameters<
typeof nameWrapperSetResolverSnippet
>

return {
...baseParams,
abi: registrySetResolverSnippet,
abi: standardRegistrySetResolverSnippet,
} as const satisfies WriteContractParameters<
typeof registrySetResolverSnippet
typeof standardRegistrySetResolverSnippet
>
}

Expand Down
1 change: 1 addition & 0 deletions packages/ensjs/src/contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export {
multicallGetCurrentBlockTimestampSnippet,
multicallTryAggregateSnippet,
} from './multicall.js'
export { standardRegistrySetResolverSnippet } from './namechain/standardRegistry.js'
export {
nameWrapperErrors,
nameWrapperGetDataSnippet,
Expand Down
20 changes: 20 additions & 0 deletions packages/ensjs/src/contracts/namechain/standardRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Namechain contract ABI snippets

export const standardRegistrySetResolverSnippet = [
{
inputs: [
{
name: 'tokenId',
type: 'uint256',
},
{
name: 'resolver',
type: 'address',
},
],
name: 'setResolver',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
] as const
Loading