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

Commit

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

Expand Down Expand Up @@ -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(),
Expand Down
8 changes: 4 additions & 4 deletions src/shared/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand All @@ -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
Expand All @@ -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;
}

Expand All @@ -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;
}
},
Expand Down

0 comments on commit ec5fa3f

Please sign in to comment.