How to Resolve ENS Domains Using JavaScript and QuickNode
5 min read
Overview
ENS is the leading naming system based on Ethereum; it maps Ethereum addresses to tangible names. Today in this guide, we will learn more about ENS and How to resolve ENS domains using JavaScript library ethers.js and QuickNode.
Prerequisites
- Node.js installed.
- MetaMask browser extension.
What is an ENS domain?
ENS or Ethereum Name Service is an Ethereum dApp that allows users to map their Ethereum address readable by EVM (Ethereum Virtual Machine) to human-readable form like name.eth. ENS is similar to the DNS (Domain Name Service) of the internet as it allows its users to register for .eth domains. ENS domains are mapped within the network. There is no top-level authority or copyright over the domains that ICANN manages.
Anyone can register an ENS domain to map their Etheruem wallet address or Contract address to a .eth domain name that is currently available and not registered by someone else. Registrar contracts of ENS manage this. The registry contract maintains a list of domains, subdomains, owner of the domains, resolver for the domain, and cache for time-to-live. If it has to do with the domain, it is probably in the registry contract. A resolver is a smart contract that follows ENS contract standards; anyone can be resolved if they follow ENS rules and standards.
So, for example, if someone wishes to find and address for name.eth they will have to send a request to the ENS registry to get the address for the resolver of name.eth. Then once they get that address, they send a request to the resolver to get the mapped address registered for the ENS name.
Now let us see how to register for an ENS domain through their dApp.
How to Register an ENS domain
We will register our ENS on the Ropsten testnet as registering a domain on mainnet requires real ETH to be sent to the registry contract and also for gas fee. So make sure you have MetaMask installed on your browser and have some Ropsten ETH. If you do not have Ropsten ETH, you can get it from the Ropsten faucet.
Make sure you are on the Ropsten testnet network in your MetaMask and head over to ENS dApp; click on connect to connect the MetaMask wallet. Now, search the name you would like to register your address with.
If the searched name is already registered, you will be presented with the following screen:
If the searched term (testname.eth) name is available to register, you will be presented with information like the required ETH to register the domain, the gas fee, and how many years you want to hold the domain.
Click on 'Request To Register' and confirm the transaction in the MetaMask popup window. Once the transaction is confirmed step 1 will be completed. Wait for at least a minute (but not more than 24 hours) for the step to get completed. This is a measure to prevent frontrunning. Then confirm the second transaction from your MetaMask window.
Transaction number one generates a secret commitment hash for the name we want to generate, and it is submitted to the registry controller. Transaction number two submits a registration request for the domain name along with the secret commitment hash transaction one generated.
Now, set the name as the primary ENS name:
Now that we have our ENS domain registered let us see how to resolve it using code. To do so, we will need a Ropsten node (as we registered our domain on Ropsten) and a web3 library.
Booting Our Ethereum Node
To serve our purpose today, let us get a free trial Ropsten node from QuickNode to get data from the Ethereum Ropsten Testnet.
Save the HTTP provider URL as we will use it next.
We have our node in place to talk to the blockchain, so now we can use ethers.js to resolve the ENS domain. If you don't have ethers.js installed, you can follow this guide on How to connect to Ethereum using ethers.js.
How to resolve an ENS domain using JavaScript
Create an index.js file in your working directory and copy-paste the following code into it.
const ethers = require("ethers");
(async () => {
const provider = new ethers.providers.JsonRpcProvider(
"ADD_YOUR_HTTP_URL_HERE"
);
const address = await provider.resolveName("YOUR_ENS_DOMAIN_HERE");
const balance = await provider.getBalance("YOUR_ENS_DOMAIN_HERE");
console.log(`Balance of ${address} is:`, ethers.utils.formatEther(balance));
})();
Replace ADD_YOUR_HTTP_URL_HERE with the HTTP provider URL we got in the last step and YOUR_ENS_DOMAIN_HERE with the ENS domain we got earlier.
Explanation of the above code:
Line 1: Importing the ethers.js library.
Line 2: Initializing the async function without any name.
Line 3: Initializing our Ropsten node using the JsonRpcProvider module of ethers providers and pointing it provider variable.
Line 4: Using resolveName method of ethers to get the address mapped to the ENS domain, and saving to address variable.
Line 5: Getting the balance of our address using the ENS domain directly and saving it in the balance variable. Ethers.js has the functionality that an ENS name can be used anywhere in the code instead of an actual Ethereum address.
Line 6: Printing the resolved address along with its balance.
Line 7: Calling the async function.
Save the file and run it using:
node index
The output should look something like this:
Conclusion
Cheers for all the work. In this guide, we learned what ENS is, how to register an ENS domain, and how to resolve an ENS domain using JavaScript library ethers.js and QuickNode. Now you can follow these steps on mainnet to claim your very own .eth domain!
Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you'll ever meet :)