diff --git a/packages/codegen/README.md b/packages/codegen/README.md index 26f389a16..a1d21275c 100644 --- a/packages/codegen/README.md +++ b/packages/codegen/README.md @@ -21,7 +21,7 @@ * Run the following command to generate a watcher from a contract file: ```bash - yarn codegen --input-files --contract-names --output-folder [output-folder] --mode [eth_call | storage | all] --flatten [true | false] --kind [lazy | active] --port [server-port] --subgraph-path [subgraph-build-path] + yarn codegen --input-files --contract-names --output-folder [output-folder] --mode [eth_call | storage | all | none] --flatten [true | false] --kind [lazy | active] --port [server-port] --subgraph-path [subgraph-build-path] ``` * `input-files`(alias: `i`): Input contract file path(s) or URL(s) (required). @@ -63,10 +63,10 @@ This will create a folder called `demo-erc20-watcher` containing the generated code at the specified path. Follow the steps in [Run Generated Watcher](#run-generated-watcher) to setup and run the generated watcher. - Generate code for `Eden` contracts in `storage` mode, `active` kind: + Generate code for `Eden` contracts in `none` mode, `active` kind: ```bash - yarn codegen --input-files ~/vulcanize/governance/contracts/EdenNetwork.sol ~/vulcanize/governance/contracts/MerkleDistributor.sol ~/vulcanize/governance/contracts/DistributorGovernance.sol --contract-names EdenNetwork MerkleDistributor DistributorGovernance --output-folder ../demo-eden-watcher --mode storage --kind active --subgraph-path ~/vulcanize/eden-data/packages/subgraph/build + yarn codegen --input-files ~/vulcanize/governance/contracts/EdenNetwork.sol ~/vulcanize/governance/contracts/MerkleDistributor.sol ~/vulcanize/governance/contracts/DistributorGovernance.sol --contract-names EdenNetwork MerkleDistributor DistributorGovernance --output-folder ../demo-eden-watcher --mode none --kind active --subgraph-path ~/vulcanize/eden-data/packages/subgraph/build ``` ## Run Generated Watcher diff --git a/packages/codegen/src/generate-code.ts b/packages/codegen/src/generate-code.ts index 1f84330c0..bc613832c 100644 --- a/packages/codegen/src/generate-code.ts +++ b/packages/codegen/src/generate-code.ts @@ -14,7 +14,7 @@ import { flatten } from '@poanet/solidity-flattener'; import { parse, visit } from '@solidity-parser/parser'; import { KIND_ACTIVE, KIND_LAZY } from '@vulcanize/util'; -import { MODE_ETH_CALL, MODE_STORAGE, MODE_ALL } from './utils/constants'; +import { MODE_ETH_CALL, MODE_STORAGE, MODE_ALL, MODE_NONE } from './utils/constants'; import { Visitor } from './visitor'; import { exportServer } from './server'; import { exportConfig } from './config'; @@ -58,7 +58,7 @@ const main = async (): Promise => { describe: 'Code generation mode.', type: 'string', default: MODE_ALL, - choices: [MODE_ETH_CALL, MODE_STORAGE, MODE_ALL] + choices: [MODE_ETH_CALL, MODE_STORAGE, MODE_ALL, MODE_NONE] }) .option('kind', { alias: 'k', @@ -113,6 +113,20 @@ const main = async (): Promise => { }; function parseAndVisit (contractStrings: string[], visitor: Visitor, mode: string) { + const eventDefinitionVisitor = visitor.eventDefinitionVisitor.bind(visitor); + let functionDefinitionVisitor; + let stateVariableDeclarationVisitor; + + // Visit function definitions only if mode is MODE_ETH_CALL | MODE_ALL. + if ([MODE_ALL, MODE_ETH_CALL].includes(mode)) { + functionDefinitionVisitor = visitor.functionDefinitionVisitor.bind(visitor); + } + + // Visit state variable declarations only if mode is MODE_STORAGE | MODE_ALL. + if ([MODE_ALL, MODE_STORAGE].includes(mode)) { + stateVariableDeclarationVisitor = visitor.stateVariableDeclarationVisitor.bind(visitor); + } + for (const contractString of contractStrings) { // Get the abstract syntax tree for the flattened contract. const ast = parse(contractString); @@ -120,19 +134,11 @@ function parseAndVisit (contractStrings: string[], visitor: Visitor, mode: strin // Filter out library nodes. ast.children = ast.children.filter(child => !(child.type === 'ContractDefinition' && child.kind === 'library')); - if ([MODE_ALL, MODE_ETH_CALL].includes(mode)) { - visit(ast, { - FunctionDefinition: visitor.functionDefinitionVisitor.bind(visitor), - EventDefinition: visitor.eventDefinitionVisitor.bind(visitor) - }); - } - - if ([MODE_ALL, MODE_STORAGE].includes(mode)) { - visit(ast, { - StateVariableDeclaration: visitor.stateVariableDeclarationVisitor.bind(visitor), - EventDefinition: visitor.eventDefinitionVisitor.bind(visitor) - }); - } + visit(ast, { + FunctionDefinition: functionDefinitionVisitor, + StateVariableDeclaration: stateVariableDeclarationVisitor, + EventDefinition: eventDefinitionVisitor + }); } } diff --git a/packages/codegen/src/utils/constants.ts b/packages/codegen/src/utils/constants.ts index 7f2fda28e..0f63640ab 100644 --- a/packages/codegen/src/utils/constants.ts +++ b/packages/codegen/src/utils/constants.ts @@ -5,3 +5,4 @@ export const MODE_ETH_CALL = 'eth_call'; export const MODE_STORAGE = 'storage'; export const MODE_ALL = 'all'; +export const MODE_NONE = 'none';