Skip to content

Commit

Permalink
Merge pull request #306 from ubiquity/revert-297-development
Browse files Browse the repository at this point in the history
Revert "fix: ens lookup"
  • Loading branch information
0x4007 authored Sep 14, 2024
2 parents 9237f0e + de49b45 commit bc2ef7d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
20 changes: 14 additions & 6 deletions static/scripts/rewards/cirip/ens-lookup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
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 @@ -19,26 +24,29 @@ 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);
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;
// }
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: [],
domains: allDomains,
};
// new Response(JSON.stringify(response), {
// headers: {
Expand Down
12 changes: 12 additions & 0 deletions static/scripts/rewards/cirip/fetch-ens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { queryGraph } from "./query-graph";

export async function fetchEns(address: string) {
const endpoint = "https://api.thegraph.com/subgraphs/name/ensdomains/ens";
const query = `{
domains(where:{owner:"${address.toLowerCase()}"}) {
name
}
}`;
const res = await queryGraph(endpoint, query);
return res.data.domains.map((domain: { name: string }) => domain.name);
}
12 changes: 12 additions & 0 deletions static/scripts/rewards/cirip/query-graph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export async function queryGraph(endpoint: string | URL | Request, query: string) {
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({ query }),
});

return response.json();
}
24 changes: 18 additions & 6 deletions static/scripts/rewards/cirip/query-reverse-ens.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { app } from "../app-state";
import { useRpcHandler } from "../web3/use-rpc-handler";
import { ethers } from "ethers";

const mainnetRpcUrl = "https://eth.api.onfinality.io/public";
import { reverseEnsInterface } from "./ens-lookup";

export async function queryReverseEns(address: string, networkId: number) {
// Try to get the ENS name from localStorage
Expand All @@ -19,14 +17,28 @@ export async function queryReverseEns(address: string, networkId: number) {
return cachedEnsName;
} else {
// If the ENS name is not in localStorage, fetch it from the API
const web3Provider = new ethers.providers.JsonRpcProvider(mainnetRpcUrl);
const ensName = await web3Provider.lookupAddress(address);
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"],
}),
});

if (ensName === null) {
if (!response.ok) {
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

0 comments on commit bc2ef7d

Please sign in to comment.