Skip to content

Instantly share code, notes, and snippets.

@ghostffcode
Last active May 18, 2023 20:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghostffcode/4f809c4c3a808f3b37de2076698bf8cc to your computer and use it in GitHub Desktop.
Save ghostffcode/4f809c4c3a808f3b37de2076698bf8cc to your computer and use it in GitHub Desktop.
Batch Resolve ENS names to addresses

Batch Resolve ENS names to Address

--

Made with ❤️ at https://buidlguidl.com/

Contract: https://etherscan.io/address/0x4bc4a43cac594432ed4f48965e5bf5aca319f350

How to use

Using ethers, create an instance of your contract:

import { ethers ) from "ethers"

// ....

// ABI is located in the next file
const batchResolver = new ethers.Contract("0x4bC4a43Cac594432ED4F48965E5bf5aCA319f350", abi, signer);

const addresses: `0x${string}`[] = await ENSBatchResolver.batchResolveWithENSRegistry([
  ethers.utils.namehash("owocki.eth"),
  ethers.utils.namehash("possiblywrongENS.eth"),
  ethers.utils.namehash("austingriffith.eth"),
]);

// invalid ENS names will be resolved to address(0)
[
{
"stateMutability": "payable",
"type": "fallback"
},
{
"inputs": [
{
"internalType": "contract Registry",
"name": "registry",
"type": "address"
},
{
"internalType": "bytes32[]",
"name": "names",
"type": "bytes32[]"
}
],
"name": "batchResolveWithCustomRegistry",
"outputs": [
{
"internalType": "address[]",
"name": "",
"type": "address[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32[]",
"name": "names",
"type": "bytes32[]"
}
],
"name": "batchResolveWithENSRegistry",
"outputs": [
{
"internalType": "address[]",
"name": "",
"type": "address[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"stateMutability": "payable",
"type": "receive"
}
]
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;
abstract contract Registry {
function resolver(bytes32 node) external view virtual returns (address);
}
abstract contract Resolver {
function addr(bytes32 node) external view virtual returns (address);
}
contract ENSBatchResolver {
address immutable defaultRegistry = 0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e;
function batchResolveWithENSRegistry(bytes32[] calldata names) public view returns (address[] memory) {
return batchResolveWithCustomRegistry(Registry(defaultRegistry), names);
}
function batchResolveWithCustomRegistry(Registry registry, bytes32[] calldata names)
public
view
returns (address[] memory)
{
address[] memory addressList = new address[](names.length);
for (uint256 i = 0; i < names.length; i++) {
bytes32 currentName = names[i];
address resolver = registry.resolver(currentName);
if (resolver == address(0)) {
addressList[i] = address(0);
} else {
addressList[i] = Resolver(resolver).addr(currentName);
}
}
return addressList;
}
// to support receiving ETH by default
receive() external payable {}
fallback() external payable {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment