From ec5fa3ff0f0690bf00da966b04e4d7f9ea641d7a Mon Sep 17 00:00:00 2001 From: nofir Date: Thu, 31 Aug 2023 21:13:01 -0400 Subject: [PATCH 1/3] feat: fix logic --- src/fetchers/opensea.js | 19 +++++++++---------- src/shared/utils.js | 8 ++++---- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/fetchers/opensea.js b/src/fetchers/opensea.js index 7080664..b8ebe17 100644 --- a/src/fetchers/opensea.js +++ b/src/fetchers/opensea.js @@ -13,6 +13,8 @@ const apiKey = process.env.OPENSEA_COLLECTION_API_KEY ? process.env.OPENSEA_COLLECTION_API_KEY.trim() : process.env.OPENSEA_API_KEY.trim(); +const osTestnetsChainIds = [4, 5, 11155111, 80001, 84531, 999]; + const getOSNetworkName = (chainId) => { switch (chainId) { case 1: @@ -50,13 +52,9 @@ const getOSNetworkName = (chainId) => { } }; -const getOSTestNetsChainIds = () => { - return [4, 5, 11155111, 80001, 84531, 999]; -}; - const getUrlForApi = (api, chainId, contract, tokenId, network, slug) => { const baseUrl = `${ - !getOSTestNetsChainIds().includes(chainId) + !osTestnetsChainIds.includes(chainId) ? "https://api.opensea.io" : "https://testnets-api.opensea.io" }`; @@ -80,7 +78,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 = !getOSTestNetsChainIds().includes(chainId) + + const headers = !osTestnetsChainIds.includes(chainId) ? { url, "X-API-KEY": apiKey, @@ -92,7 +91,7 @@ const getOSData = async (api, chainId, contract, tokenId, slug) => { try { const osResponse = await axios.get( - !getOSTestNetsChainIds().includes(chainId) ? process.env.OPENSEA_BASE_URL_ALT || url : url, + !osTestnetsChainIds.includes(chainId) ? process.env.OPENSEA_BASE_URL_ALT || url : url, { headers } ); @@ -305,12 +304,12 @@ export const fetchTokens = async (chainId, tokens) => { } const url = `${ - ![4, 5].includes(chainId) ? "https://api.opensea.io" : "https://testnets-api.opensea.io" + !osTestnetsChainIds.includes(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(!osTestnetsChainIds.includes(chainId) ? process.env.OPENSEA_BASE_URL_ALT || url : url, { + headers: !osTestnetsChainIds.includes(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; } }, From 3141896afcf8fb843e704c476ab8ecc5e7e6a3f9 Mon Sep 17 00:00:00 2001 From: nofir Date: Thu, 31 Aug 2023 21:19:25 -0400 Subject: [PATCH 2/3] feat: cleanup --- src/fetchers/opensea.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/fetchers/opensea.js b/src/fetchers/opensea.js index b8ebe17..2efd7f7 100644 --- a/src/fetchers/opensea.js +++ b/src/fetchers/opensea.js @@ -13,8 +13,6 @@ const apiKey = process.env.OPENSEA_COLLECTION_API_KEY ? process.env.OPENSEA_COLLECTION_API_KEY.trim() : process.env.OPENSEA_API_KEY.trim(); -const osTestnetsChainIds = [4, 5, 11155111, 80001, 84531, 999]; - const getOSNetworkName = (chainId) => { switch (chainId) { case 1: @@ -47,14 +45,26 @@ const getOSNetworkName = (chainId) => { return "base_goerli"; case 999: return "zora_testnet"; - case 324: - return "zksync"; } }; +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 = `${ - !osTestnetsChainIds.includes(chainId) + !isOSTestnet(chainId) ? "https://api.opensea.io" : "https://testnets-api.opensea.io" }`; @@ -79,7 +89,7 @@ const getOSData = async (api, chainId, contract, tokenId, slug) => { const network = getOSNetworkName(chainId); const url = getUrlForApi(api, chainId, contract, tokenId, network, slug); - const headers = !osTestnetsChainIds.includes(chainId) + const headers = !isOSTestnet(chainId) ? { url, "X-API-KEY": apiKey, @@ -91,7 +101,7 @@ const getOSData = async (api, chainId, contract, tokenId, slug) => { try { const osResponse = await axios.get( - !osTestnetsChainIds.includes(chainId) ? process.env.OPENSEA_BASE_URL_ALT || url : url, + !isOSTestnet(chainId) ? process.env.OPENSEA_BASE_URL_ALT || url : url, { headers } ); @@ -304,12 +314,12 @@ export const fetchTokens = async (chainId, tokens) => { } const url = `${ - !osTestnetsChainIds.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(!osTestnetsChainIds.includes(chainId) ? process.env.OPENSEA_BASE_URL_ALT || url : url, { - headers: !osTestnetsChainIds.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(), From 1b089b8681ea62637a10778eb5702d4c2b1bdc5e Mon Sep 17 00:00:00 2001 From: nofir Date: Wed, 6 Sep 2023 20:35:37 -0400 Subject: [PATCH 3/3] feat: replaced alchemy logic --- src/fetchers/opensea.js | 4 +--- src/shared/utils.js | 8 ++------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/fetchers/opensea.js b/src/fetchers/opensea.js index 6f92f09..b030373 100644 --- a/src/fetchers/opensea.js +++ b/src/fetchers/opensea.js @@ -66,9 +66,7 @@ const isOSTestnet = (chainId) => { const getUrlForApi = (api, chainId, contract, tokenId, network, slug) => { const baseUrl = `${ - !isOSTestnet(chainId) - ? "https://api.opensea.io" - : "https://testnets-api.opensea.io" + !isOSTestnet(chainId) ? "https://api.opensea.io" : "https://testnets-api.opensea.io" }`; switch (api) { diff --git a/src/shared/utils.js b/src/shared/utils.js index 607d0ef..d5e056b 100644 --- a/src/shared/utils.js +++ b/src/shared/utils.js @@ -3,12 +3,8 @@ import _ from "lodash"; import { normalizeLink } from "../parsers/onchain"; export const getProvider = (chainId) => { - if (chainId === 43114) { - const network = _.upperCase(supportedChains[chainId]).replace(" ", "_"); - return new providers.JsonRpcProvider(process.env[`RPC_URL_${network}`]); - } - - return new providers.AlchemyProvider(chainId, process.env.ALCHEMY_API_KEY); + const network = _.upperCase(supportedChains[chainId]).replace(" ", "_"); + return new providers.JsonRpcProvider(process.env[`RPC_URL_${network}`]); }; // Supported chains with key=network, value=chainId