boardroom-inc-protocol-Info/scripts/bulk_add.ts

88 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-04-22 18:48:54 +00:00
import axios from "axios";
2021-04-22 21:02:32 +00:00
import fs from "fs";
2021-04-22 18:48:54 +00:00
2021-04-22 18:25:49 +00:00
const SNAPSHOT_API = "https://hub.snapshot.page/api/";
2021-04-22 18:48:54 +00:00
interface Space {
strategies: any[];
plugins: any;
filters: any;
name: string;
network: string; // '1', '56', etc
members?: [];
2021-04-22 18:48:54 +00:00
symbol: string;
domain: string;
about: string;
twitter: string;
github: string;
}
function filterObject(obj: any, predicate: Function) {
let result: Record<string, any> = {};
for (let key in obj) {
if (obj.hasOwnProperty(key) && predicate(obj[key])) {
2021-04-22 18:48:54 +00:00
result[key] = obj[key];
}
}
return result;
}
async function fetchAllSpaces(): Promise<Record<string, Space>> {
return axios.get(`${SNAPSHOT_API}/spaces`).then((res) => {
const spaces = res.data;
return spaces;
2021-04-22 18:25:49 +00:00
});
}
2021-04-22 18:48:54 +00:00
async function fetchMainnetSpaces() {
const allSpaces = await fetchAllSpaces();
const filtered = filterObject(allSpaces, (space: Space) => space.network === "1");
2021-04-22 18:48:54 +00:00
return filtered;
}
2021-04-22 19:23:19 +00:00
function extractTokenAddress(space: Space): string | null {
const strategyWithAddress = space.strategies.find((strategy) => strategy.name === "erc20-balance-of");
if (strategyWithAddress) {
return strategyWithAddress.params.address;
} else {
return null;
}
}
2021-04-22 21:02:32 +00:00
async function extractTokenAbi(tokenAddress: string) {
const data = await axios.get(
`https://api.etherscan.io/api?module=contract&action=getabi&address=${tokenAddress}&apikey=${process.env.ETHERSCAN_API_TOKEN}`,
);
console.log(data);
}
async function run() {
const allMainnetSpaces = await fetchMainnetSpaces();
2021-04-22 18:48:54 +00:00
2021-04-22 19:23:19 +00:00
// console.log(Object.keys(allMainnetSpaces).length);
2021-04-22 18:48:54 +00:00
const withMembers = filterObject(allMainnetSpaces, (space: Space) => space.members && space.members.length > 4);
2021-04-22 19:23:19 +00:00
// console.log(Object.keys(withMembers).length);
2021-04-22 21:02:32 +00:00
fs.writeFileSync("./allprotocols.json", JSON.stringify(withMembers));
2021-04-22 19:23:19 +00:00
for (let i = 0; i < Object.keys(withMembers).length; i++) {
const key = Object.keys(withMembers)[i];
const tokenAddress = extractTokenAddress(withMembers[key]);
2021-04-22 21:02:32 +00:00
const tokenAbi = extractTokenAbi(tokenAddress);
2021-04-22 19:23:19 +00:00
console.log(tokenAddress);
}
}
run();