Skip to content

Instantly share code, notes, and snippets.

@raddy
Created April 3, 2022 22:59
Show Gist options
  • Save raddy/394dac77d7e6a28ee5654ee54f9f74d3 to your computer and use it in GitHub Desktop.
Save raddy/394dac77d7e6a28ee5654ee54f9f74d3 to your computer and use it in GitHub Desktop.
Fetch anyone whoever approved a token to an address
const { program } = require("commander");
const fetch = require('node-fetch');
//const fs = require('fs');
//const outFile = "users.txt";
const urlRoot = 'https://api.etherscan.io/api?module=logs&action=getLogs&';
const fetchApprovals = async (
startBlock,
token,
target,
apiKey
) => {
const suffix= `topic0=0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925&topic2=0x000000000000000000000000${target.slice(2)}`
const query = `${urlRoot}fromBlock=${startBlock}&toBlock=latest&address=${token}&${suffix}&apikey=${apiKey}`
const resp = await fetch(query);
const data = await resp.json();
let users = new Set();
data["result"].forEach((approval) => {
const topics = approval['topics'];
users.add(topics[1].slice(26));
});
//fs.writeFile(outFile, JSON.stringify([...users]), (err) => {});
console.log(users);
};
const main = async () => {
program
.argument("<token>", "ERC20 token")
.argument("<target>", "Target Contract")
.argument("<apikey>", "Etherscan Api Key")
.option("--block <block>", "Starting Block", 379224)
.action(async (token, target, apiKey, options) => {
await fetchApprovals(
options.block,
token,
target,
apiKey
);
});
program.parseAsync();
};
main().catch((e) => console.error(e.toString()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment