Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions packages/cli/src/commands/gen-deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { Arguments, CommandBuilder } from "yargs";
import { spawnSync } from "node:child_process";
import fs from 'fs';
import path from 'path';
import chalk from "chalk";

import {readdirSync} from 'fs';
type Options = {
components: string;
systems: string;
outdir: string;
};

export const command = "gen-deploy";
export const desc =
"generate deploy.json from smart contracts";

export const builder: CommandBuilder<Options, Options> = (yargs) =>
yargs.options({
components: { type: "string", default: "./src/components", desc: "path to components dir" },
systems: { type: "string", default: "./src/systems", desc: "path to systems dir" },
outdir: { type: "string", default: "./deploy.json",desc: "output path" },
});

export const handler = async (args: Arguments<Options>): Promise<void> => {
const depployJson:any= {};
const { components, systems, outdir } = args;
console.log("procesing...");
const comp = readdirSync(components);

const comps = comp.map(function(e) {
e = e.replace('.sol','');
return e;
});
depployJson.components = comps

const sys = readdirSync(systems).filter(c => c.includes(".sol"));
const syss = sys.map(function(e) {
const route = path.resolve(systems, e);
// give write access to any components that each system touch. even if it not actaully write
const out = spawnSync("forge",["flatten" ,route]);
const flatCode = out.stdout.toString()
const used = comps.filter(c => flatCode.includes(c))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw this would include components that the system doesn't actually write to, not that I have a better solution in mind

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regex the set function?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regex the set function?

That was my 1st thought actually, but set is called on the variable, which can have any name. And even if you account for that, a library may have some func which uses set, but the system doesnt use that func.
I've been trying some stuff with solidity-parser, but it's not rly gonna help here (imports make things very complicated)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can think of 2 ways.

  • try find calls in systems that hit any non-view/pure function on components. (might be hard tho)
  • add warning that this function might add more write access than actually required. user might wanna remove some manually

@dk1a dk1a Dec 14, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • For 1 you'd need to write some complicated parsing, imo not worth it for this little utility
  • I like 2, would prefer it for my project compared to filling writeAccess manually from scratch

Another option I could suggest: parse e.g. @custom:ecsc TestComponent; but neither forge inspect, nor solidity-parser can do that. So I guess you'd need solc's devdoc json output. Seems complicated, but still way easier than writing a function parser.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added warning msg :)

return {
"name": e.replace(".sol",""),
"writeAccess": used
};
});
depployJson.systems = syss

const deployJsonStr = JSON.stringify(depployJson, undefined, 4);
fs.writeFileSync(outdir, deployJsonStr, 'utf8');
console.log("write output to", outdir)
console.warn(chalk.yellow.bold("Warning: this function may grant more write access than necessary. It is recommended to manually remove any unnecessary permissions."))
process.exit(0);
};