mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-08-02 04:32:06 +00:00
Create queries for only inherited contract methods in codegen generated watchers (#371)
* Codegen create queries only for inherited contracts * Refactor recursive method to filter inherited contracts * Comment code and refactor --------- Co-authored-by: Dhruv Srivastava <dhruvdhs.ds@gmail.com>
This commit is contained in:
parent
ca844dbe47
commit
26c1607663
@ -14,6 +14,7 @@ import os from 'os';
|
|||||||
|
|
||||||
import { flatten } from '@poanet/solidity-flattener';
|
import { flatten } from '@poanet/solidity-flattener';
|
||||||
import { parse, visit } from '@solidity-parser/parser';
|
import { parse, visit } from '@solidity-parser/parser';
|
||||||
|
import { ASTNode } from '@solidity-parser/parser/dist/src/ast-types';
|
||||||
import { KIND_ACTIVE, KIND_LAZY } from '@cerc-io/util';
|
import { KIND_ACTIVE, KIND_LAZY } from '@cerc-io/util';
|
||||||
|
|
||||||
import { MODE_ETH_CALL, MODE_STORAGE, MODE_ALL, MODE_NONE, DEFAULT_PORT } from './utils/constants';
|
import { MODE_ETH_CALL, MODE_STORAGE, MODE_ALL, MODE_NONE, DEFAULT_PORT } from './utils/constants';
|
||||||
@ -38,7 +39,7 @@ import { getSubgraphConfig } from './utils/subgraph';
|
|||||||
import { exportIndexBlock } from './index-block';
|
import { exportIndexBlock } from './index-block';
|
||||||
import { exportSubscriber } from './subscriber';
|
import { exportSubscriber } from './subscriber';
|
||||||
import { exportReset } from './reset';
|
import { exportReset } from './reset';
|
||||||
import { writeFileToStream } from './utils/helpers';
|
import { filterInheritedContractNodes, writeFileToStream } from './utils/helpers';
|
||||||
|
|
||||||
const ASSET_DIR = path.resolve(__dirname, 'assets');
|
const ASSET_DIR = path.resolve(__dirname, 'assets');
|
||||||
|
|
||||||
@ -146,8 +147,14 @@ function parseAndVisit (visitor: Visitor, contracts: any[], mode: string) {
|
|||||||
// Get the abstract syntax tree for the flattened contract.
|
// Get the abstract syntax tree for the flattened contract.
|
||||||
const ast = parse(contract.contractString);
|
const ast = parse(contract.contractString);
|
||||||
|
|
||||||
// Filter out library nodes.
|
const contractNode = ast.children.find((node: ASTNode) =>
|
||||||
ast.children = ast.children.filter(child => !(child.type === 'ContractDefinition' && child.kind === 'library'));
|
node.type === 'ContractDefinition' &&
|
||||||
|
node.name === contract.contractName
|
||||||
|
);
|
||||||
|
|
||||||
|
assert(contractNode);
|
||||||
|
const nodes = filterInheritedContractNodes(ast, [contractNode]);
|
||||||
|
ast.children = Array.from(nodes).concat(contractNode);
|
||||||
|
|
||||||
visit(ast, {
|
visit(ast, {
|
||||||
StateVariableDeclaration: stateVariableDeclarationVisitor,
|
StateVariableDeclaration: stateVariableDeclarationVisitor,
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { Writable } from 'stream';
|
import { Writable } from 'stream';
|
||||||
import { TypeName } from '@solidity-parser/parser/dist/src/ast-types';
|
|
||||||
|
import { TypeName, ASTNode, InheritanceSpecifier, SourceUnit } from '@solidity-parser/parser/dist/src/ast-types';
|
||||||
|
|
||||||
export const isArrayType = (typeName: TypeName): boolean => (typeName.type === 'ArrayTypeName');
|
export const isArrayType = (typeName: TypeName): boolean => (typeName.type === 'ArrayTypeName');
|
||||||
|
|
||||||
@ -22,3 +23,38 @@ export function writeFileToStream (pathToFile: string, outStream: Writable): voi
|
|||||||
const fileStream = fs.createReadStream(pathToFile);
|
const fileStream = fs.createReadStream(pathToFile);
|
||||||
fileStream.pipe(outStream);
|
fileStream.pipe(outStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get inherited contracts for array of contractNodes
|
||||||
|
* @param ast
|
||||||
|
* @param contractNodes
|
||||||
|
*/
|
||||||
|
export function filterInheritedContractNodes (ast: SourceUnit, contractNodes: ASTNode[]): Set<ASTNode> {
|
||||||
|
const resultSet: Set<ASTNode> = new Set();
|
||||||
|
|
||||||
|
contractNodes.forEach((node: ASTNode) => {
|
||||||
|
if (node.type !== 'ContractDefinition') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter out library nodes
|
||||||
|
if (node.kind === 'library') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const inheritedContracts = ast.children.filter((childNode: ASTNode) =>
|
||||||
|
node.baseContracts.some((baseContract: InheritanceSpecifier) =>
|
||||||
|
childNode.type === 'ContractDefinition' && baseContract.baseName.namePath === childNode.name
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Add inherited contracts to result set
|
||||||
|
inheritedContracts.forEach((node: ASTNode) => resultSet.add(node));
|
||||||
|
// Get parent inherited contracts
|
||||||
|
const parentInheritedNodes = filterInheritedContractNodes(ast, inheritedContracts);
|
||||||
|
// Add parent inherited contract nodes in result set
|
||||||
|
parentInheritedNodes.forEach((node: ASTNode) => resultSet.add(node));
|
||||||
|
});
|
||||||
|
|
||||||
|
return resultSet;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user