Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ens reverse resolver #311

Merged
merged 5 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 1 addition & 24 deletions static/scripts/rewards/cirip/ens-lookup.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { ethers } from "ethers";
import abi from "../abis/cirip.json";
import { fetchEns } from "./fetch-ens";
import { queryReverseEns } from "./query-reverse-ens";

export const reverseEnsInterface = new ethers.utils.Interface(abi);

// addEventListener("fetch", event => {
// event.respondWith(handleRequest(event.request).catch(err => new Response(err.stack, { status: 500 })));
// });
Expand All @@ -24,30 +19,12 @@ export async function ensLookup(addr: string, networkId: number) {
// let response = "";
try {
reverseRecord = await queryReverseEns(address, networkId);
const responseParsed = JSON.parse(reverseRecord).result;
const _reverseRecord = ethers.utils.defaultAbiCoder.decode([ethers.utils.ParamType.from("string[]")], responseParsed);
reverseRecord = _reverseRecord[0][0];
} catch (e) {
console.error(e);
// throw "Error contacting ethereum node. \nCause: '" + e + "'. \nResponse: " + response;
}

const allDomains = await fetchEns(address);

if (reverseRecord == "") {
reverseRecord = null;
}

// if reverse record is set, validate addr owns this domain.
if (reverseRecord != null && !allDomains.includes(reverseRecord)) {
console.warn("Failed to validate! Reverse record set to " + reverseRecord + ", but user does not own this name.");
reverseRecord = null;
}

return {
reverseRecord: reverseRecord,
domains: allDomains,
};
return reverseRecord;
// new Response(JSON.stringify(response), {
// headers: {
// "Content-Type": "application/json;charset=UTF-8",
Expand Down
12 changes: 0 additions & 12 deletions static/scripts/rewards/cirip/fetch-ens.ts

This file was deleted.

12 changes: 0 additions & 12 deletions static/scripts/rewards/cirip/query-graph.ts

This file was deleted.

27 changes: 8 additions & 19 deletions static/scripts/rewards/cirip/query-reverse-ens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { app } from "../app-state";
import { useRpcHandler } from "../web3/use-rpc-handler";
import { reverseEnsInterface } from "./ens-lookup";
import { ethers } from "ethers";

const mainnetRpcUrl = "https://eth.drpc.org";

export async function queryReverseEns(address: string, networkId: number) {
// Try to get the ENS name from localStorage
Expand All @@ -12,33 +14,20 @@ export async function queryReverseEns(address: string, networkId: number) {
if (cachedEnsName) return cachedEnsName;
}

if (cachedEnsName) {
// Let's drop the old cache.
if (cachedEnsName && !cachedEnsName.trim().startsWith("{")) {
// If the ENS name is in localStorage, return it
return cachedEnsName;
} else {
// If the ENS name is not in localStorage, fetch it from the API
const data = reverseEnsInterface.encodeFunctionData("getNames", [[address.substring(2)]]);

const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: "1",
method: "eth_call",
params: [{ to: "0x3671aE578E63FdF66ad4F3E12CC0c0d71Ac7510C", data: data }, "latest"],
}),
});
const web3Provider = new ethers.providers.JsonRpcProvider(mainnetRpcUrl);
const ensName = await web3Provider.lookupAddress(address);

if (!response.ok) {
if (ensName === null) {
console.error("ENS lookup failed: API request failed");
return "";
}

const ensName = await response.text();

// Store the ENS name in localStorage for future use
localStorage.setItem(address, ensName);

Expand Down
11 changes: 1 addition & 10 deletions static/scripts/rewards/render-transaction/render-ens-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@ type EnsParams =
export async function renderEnsName({ element, address, tokenAddress, tokenView, networkId }: EnsParams): Promise<void> {
let href: string = "";
try {
const resolved = await ensLookup(address, networkId);
let ensName: undefined | string;
if (resolved.reverseRecord) {
ensName = resolved.reverseRecord;
} else if (resolved.domains.length) {
const domain = resolved.domains.shift();
if (domain) {
ensName = domain;
}
}
const ensName = await ensLookup(address, networkId);
if (ensName) {
if (tokenView) {
href = `${app.currentExplorerUrl}/token/${tokenAddress}?a=${address}`;
Expand Down
Loading