mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-01-08 12:28:05 +00:00
Add watcher generation mode none (#92)
This commit is contained in:
parent
8b5d408f77
commit
44e18fee48
@ -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).
|
||||
@ -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
|
||||
|
@ -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<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',
|
||||
@ -113,6 +113,20 @@ 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);
|
||||
@ -120,20 +134,12 @@ 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)
|
||||
FunctionDefinition: functionDefinitionVisitor,
|
||||
StateVariableDeclaration: stateVariableDeclarationVisitor,
|
||||
EventDefinition: eventDefinitionVisitor
|
||||
});
|
||||
}
|
||||
|
||||
if ([MODE_ALL, MODE_STORAGE].includes(mode)) {
|
||||
visit(ast, {
|
||||
StateVariableDeclaration: visitor.stateVariableDeclarationVisitor.bind(visitor),
|
||||
EventDefinition: visitor.eventDefinitionVisitor.bind(visitor)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function generateWatcher (contractStrings: string[], visitor: Visitor, argv: any) {
|
||||
|
@ -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';
|
||||
|
Loading…
Reference in New Issue
Block a user