Commit b553a6fc authored by eddie's avatar eddie Committed by GitHub

feat: nfts sitemap (#7456)

* feat: add nft collections to sitemap

* feat: size check on sitemap

* fix: update generate-sitemap script
parent ad1e2c60
This diff is collapsed.
...@@ -6,7 +6,7 @@ const { parseStringPromise, Builder } = require('xml2js') ...@@ -6,7 +6,7 @@ const { parseStringPromise, Builder } = require('xml2js')
const weekMs = 7 * 24 * 60 * 60 * 1000 const weekMs = 7 * 24 * 60 * 60 * 1000
const nowISO = new Date().toISOString() const nowISO = new Date().toISOString()
const getQuery = (chain) => ` const getTopTokensQuery = (chain) => `
query { query {
topTokens(pageSize: 100, page: 1, chain: ${chain}, orderBy: VOLUME) { topTokens(pageSize: 100, page: 1, chain: ${chain}, orderBy: VOLUME) {
address address
...@@ -15,6 +15,20 @@ const getQuery = (chain) => ` ...@@ -15,6 +15,20 @@ const getQuery = (chain) => `
` `
const chains = ['ETHEREUM', 'ARBITRUM', 'OPTIMISM', 'POLYGON', 'BASE', 'BNB', 'CELO'] const chains = ['ETHEREUM', 'ARBITRUM', 'OPTIMISM', 'POLYGON', 'BASE', 'BNB', 'CELO']
const nftTopCollectionsQuery = `
query {
topCollections(first: 100, duration: MAX) {
edges {
node {
nftContracts {
address
}
}
}
}
}
`
fs.readFile('./public/sitemap.xml', 'utf8', async (err, data) => { fs.readFile('./public/sitemap.xml', 'utf8', async (err, data) => {
const sitemapURLs = {} const sitemapURLs = {}
try { try {
...@@ -30,15 +44,15 @@ fs.readFile('./public/sitemap.xml', 'utf8', async (err, data) => { ...@@ -30,15 +44,15 @@ fs.readFile('./public/sitemap.xml', 'utf8', async (err, data) => {
} }
for (const chainName of chains) { for (const chainName of chains) {
const response = await fetch('https://api.uniswap.org/v1/graphql', { const tokensResponse = await fetch('https://api.uniswap.org/v1/graphql', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Origin: 'https://app.uniswap.org', Origin: 'https://app.uniswap.org',
}, },
body: JSON.stringify({ query: getQuery(chainName) }), body: JSON.stringify({ query: getTopTokensQuery(chainName) }),
}) })
const tokensJSON = await response.json() const tokensJSON = await tokensResponse.json()
const tokenAddresses = tokensJSON.data.topTokens.map((token) => token.address.toLowerCase()) const tokenAddresses = tokensJSON.data.topTokens.map((token) => token.address.toLowerCase())
tokenAddresses.forEach((address) => { tokenAddresses.forEach((address) => {
...@@ -53,10 +67,39 @@ fs.readFile('./public/sitemap.xml', 'utf8', async (err, data) => { ...@@ -53,10 +67,39 @@ fs.readFile('./public/sitemap.xml', 'utf8', async (err, data) => {
}) })
} }
const nftResponse = await fetch('https://api.uniswap.org/v1/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Origin: 'https://app.uniswap.org',
},
body: JSON.stringify({ query: nftTopCollectionsQuery }),
})
const nftJSON = await nftResponse.json()
const collectionAddresses = nftJSON.data.topCollections.edges.map((edge) => edge.node.nftContracts[0].address)
collectionAddresses.forEach((address) => {
const collectionURL = `https://app.uniswap.org/nfts/collection/${address}`
if (!(collectionURL in sitemapURLs)) {
sitemap.urlset.url.push({
loc: [collectionURL],
lastmod: [nowISO],
priority: [0.7],
})
}
})
const builder = new Builder() const builder = new Builder()
const xml = builder.buildObject(sitemap) const xml = builder.buildObject(sitemap)
fs.writeFile('./public/sitemap.xml', xml, (error) => { const path = './public/sitemap.xml'
fs.writeFile(path, xml, (error) => {
if (error) throw error if (error) throw error
const stats = fs.statSync(path)
const fileSizeBytes = stats.size
const fileSizeMegabytes = fileSizeBytes / (1024 * 1024)
if (fileSizeMegabytes > 50) {
throw new Error('Generated sitemap file size exceeds 50MB')
}
console.log('Sitemap updated') console.log('Sitemap updated')
}) })
} catch (e) { } catch (e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment