Interacting with a Resolver
Some apps may want to allow for users to edit, update, or modify their name and its behaviour at a more advanced level. This is possible by interacting with the resolver contract of a name directly.
Before you start sending transactions to users resolvers, you should check if they support the interface you want to use. This is done by calling the supportsInterface
(see EIP-165) function on the resolver contract.
function supportsInterface(bytes4 interfaceID) external pure returns (bool)
In order to ensure that resolvers we interact with are compatible with specific standards you can call the above function on contracts with an interfaceID and then check the boolean it returns.
Interface IDs are calculated according to solidity ABI and stored in a four-byte value.
If you want to help a user set their avatar, specify a preferred color scheme, or set any other record on their ENS name you can do so in specific cases.
First we need to check if the user's resolver supports the interface we want to use. And then you can call the setRecord
function on the resolver contract.
interface ENS {
function setResolver(bytes32 node, address resolver) external;
}
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
import { addEnsContracts } from '@ensdomains/ensjs'
import { setRecords } from '@ensdomains/ensjs/wallet'
const wallet = createWalletClient({
chain: addEnsContracts(mainnet),
transport: custom(window.ethereum),
})
const hash = await setRecords(wallet, {
name: 'ens.eth',
coins: [
{
coin: 'ETH',
value: '0xFe89cc7aBB2C4183683ab71653C4cdc9B02D44b7',
},
],
texts: [{ key: 'foo', value: 'bar' }],
resolverAddress: '0x4976fb03C32e5B8cfe2b6cCB31c09Ba78EBaBa41',
})
// 0x...
Please make it clear to the user what you are doing and why. If possible please also showcase the record you are setting to the user before you do so.
Overwriting a user's resolver involves overwriting the behaviour of their ENS name.
In order to overwrite the resolver for a user you need to call the setResolver
function on the ENSRegistry
contract.
interface ENS {
function setResolver(bytes32 node, address resolver) external;
}
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
import { addEnsContracts } from '@ensdomains/ensjs'
import { setResolver } from '@ensdomains/ensjs/wallet'
const wallet = createWalletClient({
chain: addEnsContracts(mainnet),
transport: custom(window.ethereum),
})
const hash = await setResolver(wallet, {
name: 'ens.eth',
contract: 'registry',
resolverAddress: '0x4976fb03C32e5B8cfe2b6cCB31c09Ba78EBaBa41',
})
// 0x...
Please do not change the resolver for a user without their permission. Overwriting the resolver is a destructive action and will overwrite any existing resolution logic.
At the time of writing this the ecosystem around multichain and "writing" to layer 2 & offchain resolvers has yet to be standardized and is still under active development. Please check back at a later date.