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

121 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-04-23 19:01:04 +00:00
import { exec } from "child_process";
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;
2021-04-23 19:53:02 +00:00
private?: boolean;
2021-04-22 18:48:54 +00:00
github: string;
}
2021-04-23 19:53:02 +00:00
const deadOrInvalidSpaces = [
"FinNexus",
"ForTube(Ethereum)",
"GoldMining Token",
"Percent",
"QIAN(ETH)",
"ROPETHEVOTE",
"yearn.finance (archive)",
"KAIJU",
"OMG",
"Evolution Land",
"Yam Finance Signal",
"BIOPset House of Representatives",
"BeetsDAO",
"Scoobi-doge",
];
2021-04-22 18:48:54 +00:00
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();
2021-04-23 19:53:02 +00:00
const filtered = filterObject(allSpaces, (space: Space) => space.network === "1" && !space.private);
2021-04-22 18:48:54 +00:00
return filtered;
}
2021-04-22 19:23:19 +00:00
function extractTokenAddress(space: Space): string | null {
2021-04-23 19:53:02 +00:00
const strategyWithAddress = space.strategies.find(
(strategy) => strategy.name === "erc20-balance-of" || strategy.name === "ctoken",
);
2021-04-22 19:23:19 +00:00
if (strategyWithAddress) {
return strategyWithAddress.params.address;
} else {
2021-04-23 19:53:02 +00:00
console.log(space);
2021-04-22 19:23:19 +00:00
return null;
}
}
2021-04-23 19:01:04 +00:00
async function extractTokenAbi(tokenAddress: string | null) {
if (!tokenAddress) {
return;
}
const res = await axios.get(
2021-04-22 21:02:32 +00:00
`https://api.etherscan.io/api?module=contract&action=getabi&address=${tokenAddress}&apikey=${process.env.ETHERSCAN_API_TOKEN}`,
);
2021-04-23 19:01:04 +00:00
return res.data.result;
2021-04-22 21:02:32 +00:00
}
async function run() {
const allMainnetSpaces = await fetchMainnetSpaces();
2021-04-22 18:48:54 +00:00
const withMembers = filterObject(allMainnetSpaces, (space: Space) => space.members && space.members.length > 4);
2021-04-23 19:53:02 +00:00
const notDed = filterObject(withMembers, (space: Space) => !deadOrInvalidSpaces.includes(space.name));
2021-04-22 21:02:32 +00:00
fs.writeFileSync("./allprotocols.json", JSON.stringify(withMembers));
2021-04-23 19:53:02 +00:00
for (let i = 0; i < Object.keys(notDed).length; i++) {
const key = Object.keys(notDed)[i];
2021-04-22 19:23:19 +00:00
2021-04-23 19:53:02 +00:00
const tokenAddress = extractTokenAddress(notDed[key]);
2021-04-23 19:01:04 +00:00
2021-04-23 19:53:02 +00:00
exec(`sh ./scripts/add_new_protocol.sh ${key} ${tokenAddress}`, (error, stdout, stderr) => {
2021-04-23 19:01:04 +00:00
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
2021-04-23 19:53:02 +00:00
if (tokenAddress) {
const tokenAbi = await extractTokenAbi(tokenAddress);
2021-04-23 19:01:04 +00:00
2021-04-23 19:53:02 +00:00
fs.writeFileSync(`./protocols/${key}/contracts/token.json`, tokenAbi);
}
2021-04-22 19:23:19 +00:00
}
}
run();