Skip to content

Instantly share code, notes, and snippets.

@JWPapi
Created October 4, 2022 10:29
Show Gist options
  • Save JWPapi/0ae057ca093b085b9dc678546328faf5 to your computer and use it in GitHub Desktop.
Save JWPapi/0ae057ca093b085b9dc678546328faf5 to your computer and use it in GitHub Desktop.
import axios from "axios"
import _ from "lodash"
import * as cheerio from "cheerio"
export default async function handler(req, res) {
const {address, start, end} = req.query
const tokenIds = _.range(start, end)
const urls = tokenIds.map((tokenId) => polygonScanUrlGenerator(address, tokenId))
const responses = await Promise.all(urls.map((url) => axios.get(url)))
const htmls = responses.map((response) => response.data)
const owners = htmls.map((html) => {
const $ = cheerio.load(html)
return $(`#resulttable .table tbody tr td:nth-child(2) a`).text()
})
const uniqueOwners = _.uniq(owners)
let failures = 0
const unMoonedWallets = await Promise.all(uniqueOwners.map(async (wallet, i) => {
await sleep(100, i)
const url = `https://nova-explorer.arbitrum.io/api?module=account&action=tokentx&address=${wallet}`
const response = await axios.get(url).catch((e) => {
failures++
console.log(e)
}
)
return response?.data.message === "No token transfers found" ? wallet : null
})).then((e) => e.filter(e => e))
res.json({uniqueOwners, unMoonedWallets, failures})
}
const polygonScanUrlGenerator = (address, tokenId) => {
return `https://polygonscan.com/token/generic-tokenholder-inventory?m=normal&contractAddress=${address}&a=${tokenId}&pUrl=token`
}
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment