Created
August 27, 2024 16:13
-
-
Save okwme/dde885bced64c92473639a8d2722883c to your computer and use it in GitHub Desktop.
This is an example of how to resolve base names using ethers.js
This file contains 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
import { Contract, utils } from 'ethers' | |
import { getProvider } from './contracts' // https://docs.ethers.org/v5/api/providers/ | |
import L2ResolverAbi from './L2ResolverAbi.json' // https://gist.github.com/hughescoin/adf1c90b67cd9b2b913b984a2cc98de9 | |
const BASENAME_L2_RESOLVER_ADDRESS = '0xC6d566A56A1aFf6508b41f6c90ff131615583BCD' | |
const convertChainIdToCoinType = (chainId) => { | |
// L1 resolvers to addr | |
if (chainId === 1) { // 1 is mainnet chain id | |
return 'addr' | |
} | |
const cointype = (0x80000000 | chainId) >>> 0 | |
return cointype.toString(16).toLocaleUpperCase() | |
} | |
const convertReverseNodeToBytes = (address, chainId) => { | |
const addressFormatted = address.toLowerCase() | |
const addressNode = utils.solidityKeccak256(['string'], [addressFormatted.substring(2)]) // NOTE: hash it as a string not address | |
const chainCoinType = convertChainIdToCoinType(chainId) | |
const baseReverseNode = utils.namehash(`${chainCoinType.toLocaleUpperCase()}.reverse`) | |
const addressReverseNode = utils.solidityKeccak256( | |
['bytes32', 'bytes32'], | |
[baseReverseNode, addressNode] | |
) | |
return addressReverseNode | |
} | |
export const getBaseName = async (address) => { | |
try { | |
const addressReverseNode = convertReverseNodeToBytes(address, 8453) // 8453 is base chain id | |
const baseProvider = await getProvider() | |
const contract = new Contract(BASENAME_L2_RESOLVER_ADDRESS, L2ResolverAbi, baseProvider) | |
const basename = await contract.name(addressReverseNode) | |
return basename | |
} catch (e) { | |
console.error('Error getting basename', e) | |
} | |
} |
Thanks
This is great thanks!
How would you then get the user's avatar from a resolver?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🙋💲⚖️🌹