Skip to content

Commit

Permalink
fix: include *Facet.sol in ABI (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
silochad authored Jun 27, 2023
2 parents 49b4eb1 + b45071f commit 6399704
Show file tree
Hide file tree
Showing 3 changed files with 2,793 additions and 31 deletions.
2,757 changes: 2,756 additions & 1 deletion protocol/abi/Beanstalk.json

Large diffs are not rendered by default.

66 changes: 36 additions & 30 deletions protocol/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const path = require("path");
const fs = require("fs");
const glob = require("glob");

require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-ethers");
require("hardhat-contract-sizer");
Expand All @@ -8,7 +12,6 @@ require("@openzeppelin/hardhat-upgrades");
require("dotenv").config();
require("hardhat-contract-sizer");

const fs = require("fs");
const { upgradeWithNewFacets } = require("./scripts/diamond");
const {
impersonateSigner,
Expand Down Expand Up @@ -95,47 +98,50 @@ task("sunrise", async function () {
})*/

task("diamondABI", "Generates ABI file for diamond, includes all ABIs of facets", async () => {
const basePath = "/contracts/beanstalk/";
// The path (relative to the root of `protocol` directory) where all modules sit.
const modulesDir = path.join("contracts", "beanstalk");

// The list of modules to combine into a single ABI. All facets (and facet dependencies) will be aggregated.
const modules = ["barn", "diamond", "farm", "field", "market", "silo", "sun"];

// The glob returns the full file path like this:
// contracts/beanstalk/barn/UnripeFacet.sol
// We want the "UnripeFacet" part.
const getFacetName = (file) => {
return file.split("/").pop().split(".")[0];
};

// Load files across all modules
const paths = [];
modules.forEach((m) => {
const filesInModule = fs.readdirSync(`.${basePath}${m}`);
paths.push(...filesInModule.map((f) => [m, f]));
modules.forEach((module) => {
const filesInModule = fs.readdirSync(path.join(".", modulesDir, module));
paths.push(...filesInModule.map((f) => [module, f]));
});

// Build ABI
let abi = [];
for (var [module, file] of paths) {
// We're only interested in facets
if (file.includes("Facet")) {
let jsonFile;
modules.forEach((module) => {
const pattern = path.join(".", modulesDir, module, "**", "*Facet.sol");
const files = glob.sync(pattern);

// A Facet can be packaged in two formats:
// 1. XYZFacet.sol
// 2. XYZFacet/XYZFacet.sol
// By convention, a folder ending with "Facet" will also contain a .sol file with the same name.
if (!file.includes(".sol")) {
// This is a directory
jsonFile = `${file}.json`;
file = `${file}/${file}.sol`;
} else {
// This is a file
jsonFile = file.replace("sol", "json");
}
files.forEach((file) => {
const facetName = getFacetName(file);
const jsonFileName = `${facetName}.json`;
const jsonFileLoc = path.join(".", "artifacts", file, jsonFileName);

const json = JSON.parse(fs.readFileSync(jsonFileLoc));

const loc = `./artifacts${basePath}${module}/${file}/${jsonFile}`;
console.log(`ADD: `, module, file, "=>", loc);
// Log what's being included
console.log(`${module}:`.padEnd(10), file);
json.abi.forEach((item) => console.log(``.padEnd(10), item.type, item.name));
console.log("");

const json = JSON.parse(fs.readFileSync(loc));
abi.push(...json.abi);
} else {
console.log(`SKIP: `, module, file);
}
}
});
});

fs.writeFileSync("./abi/Beanstalk.json", JSON.stringify(abi.filter((item, pos) => abi.map((a) => a.name).indexOf(item.name) == pos)));
const names = abi.map((a) => a.name);
fs.writeFileSync("./abi/Beanstalk.json", JSON.stringify(abi.filter((item, pos) => names.indexOf(item.name) == pos)));

console.log("ABI written to abi/Beanstalk.json");
});
Expand Down Expand Up @@ -256,4 +262,4 @@ module.exports = {
sources: "./contracts",
cache: "./cache"
}
};
};
1 change: 1 addition & 0 deletions protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"eth-gas-reporter": "0.2.25",
"eth-permit": "^0.2.1",
"forge-std": "^1.1.2",
"glob": "10.3.0",
"hardhat-tracer": "^1.1.0-rc.9",
"keccak256": "^1.0.6",
"mathjs": "^11.0.1",
Expand Down

0 comments on commit 6399704

Please sign in to comment.