Skip to content

Instantly share code, notes, and snippets.

@tobySolutions
Created July 17, 2024 15:27
Show Gist options
  • Save tobySolutions/d37a36777023fcc4a951b37cf4fd7114 to your computer and use it in GitHub Desktop.
Save tobySolutions/d37a36777023fcc4a951b37cf4fd7114 to your computer and use it in GitHub Desktop.
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>`;
}
}
@tobySolutions
Copy link
Author

My webpack config

const path = require("path");

module.exports = {
  entry: "./src/index.js",
  output: {
    library: {
      type: "module",
    },
    filename: "bundle.js",
    path: path.resolve(__dirname, "dist"),
  },
  mode: "none",
  experiments: {
    outputModule: true,
  },
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment