Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
feat: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nofir committed Sep 1, 2023
1 parent ec5fa3f commit 3141896
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/fetchers/opensea.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
}`;
Expand All @@ -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,
Expand All @@ -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 }
);

Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 3141896

Please sign in to comment.