-
Notifications
You must be signed in to change notification settings - Fork 67
migrate setResolver
#248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nexxeln
wants to merge
8
commits into
feature/fet-1885-ensjs-refactor
Choose a base branch
from
feature/fet-1895-migrate-setresolver
base: feature/fet-1885-ensjs-refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
migrate setResolver
#248
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d942f49
migrate setResolver
nexxeln 87aa5b9
export abi from separate file
nexxeln d0f9c45
add checks for address
nexxeln 5f97f9d
fix failing test
nexxeln c67abf3
organise imports
nexxeln 0f730ba
fix formatting
nexxeln c42aa3b
rename namechain registry to standard registry, add a test for the fu…
talentlessguy 3cf1a35
fmt
talentlessguy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
@@ -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 | ||
|
@@ -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)) { | ||
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!, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
> | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/ensjs/src/contracts/namechain/standardRegistry.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.