Skip to content

Instantly share code, notes, and snippets.

@gre
Last active October 2, 2019 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gre/270049efd68420a06f7e55507afef4fd to your computer and use it in GitHub Desktop.
Save gre/270049efd68420a06f7e55507afef4fd to your computer and use it in GitHub Desktop.
require("babel-polyfill");
const request = require("request");
const { listTokens } = require("@ledgerhq/live-common/lib/currencies");
require("@ledgerhq/live-common/lib/load/tokens/ethereum/erc20");
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const fetch = url =>
new Promise((resolve, reject) =>
request(url, (error, response, body) => {
if (error) {
reject(error);
} else {
resolve(body);
}
})
);
const getEtherscanScore = a =>
fetch(`https://etherscan.io/token/${a}`).then(r => {
const m = r.match(/Reputation ([a-zA-Z0-9]+)/);
const reputation = (m && m[1]) || "";
const blueChecked = r.includes("Blue Check Mark");
return [reputation, blueChecked];
});
const getERC20HodlerCountFallback = a =>
fetch(
`https://etherscan.io/token/generic-tokenholders2?m=normal&a=${a}&s=1590057493363429`
).then(r => {
const m = r.match(/total of ([0-9,]+)/);
if (m) {
return parseInt(m[1].replace(",", ""), 10);
} else {
console.error(r);
console.error("no holder for " + a);
}
return 0;
});
const getERC20HodlerCount = a =>
fetch(`https://etherscan.io/token/tokenholderchart/${a}`).then(r => {
const m = r.match(/Token Holders: ([0-9,]+)/);
const n = r.match(/own ([0-9.]+)%/);
if (m) {
return [
parseInt(m[1].replace(",", ""), 10),
n ? parseFloat(n[1]) / 100 : 0
];
}
return getERC20HodlerCountFallback(a)
.catch(e => {
console.error(e.message);
return 0;
})
.then(c => [c, 0]);
});
listTokens()
.reduce(
(p, token) =>
p
.then(r => delay(500).then(() => r))
.then(all =>
getERC20HodlerCount(token.contractAddress)
.then(fields =>
getEtherscanScore(token.contractAddress)
.catch(e => ["", ""])
.then(r => [...fields, ...r])
)
.then(all => {
return [...all, token.id, token.ticker, token.contractAddress];
})
.then(r => all.concat([r]))
),
Promise.resolve([])
)
.then(all => all.sort((a, b) => a[0] - b[0]))
.then(r =>
console.log(r.map(row => row.map(o => String(o)).join("\t")).join("\n"))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment