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

Commit

Permalink
Merge pull request #223 from reservoirprotocol/deor/platf-1815-onchai…
Browse files Browse the repository at this point in the history
…n-social-links-metadata-plans-to-implement

feat: add normalizeMetadata
  • Loading branch information
d3or authored Aug 31, 2023
2 parents 43dda3d + 8cf97ca commit 3ad91c6
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 30 deletions.
6 changes: 2 additions & 4 deletions src/fetchers/manifold.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from "axios";
import { normalizeMetadata } from "../shared/utils";

export const fetchCollection = async (_chainId, { contract, instanceId }) => {
const result = await axios
Expand All @@ -9,10 +10,7 @@ export const fetchCollection = async (_chainId, { contract, instanceId }) => {
id: contract,
slug: result.slug,
name: result.publicData.name,
metadata: {
description: result.publicData.description ?? null,
imageUrl: result.publicData.image ?? null,
},
metadata: normalizeMetadata(result.publicData),
contract,
tokenSetId: `contract:${contract}`,
};
Expand Down
8 changes: 3 additions & 5 deletions src/fetchers/onchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { RequestWasThrottledError } from "./errors";
import { supportedChains } from "../shared/utils";
import _ from "lodash";
import { logger } from "../shared/logger";
import { normalizeMetadata } from "../shared/utils";

const FETCH_TIMEOUT = 30000;

Expand Down Expand Up @@ -143,7 +144,7 @@ const getCollectionMetadata = async (contractAddress, rpcURL) => {
}).then((response) => response.json());

return json;
} catch {
} catch (e) {
return null;
}
};
Expand Down Expand Up @@ -402,10 +403,7 @@ export const fetchCollection = async (chainId, { contract }) => {
id: contract,
slug: slugify(collectionName, { lower: true }),
name: collectionName,
metadata: {
description: collection?.description ?? null,
imageUrl: normalizeLink(collection?.image) ?? null,
},
metadata: normalizeMetadata(collection),
contract,
tokenSetId: `contract:${contract}`,
};
Expand Down
14 changes: 2 additions & 12 deletions src/fetchers/opensea.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from "axios";
import { Contract } from "ethers";
import { Interface } from "ethers/lib/utils";
import slugify from "slugify";
import { getProvider } from "../shared/utils";
import { getProvider, normalizeMetadata } from "../shared/utils";
import { logger } from "../shared/logger";
import { RequestWasThrottledError } from "./errors";
import { parse } from "../parsers/opensea";
Expand Down Expand Up @@ -241,17 +241,7 @@ export const fetchCollection = async (chainId, { contract, tokenId }) => {
slug: data.collection.slug,
name: data.collection ? data.collection.name : data.name,
community: communities[contract] || null,
metadata: data.collection
? {
description: data.collection.description,
imageUrl: data.collection.image_url,
bannerImageUrl: data.collection.banner_image_url,
discordUrl: data.collection.discord_url,
externalUrl: data.collection.external_url,
twitterUsername: data.collection.twitter_username,
safelistRequestStatus: data.collection.safelist_request_status,
}
: null,
metadata: data.collection ? normalizeMetadata(data.collection) : null,
openseaRoyalties: royalties,
openseaFees: fees,
contract,
Expand Down
11 changes: 2 additions & 9 deletions src/fetchers/simplehash.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Interface } from "ethers/lib/utils";
import slugify from "slugify";

import { parse } from "../parsers/simplehash";
import { supportedChains, getProvider } from "../shared/utils";
import { supportedChains, getProvider, normalizeMetadata } from "../shared/utils";
import { logger } from "../shared/logger";
import _ from "lodash";

Expand Down Expand Up @@ -58,14 +58,7 @@ export const fetchCollection = async (chainId, { contract, tokenId }) => {
slug,
name: data.collection.name,
community: null,
metadata: {
description: data.collection.description,
imageUrl: data.collection.image_url,
bannerImageUrl: data.collection.banner_image_url,
discordUrl: data.collection.discord_url,
externalUrl: data.collection.external_url,
twitterUsername: data.collection.twitter_username,
},
metadata: normalizeMetadata(data.collection),
contract,
tokenIdRange: null,
tokenSetId: `contract:${contract}`,
Expand Down
123 changes: 123 additions & 0 deletions src/shared/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { providers } from "ethers";
import _ from "lodash";
import { normalizeLink } from "../parsers/onchain";

export const getProvider = (chainId) => {
if (chainId === 43114) {
Expand All @@ -21,3 +22,125 @@ export const supportedChains = process.env.SUPPORTED_CHAINS.split(",").reduce(
(acc, val) => ((acc[Number(val.split(":")[0])] = val.split(":")[1]), acc),
{}
);

export const normalizeMetadata = (collection) => {
if (!collection) {
return {};
}
let map = {
discord: {
key: "discordUrl",
},
discord_url: {
key: "discordUrl",
},
twitter_username: {
key: "twitterUsername",
normalize: (value) => {
// if the value is a url, return the username
if (value.includes("twitter.com")) {
return value.split("/")[3];
}

return value;
},
},
twitter: {
key: "twitterUrl",
normalize: (value) => {
if (value.includes("twitter.com")) {
return value;
}
// if the value is a username, return the url
return `https://twitter.com/${value}`;
},
},
telegram: {
key: "telegramUrl",
normalize: (value) => {
if (value.includes("t.me")) {
return value;
}

return `https://t.me/${value}`;
},
},
instagram: {
key: "instagramUrl",
normalize: (value) => {
if (value.includes("instagram.com")) {
return value;
}
},
},
medium: {
key: "mediumUrl",
},
github: {
key: "githubUrl",
},
website: {
key: "externalUrl",
normalize: (value) => normalizeLink(value),
},
website_url: {
key: "externalUrl",
normalize: (value) => normalizeLink(value),
},
external_url: {
key: "externalUrl",
normalize: (value) => normalizeLink(value),
},
image: {
key: "imageUrl",
normalize: (value) => normalizeLink(value),
},
image_url: {
key: "imageUrl",
normalize: (value) => normalizeLink(value),
},
cover_image: {
key: "bannerImageUrl",
normalize: (value) => normalizeLink(value),
},
banner_image_url: {
key: "bannerImageUrl",
normalize: (value) => normalizeLink(value),
},
safelist_request_status: {
key: "safelistRequestStatus",
},
name: {
key: "name",
},
description: {
key: "description",
},
};

let metadata = {};
if (collection?.social_urls) {
Object.keys(collection.social_urls).forEach((key) => {
if (key in map) {
if (map[key].normalize) {
metadata[map[key].key] = map[key].normalize(collection.social_urls[key]);
} else {
metadata[map[key].key] = collection.social_urls[key];
}
}
});
}

// do the above via the map
Object.keys(map).forEach((key) => {
if (key in collection) {
if (map[key].normalize) {
metadata[map[key].key] = map[key].normalize(collection[key]);
} else {
metadata[map[key].key] = collection[key];
}
}
});

return metadata;
};

0 comments on commit 3ad91c6

Please sign in to comment.