Skip to content

Instantly share code, notes, and snippets.

@alextcn
Created January 25, 2023 18:36
Show Gist options
  • Save alextcn/6b249a2f187e53773611cd913cfcdbea to your computer and use it in GitHub Desktop.
Save alextcn/6b249a2f187e53773611cd913cfcdbea to your computer and use it in GitHub Desktop.
Set multiple resolver records in a single transaction
import { ethers } from 'ethers'
import { namehash } from 'ethers/lib/utils.js'
const ENS_RESOLVER_ADDRESS = '0x2800Ec5BAB9CE9226d19E0ad5BC607e3cfC4347E' // goerli
const ENS_RESOLVER_ABI = [
'function addr(bytes32 node) external view returns (address)',
'function setAddr(bytes32 node, address a) external',
'function text(bytes32 node, string calldata key) external view returns (string memory)',
'function setText(bytes32 node, string calldata key, string calldata value) external',
'function multicall(bytes[] calldata data) external returns(bytes[] memory results)'
]
const DOMAIN = 'startstop0.eth'
async function setMulticallFields() {
const provider = new ethers.providers.Web3Provider(window.ethereum!)
const signer = provider.getSigner()
const _contract = new ethers.Contract(ENS_RESOLVER_ADDRESS, ENS_RESOLVER_ABI, provider)
const contract = _contract.connect(signer)
const node = namehash(DOMAIN)
// read ETH addr
const ethAddress = await contract.addr(node)
console.log(`ETH address for ${DOMAIN} is ${ethAddress}`)
// read email text
const emailText = await contract.text(node, 'email')
console.log(`email text for ${DOMAIN} is ${emailText}`)
// set only ETH addr
// let tx = await contract.setAddr(node, '0x000000000000000000000000000000000000dead')
// let txHash = (await tx.wait()).transactionHash
// console.log(`ETH address updated: tx = ${txHash}`)
// set only email text
// let tx = await contract.setText(node, 'email', '[email protected]')
// let txHash = (await tx.wait()).transactionHash
// console.log(`email text updated: tx = ${txHash}`)
// set both ETH addr and email text with multicall
const resolverInterface = new ethers.utils.Interface(ENS_RESOLVER_ABI)
const setAddrData = resolverInterface.encodeFunctionData('setAddr', [node, '0x0000000000000000000000000000000000000000'])
const setEmailData = resolverInterface.encodeFunctionData('setText', [node, 'email', '[email protected]'])
let tx = await contract.multicall([setAddrData, setEmailData])
let txHash = (await tx.wait()).transactionHash
console.log(`multicall success: tx = ${txHash}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment