Skip to content

Instantly share code, notes, and snippets.

@tobySolutions
Last active September 2, 2024 08:47
Show Gist options
  • Save tobySolutions/76fa868a75ee3ea984490bfbe6e88318 to your computer and use it in GitHub Desktop.
Save tobySolutions/76fa868a75ee3ea984490bfbe6e88318 to your computer and use it in GitHub Desktop.
ENS-lookup function
import Web3 from "web3";
export const main = async ({ path = "vitalik.eth" }) => {
const ensDomain = path.split("/").pop();
// Check if the path is a valid string
if (typeof ensDomain !== "string" || !ensDomain.endsWith(".eth")) {
throw new Error("Invalid ENS domain");
}
const provider = new Web3.providers.HttpProvider(
"https://cloudflare-eth.com"
);
const web3 = new Web3(provider);
try {
// Resolve the ENS name to an Ethereum address
const address = await web3.eth.ens.getAddress(ensDomain);
if (!address) {
throw new Error(`No address found for ENS domain: ${ensDomain}`);
}
// Get the Ethereum balance in Wei
const balance = await web3.eth.getBalance(address);
// Convert balance from Wei to Ether
const ethBalance = web3.utils.fromWei(balance, "ether");
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>
</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