Skip to content

Instantly share code, notes, and snippets.

@doomsower
Created December 14, 2023 11:56
Show Gist options
  • Save doomsower/df26a336c1de48dd3c690b1fcad0a459 to your computer and use it in GitHub Desktop.
Save doomsower/df26a336c1de48dd3c690b1fcad0a459 to your computer and use it in GitHub Desktop.
Script to update abis
import { ethers } from "ethers";
import fs from "fs";
import path from "path";
// Define the callback function type
type ProcessJsonCallback = (json: any, path: string) => void;
// Function to process a JSON file
const processJsonFile = (filePath: string, callback: ProcessJsonCallback) => {
try {
const data = fs.readFileSync(filePath, "utf-8");
const json = JSON.parse(data);
callback(json, filePath);
} catch (error) {
console.error(`Error processing ${filePath}:`, error);
}
};
// Recursive function to walk through directory and find JSON files
const traverseDirectory = (dirPath: string, callback: ProcessJsonCallback) => {
fs.readdirSync(dirPath).forEach(file => {
const fullPath = path.join(dirPath, file);
if (fs.statSync(fullPath).isDirectory()) {
traverseDirectory(fullPath, callback);
} else if (path.extname(fullPath) === ".json") {
processJsonFile(fullPath, callback);
}
});
};
function getAllAbis() {
const out: Record<string, any> = { ...oldAbis };
traverseDirectory(path.resolve(__dirname, "..", "forge-out"), file => {
try {
const [contractPath, name]: string[] = Object.entries(
file.metadata.settings.compilationTarget,
)[0] as any;
if (contractPath.includes("/interfaces/")) {
return;
}
const contract = new ethers.Contract("0x0", file.abi);
const abis: string[] = [];
contract.interface.forEachFunction(fn => {
abis.push(fn.format("full"));
});
if (abis.length) {
out[name] = abis;
}
} catch (e) {
console.error(e);
}
});
fs.writeFileSync(
path.resolve(__dirname, "../abis.json"),
JSON.stringify(out, null, 2),
"utf-8",
);
}
getAllAbis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment