From d1203f900575f09fdd5c0b07c8fa51a486b23228 Mon Sep 17 00:00:00 2001 From: hh Date: Thu, 19 Sep 2024 12:07:04 +0330 Subject: [PATCH] refactor: cleanup --- static/scripts/rewards/cirip/ens-lookup.ts | 21 +-------------- static/scripts/rewards/cirip/fetch-ens.ts | 12 --------- static/scripts/rewards/cirip/query-graph.ts | 12 --------- .../rewards/cirip/query-reverse-ens.ts | 26 +++++-------------- .../render-transaction/render-ens-name.ts | 11 +------- 5 files changed, 9 insertions(+), 73 deletions(-) delete mode 100644 static/scripts/rewards/cirip/fetch-ens.ts delete mode 100644 static/scripts/rewards/cirip/query-graph.ts diff --git a/static/scripts/rewards/cirip/ens-lookup.ts b/static/scripts/rewards/cirip/ens-lookup.ts index e0fece59..97ef58ca 100644 --- a/static/scripts/rewards/cirip/ens-lookup.ts +++ b/static/scripts/rewards/cirip/ens-lookup.ts @@ -1,6 +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); @@ -24,30 +23,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", diff --git a/static/scripts/rewards/cirip/fetch-ens.ts b/static/scripts/rewards/cirip/fetch-ens.ts deleted file mode 100644 index ba3ba46c..00000000 --- a/static/scripts/rewards/cirip/fetch-ens.ts +++ /dev/null @@ -1,12 +0,0 @@ -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); -} diff --git a/static/scripts/rewards/cirip/query-graph.ts b/static/scripts/rewards/cirip/query-graph.ts deleted file mode 100644 index 8df99d5d..00000000 --- a/static/scripts/rewards/cirip/query-graph.ts +++ /dev/null @@ -1,12 +0,0 @@ -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(); -} diff --git a/static/scripts/rewards/cirip/query-reverse-ens.ts b/static/scripts/rewards/cirip/query-reverse-ens.ts index 8ed46d62..8f87b529 100644 --- a/static/scripts/rewards/cirip/query-reverse-ens.ts +++ b/static/scripts/rewards/cirip/query-reverse-ens.ts @@ -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.api.onfinality.io/public"; export async function queryReverseEns(address: string, networkId: number) { // Try to get the ENS name from localStorage @@ -12,34 +14,20 @@ export async function queryReverseEns(address: string, networkId: number) { if (cachedEnsName) return cachedEnsName; } - // Let's drop the old cache on the first run! + // 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); diff --git a/static/scripts/rewards/render-transaction/render-ens-name.ts b/static/scripts/rewards/render-transaction/render-ens-name.ts index 810df96d..0bb98ce2 100644 --- a/static/scripts/rewards/render-transaction/render-ens-name.ts +++ b/static/scripts/rewards/render-transaction/render-ens-name.ts @@ -20,16 +20,7 @@ type EnsParams = export async function renderEnsName({ element, address, tokenAddress, tokenView, networkId }: EnsParams): Promise { 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}`;