Skip to content

Commit

Permalink
Fix whois label sources
Browse files Browse the repository at this point in the history
  • Loading branch information
rkalis committed Nov 6, 2023
1 parent 1363805 commit 33fe67f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
1 change: 1 addition & 0 deletions .example.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
AMPLITUDE_API_KEY=
INFURA_API_KEY=
HARPIE_API_KEY=XXX
6 changes: 4 additions & 2 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const WHOIS_BASE_URL = 'https://raw.githubusercontent.com/RevokeCash/whois/master/data/generated';
export const INFURA_API_KEY = process.env.INFURA_API_KEY!;
export const HARPIE_API_KEY = process.env.HARPIE_API_KEY!;

export const Urls = {
REVOKE_CASH: 'https://revoke.cash',
DISCORD: 'https://discord.gg/revoke-cash',
Expand Down Expand Up @@ -122,8 +126,6 @@ export const NFT_MARKETPLACES: Record<string, string> = {
'0x2445a4e934af7c786755931610af099554ba1354': 'UneMeta',
};

export const INFURA_API_KEY = process.env.INFURA_API_KEY!;

export const warningSettingKeys: Record<WarningType, string> = {
[WarningType.ALLOWANCE]: 'settings:warnOnApproval',
[WarningType.LISTING]: 'settings:warnOnListing',
Expand Down
45 changes: 22 additions & 23 deletions src/lib/utils/whois.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
import { Address, getAddress } from 'viem';
import { SpenderData } from '../types';
import { HARPIE_API_KEY, WHOIS_BASE_URL } from '../constants';

// ALL THE BELOW ARE COPIED FROM REVOKE.CASH AND SHOULD BE EXTRACTED AT SOME POINT

export const DATA_BASE_URL = 'https://raw.githubusercontent.com/RevokeCash/revoke.cash/master/data';
export const ETHEREUM_LISTS_CONTRACTS = 'https://raw.githubusercontent.com/ethereum-lists/contracts/main';

export const getSpenderData = async (
address: Address,
address: string,
chainId?: number,
openseaProxyAddress?: Address
openseaProxyAddress?: string
): Promise<SpenderData | null> => {
if (!chainId) return null;
if (!address) return null;
if (address === openseaProxyAddress) return { name: 'OpenSea (old)' };

// Request dapplist and ethereumlists in parallel since they're both just GitHub repos
const internalPromise = getSpenderDataFromInternal(address, chainId);
const ethereumListsPromise = getSpenderDataFromEthereumList(address, chainId);

const data = (await internalPromise) ?? (await ethereumListsPromise);
// Check Harpie only if the whois doesn't have a name, because this is a rate-limited API
const data = (await getSpenderDataFromWhois(address, chainId)) ?? (await getSpenderDataFromHarpie(address, chainId));

return data;
};

const getSpenderDataFromInternal = async (address: Address, chainId: number): Promise<SpenderData | null> => {
const getSpenderDataFromWhois = async (address: string, chainId: number): Promise<SpenderData | null> => {
try {
const res = await fetch(`${DATA_BASE_URL}/spenders/${chainId}/${getAddress(address)}.json`);
const data = await res.json();
return data;
const response = await fetch(`${WHOIS_BASE_URL}/spenders/${chainId}/${getAddress(address)}.json`);
if (!response.ok) return null;
return await response.json();
} catch {
return null;
}
};

const getSpenderDataFromEthereumList = async (address: Address, chainId: number): Promise<SpenderData | null> => {
try {
const contractRes = await fetch(`${ETHEREUM_LISTS_CONTRACTS}/contracts/${chainId}/${getAddress(address)}.json`);
const contractData = await contractRes.json();

try {
const projectRes = await fetch(`${ETHEREUM_LISTS_CONTRACTS}/projects/${contractData.project}.json`);
const projectData = await projectRes.json();
return { name: projectData.name };
} catch {}
const getSpenderDataFromHarpie = async (address: string, chainId: number): Promise<SpenderData | null> => {
const apiKey = HARPIE_API_KEY;
if (!apiKey || chainId !== 1) return null;

return { name: contractData.project };
} catch {
try {
const response = await fetch('https://api.harpie.io/getprotocolfromcontract', {
method: 'POST',
body: JSON.stringify({ apiKey, address }),
});
if (!response.ok) return null;
const data = await response.json();
if (!data?.contractOwner || data?.contractOwner === 'NO_DATA') return null;
return { name: data.contractOwner };
} catch (e) {
return null;
}
};

0 comments on commit 33fe67f

Please sign in to comment.