Skip to content

Commit

Permalink
Add watcher generation mode none (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 authored Dec 23, 2021
1 parent 95a95c6 commit 493e91e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
6 changes: 3 additions & 3 deletions packages/codegen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Run the following command to generate a watcher from a contract file:

```bash
yarn codegen --input-files <input-file-paths> --contract-names <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 <input-file-paths> --contract-names <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).
Expand Down Expand Up @@ -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
Expand Down
36 changes: 21 additions & 15 deletions packages/codegen/src/generate-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -58,7 +58,7 @@ const main = async (): Promise<void> => {
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',
Expand Down Expand Up @@ -113,26 +113,32 @@ const main = async (): Promise<void> => {
};

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);

// 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
});
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/codegen/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit 493e91e

Please sign in to comment.