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

Commit

Permalink
Merge branch 'ofir/platf-2042-change-collection-fallback-logic-to-use…
Browse files Browse the repository at this point in the history
…-dedicated-rpc-url' into staging
  • Loading branch information
nofir committed Sep 7, 2023
2 parents 279cc96 + 1b089b8 commit a34dc7c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
33 changes: 21 additions & 12 deletions src/fetchers/opensea.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import axios from "axios";
import { Contract } from "ethers";
import { Interface } from "ethers/lib/utils";

import slugify from "slugify";
import { getProvider, normalizeMetadata } from "../shared/utils";

import { logger } from "../shared/logger";
import { RequestWasThrottledError } from "./errors";
import { parse } from "../parsers/opensea";
Expand Down Expand Up @@ -45,20 +47,26 @@ const getOSNetworkName = (chainId) => {
return "base_goerli";
case 999:
return "zora_testnet";
case 324:
return "zksync";
}
};

const getOSTestNetsChainIds = () => {
return [4, 5, 11155111, 80001, 84531, 999];
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 = `${
!getOSTestNetsChainIds().includes(chainId)
? "https://api.opensea.io"
: "https://testnets-api.opensea.io"
!isOSTestnet(chainId) ? "https://api.opensea.io" : "https://testnets-api.opensea.io"
}`;

switch (api) {
Expand All @@ -80,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 = !getOSTestNetsChainIds().includes(chainId)

const headers = !isOSTestnet(chainId)
? {
url,
"X-API-KEY": apiKey,
Expand All @@ -92,7 +101,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,
!isOSTestnet(chainId) ? process.env.OPENSEA_BASE_URL_ALT || url : url,
{ headers }
);

Expand Down Expand Up @@ -305,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(),
Expand Down
16 changes: 6 additions & 10 deletions src/shared/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,7 +34,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 +44,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 +54,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 +64,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 a34dc7c

Please sign in to comment.