Created
July 17, 2024 15:27
-
-
Save tobySolutions/d37a36777023fcc4a951b37cf4fd7114 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { ethers } from "ethers"; | |
import { JsonRpcProvider } from "ethers"; | |
BigInt.prototype.toJSON = function () { | |
return this.toString(); | |
}; | |
export async function main(ensDomain = "vitalik.eth") { | |
// const ensDomain = "vitalik.eth"; | |
// Check if the ensDomain is a valid string | |
if (typeof ensDomain !== "string" || !ensDomain.endsWith(".eth")) { | |
throw new Error("Invalid ENS domain"); | |
} | |
const provider = new JsonRpcProvider("https://cloudflare-eth.com"); | |
try { | |
// Resolve the ENS name to an Ethereum address | |
const address = await provider.resolveName(ensDomain); | |
if (!address) { | |
throw new Error(`No address found for ENS domain: ${ensDomain}`); | |
} | |
// Get the Ethereum balance in Wei | |
const balance = await provider.getBalance(address); | |
// Convert balance from Wei to Ether | |
const ethBalance = ethers.formatEther(balance); | |
// Reverse lookup the ENS name from the Ethereum address | |
const reverseName = await provider.lookupAddress(address); | |
return `<html> | |
<body style="font-family: Arial, sans-serif; text-align: center; background-color: #f0f0f0; padding: 20px;"> | |
<h1 style="color: #333;">ENS Lookup</h1> | |
<p style="font-size: 18px;">ENS domain: <strong>${ensDomain}</strong></p> | |
<p style="font-size: 18px;">Address: <strong>${address}</strong></p> | |
<p style="font-size: 18px;">Balance: <strong>${ethBalance} ETH</strong></p> | |
${ | |
reverseName | |
? `<p style="font-size: 18px;">Reverse ENS: <strong>${reverseName}</strong></p>` | |
: "" | |
} | |
</body> | |
</html>`; | |
} catch (error) { | |
console.error("Error:", error.message); | |
return `<html> | |
<body style="font-family: Arial, sans-serif; text-align: center; background-color: #f0f0f0; padding: 20px;"> | |
<h1 style="color: red;">Error</h1> | |
<p style="font-size: 18px;">${error.message}</p> | |
</body> | |
</html>`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My webpack config