2023-10-05 12:19:58 -07:00
|
|
|
/* eslint-env node */
|
|
|
|
|
|
|
|
const fs = require('fs')
|
|
|
|
const { parseStringPromise, Builder } = require('xml2js')
|
|
|
|
|
2023-10-06 17:08:46 -07:00
|
|
|
const weekMs = 7 * 24 * 60 * 60 * 1000
|
|
|
|
const nowISO = new Date().toISOString()
|
|
|
|
|
2023-10-13 09:17:57 -07:00
|
|
|
const getTopTokensQuery = (chain) => `
|
2023-10-06 17:08:46 -07:00
|
|
|
query {
|
|
|
|
topTokens(pageSize: 100, page: 1, chain: ${chain}, orderBy: VOLUME) {
|
|
|
|
address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
const chains = ['ETHEREUM', 'ARBITRUM', 'OPTIMISM', 'POLYGON', 'BASE', 'BNB', 'CELO']
|
|
|
|
|
2023-10-13 09:17:57 -07:00
|
|
|
const nftTopCollectionsQuery = `
|
|
|
|
query {
|
|
|
|
topCollections(first: 100, duration: MAX) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
nftContracts {
|
|
|
|
address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2023-10-05 12:19:58 -07:00
|
|
|
fs.readFile('./public/sitemap.xml', 'utf8', async (err, data) => {
|
2023-10-06 17:08:46 -07:00
|
|
|
const sitemapURLs = {}
|
2023-10-05 12:19:58 -07:00
|
|
|
try {
|
|
|
|
const sitemap = await parseStringPromise(data)
|
|
|
|
if (sitemap.urlset.url) {
|
|
|
|
sitemap.urlset.url.forEach((url) => {
|
2023-10-11 15:23:15 -07:00
|
|
|
const lastMod = new Date(url.lastmod).getTime()
|
2023-10-06 17:08:46 -07:00
|
|
|
if (lastMod < Date.now() - weekMs) {
|
2023-10-11 15:23:15 -07:00
|
|
|
url.lastmod = nowISO
|
2023-10-06 17:08:46 -07:00
|
|
|
}
|
2023-10-11 15:23:15 -07:00
|
|
|
sitemapURLs[url.loc] = true
|
2023-10-05 12:19:58 -07:00
|
|
|
})
|
|
|
|
}
|
2023-10-06 17:08:46 -07:00
|
|
|
|
|
|
|
for (const chainName of chains) {
|
2023-10-13 09:17:57 -07:00
|
|
|
const tokensResponse = await fetch('https://api.uniswap.org/v1/graphql', {
|
2023-10-06 17:08:46 -07:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Origin: 'https://app.uniswap.org',
|
|
|
|
},
|
2023-10-13 09:17:57 -07:00
|
|
|
body: JSON.stringify({ query: getTopTokensQuery(chainName) }),
|
2023-10-06 17:08:46 -07:00
|
|
|
})
|
2023-10-13 09:17:57 -07:00
|
|
|
const tokensJSON = await tokensResponse.json()
|
2023-10-06 17:08:46 -07:00
|
|
|
const tokenAddresses = tokensJSON.data.topTokens.map((token) => token.address.toLowerCase())
|
|
|
|
|
|
|
|
tokenAddresses.forEach((address) => {
|
|
|
|
const tokenURL = `https://app.uniswap.org/tokens/${chainName.toLowerCase()}/${address}`
|
|
|
|
if (!(tokenURL in sitemapURLs)) {
|
|
|
|
sitemap.urlset.url.push({
|
2023-10-11 15:23:15 -07:00
|
|
|
loc: [tokenURL],
|
|
|
|
lastmod: [nowISO],
|
|
|
|
priority: [0.8],
|
2023-10-06 17:08:46 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-13 09:17:57 -07:00
|
|
|
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],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-10-05 12:19:58 -07:00
|
|
|
const builder = new Builder()
|
|
|
|
const xml = builder.buildObject(sitemap)
|
2023-10-13 09:17:57 -07:00
|
|
|
const path = './public/sitemap.xml'
|
|
|
|
fs.writeFile(path, xml, (error) => {
|
2023-10-05 12:19:58 -07:00
|
|
|
if (error) throw error
|
2023-10-13 09:17:57 -07:00
|
|
|
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')
|
|
|
|
}
|
2023-10-05 12:19:58 -07:00
|
|
|
console.log('Sitemap updated')
|
|
|
|
})
|
2023-10-06 17:08:46 -07:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
2023-10-05 12:19:58 -07:00
|
|
|
}
|
|
|
|
})
|