Skip to content
This repository was archived by the owner on Aug 12, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions src/jobs/get-missing-token-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,62 @@ const Token = require('../model/token');

const logger = signale.scope('get missing token images');

const getMissingTokenImages = async () => {
const tokens = await Token.find({}, '_id address imageUrl').lean();
const getAssetsTreeSHA = async () => {
const response = await axios.get(
'https://api.github.com/repos/TrustWallet/assets/contents/blockchains/ethereum',
);

logger.info(`${tokens.length} tokens were found`);
logger.pending('fetching images from Trust Wallet repository');
const assetsTree = response.data.find(
tree => tree.path === 'blockchains/ethereum/assets',
);

return assetsTree.sha;
};

const getTree = async treeSHA => {
const response = await axios.get(
'https://api.github.com/repos/TrustWallet/assets/git/trees/master?recursive=1',
`https://api.github.com/repos/TrustWallet/assets/git/trees/${treeSHA}`,
);

if (!_.isArray(_.get(response, 'data.tree'))) {
throw new Error(
'Data returned by Trust Wallet repository was in unexpected format',
);
}
return response.data.tree;
};

const getMissingTokenImages = async () => {
const tokens = await Token.find({}, '_id address imageUrl').lean();
const filteredTokens = tokens.filter(token => _.isEmpty(token.imageUrl));

logger.info(`${filteredTokens.length} tokens were found without images`);
logger.pending('fetching images from Trust Wallet repository');

const assetsTreeSHA = await getAssetsTreeSHA();
const assetsTree = await getTree(assetsTreeSHA);

const operations = flow(
map(token => {
const image = _.find(
response.data.tree,
item =>
item.path.toUpperCase() ===
`blockchains/ethereum/assets/${token.address}/logo.png`.toUpperCase(),
const assetTree = _.find(
assetsTree,
tree => tree.path.toUpperCase() === token.address.toUpperCase(),
);

if (_.isUndefined(image) || token.imageUrl !== null) {
if (assetTree === undefined) {
return null;
}

const assetPath = `blockchains/ethereum/assets/${assetTree.path}/logo.png`;

return {
updateOne: {
filter: { _id: token._id },
update: {
$set: {
imageUrl: `https://raw.githubusercontent.com/TrustWallet/assets/master/${image.path}`,
imageUrl: `https://raw.githubusercontent.com/TrustWallet/assets/master/${assetPath}`,
},
},
},
};
}),
compact,
)(tokens);
)(filteredTokens);

if (operations.length === 0) {
logger.info('no new token images were found');
Expand Down