diff --git a/src/fetchers/opensea.js b/src/fetchers/opensea.js index dcba2f9..2efd7f7 100644 --- a/src/fetchers/opensea.js +++ b/src/fetchers/opensea.js @@ -37,12 +37,36 @@ const getOSNetworkName = (chainId) => { return "base"; case 7777777: return "zora"; + case 11155111: + return "sepolia"; + case 80001: + return "mumbai"; + case 84531: + return "base_goerli"; + case 999: + return "zora_testnet"; } }; +const isOSTestnet = (chainId) => { + switch (chainId) { + case 4: + case 5: + case 11155111: + case 80001: + case 84531: + case 999: + return true; + } + + return false; +}; + const getUrlForApi = (api, chainId, contract, tokenId, network, slug) => { const baseUrl = `${ - ![4, 5].includes(chainId) ? "https://api.opensea.io" : "https://testnets-api.opensea.io" + !isOSTestnet(chainId) + ? "https://api.opensea.io" + : "https://testnets-api.opensea.io" }`; switch (api) { @@ -64,7 +88,8 @@ const getUrlForApi = (api, chainId, contract, tokenId, network, slug) => { const getOSData = async (api, chainId, contract, tokenId, slug) => { const network = getOSNetworkName(chainId); const url = getUrlForApi(api, chainId, contract, tokenId, network, slug); - const headers = ![4, 5].includes(chainId) + + const headers = !isOSTestnet(chainId) ? { url, "X-API-KEY": apiKey, @@ -76,7 +101,7 @@ const getOSData = async (api, chainId, contract, tokenId, slug) => { try { const osResponse = await axios.get( - ![4, 5].includes(chainId) ? process.env.OPENSEA_BASE_URL_ALT || url : url, + !isOSTestnet(chainId) ? process.env.OPENSEA_BASE_URL_ALT || url : url, { headers } ); @@ -148,19 +173,6 @@ export const fetchCollection = async (chainId, { contract, tokenId }) => { let data; let creatorAddress; - if (chainId === 43114) { - logger.info( - "opensea-fetcher", - JSON.stringify({ - topic: "fetchCollectionDebug", - message: `Collection metadata start. contract=${contract}, tokenId=${tokenId}`, - contract, - tokenId, - data, - }) - ); - } - if (chainId === 1) { data = await getOSData("asset", chainId, contract, tokenId); creatorAddress = data?.creator?.address; @@ -185,19 +197,6 @@ export const fetchCollection = async (chainId, { contract, tokenId }) => { } } - if (chainId === 43114) { - logger.info( - "opensea-fetcher", - JSON.stringify({ - topic: "fetchCollectionDebug", - message: `Collection metadata debug. contract=${contract}, tokenId=${tokenId}`, - contract, - tokenId, - data, - }) - ); - } - if (!data?.collection) { throw new Error("Missing collection"); } @@ -315,12 +314,12 @@ export const fetchTokens = async (chainId, tokens) => { } const url = `${ - ![4, 5].includes(chainId) ? "https://api.opensea.io" : "https://testnets-api.opensea.io" + !isOSTestnet(chainId) ? "https://api.opensea.io" : "https://testnets-api.opensea.io" }/api/v1/assets?${searchParams.toString()}`; const data = await axios - .get(![4, 5].includes(chainId) ? process.env.OPENSEA_BASE_URL_ALT || url : url, { - headers: ![4, 5].includes(chainId) + .get(!isOSTestnet(chainId) ? process.env.OPENSEA_BASE_URL_ALT || url : url, { + headers: !isOSTestnet(chainId) ? { url, "X-API-KEY": process.env.OPENSEA_API_KEY.trim(), diff --git a/src/shared/utils.js b/src/shared/utils.js index 186c948..607d0ef 100644 --- a/src/shared/utils.js +++ b/src/shared/utils.js @@ -38,7 +38,7 @@ export const normalizeMetadata = (collection) => { key: "twitterUsername", normalize: (value) => { // if the value is a url, return the username - if (value.includes("twitter.com")) { + if (value?.includes("twitter.com")) { return value.split("/")[3]; } @@ -48,7 +48,7 @@ export const normalizeMetadata = (collection) => { twitter: { key: "twitterUrl", normalize: (value) => { - if (value.includes("twitter.com")) { + if (value?.includes("twitter.com")) { return value; } // if the value is a username, return the url @@ -58,7 +58,7 @@ export const normalizeMetadata = (collection) => { telegram: { key: "telegramUrl", normalize: (value) => { - if (value.includes("t.me")) { + if (value?.includes("t.me")) { return value; } @@ -68,7 +68,7 @@ export const normalizeMetadata = (collection) => { instagram: { key: "instagramUrl", normalize: (value) => { - if (value.includes("instagram.com")) { + if (value?.includes("instagram.com")) { return value; } },