Created
August 16, 2022 05:53
-
-
Save alexreyes/379b00fd62abe9c0e66e485fd73c3b86 to your computer and use it in GitHub Desktop.
Get ENS expiry for a given address
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
const { ethers, BigNumber } = require("ethers"); | |
const provider = new ethers.providers.AlchemyProvider('homestead', '_gNU-Y2xjUgiSuMx2tr87F-wZ12ybjrh'); | |
async function getExpiryDate(address) { | |
const ens = await provider.lookupAddress(address); | |
if (ens === null) { | |
console.log('No ENS found for this address'); | |
return; | |
} | |
const ensDomainSubstring = ens.substring(0, ens.indexOf('.eth')); | |
const ensContractAddress = "0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85" | |
const ensAbi = [ | |
"function nameExpires(uint256 id) external view returns(uint)" | |
] | |
const ensContract = new ethers.Contract(ensContractAddress, ensAbi, provider); | |
const keccakAddress = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(ensDomainSubstring)); | |
const result = await ensContract.nameExpires(keccakAddress); | |
const timestamp = BigNumber.from(result).toNumber(); | |
var date = new Date(timestamp*1000); // https://stackoverflow.com/questions/24170933/convert-unix-timestamp-to-date-time-javascript | |
console.log('Expiry: ', date); | |
} | |
getExpiryDate('0xa892631eacaf19db0b229d08b3a02e5e53ff37c1'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment