CLI to flatten and generate GQL schema from Solidity file (#245)

* Add schema generation.

* Use yargs and add a script to generate schema.

* Add command to generate schema to README.md

* Remove use of arrow functions in visitor.

* Add function to export schema in visitor.

* Create Event union while adding to it.

* Add bool to typemappings.

* Add method descriptions and remove output type mappings.

* Add storage mode.

* Add URL support and a flattening script.

* Fix parameter for flattening in README.md.

* Add setup and references to README.md.

Co-authored-by: prathamesh <prathamesh.musale0@gmail.com>
This commit is contained in:
Ashwin Phatak
2021-09-16 17:06:10 +05:30
committed by GitHub
co-authored by prathamesh
parent a9dc34f704
commit f9934675b2
9 changed files with 1080 additions and 7 deletions
+2
View File
@@ -0,0 +1,2 @@
# Don't lint node_modules.
node_modules
+27
View File
@@ -0,0 +1,27 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"semistandard",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/explicit-module-boundary-types": [
"warn",
{
"allowArgumentsExplicitlyTypedAsAny": true
}
]
}
}
+83
View File
@@ -0,0 +1,83 @@
# Code Generator
## Setup
* Run the following command to install required packages:
```bash
yarn
```
## Run
* Run the following command to generate a flattened contract file from a contract file:
```bash
yarn codegen:flatten <input-file-path> [output-dir]
```
* `input-file-path`: Input contract file path (absolute) (required). Note: Currently, relative path doesn't work.
* `output-dir`: Directory to store the flattened contract output file (default: `./out`).
Example:
```bash
yarn codegen:flatten ~/watcher-ts/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol ./flattened
```
This will generate file `ERC20_flat.sol` in `./flattened`.
* Run the following command to generate schema from a contract file:
```bash
yarn codegen:gql --input-file <input-file-path> --output-file [output-file-path] --mode [eth_call | storage]
```
* `input-file`: Input contract (must be a flattened contract) file path or an URL (required).
* `output-file`: Schema output file path (logs output using `stdout` if not provided).
* `mode`: Contract variables access mode (default: `storage`).
Examples:
```bash
yarn codegen:gql --input-file ./test/examples/contracts/ERC20-flat.sol --output-file ./ERC20-schema.gql --mode eth_call
```
```bash
yarn codegen:gql --input-file https://git.io/Jupci --output-file ./ERC721-schema.gql --mode storage
```
## Demo
* Install required packages:
```bash
yarn
```
* Flatten a contract file:
```bash
# Note: Currently, relative path for input-file-path doesn't work. Use absolute path.
yarn codegen:flatten ~/watcher-ts/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol ./flattened
```
* Generate schema from the flattened contract file:
```bash
yarn codegen:gql --input-file ./flattened/ERC20_flat.sol --output-file ./ERC20-schema.gql --mode storage
```
* Generate schema from the flattened contract file from an URL:
```bash
yarn codegen:gql --input-file https://git.io/Jupci --output-file ./ERC721-schema.gql --mode eth_call
```
## References
* [ERC20 schema generation (eth_call mode).](https://git.io/JuhN2)
* [ERC20 schema generation (storage mode).](https://git.io/JuhNr)
* [ERC721 schema generation (eth_call mode).](https://git.io/JuhNK)
* [ERC721 schema generation (storage mode).](https://git.io/JuhN1)
+44
View File
@@ -0,0 +1,44 @@
{
"name": "@vulcanize/codegen",
"version": "0.1.0",
"description": "Code generator",
"private": true,
"main": "index.js",
"scripts": {
"lint": "eslint .",
"codegen:gql": "ts-node src/generate-schema.ts",
"codegen:flatten": "poa-solidity-flattener"
},
"repository": {
"type": "git",
"url": "git+https://github.com/vulcanize/watcher-ts.git"
},
"author": "",
"license": "AGPL-3.0",
"bugs": {
"url": "https://github.com/vulcanize/watcher-ts/issues"
},
"homepage": "https://github.com/vulcanize/watcher-ts#readme",
"dependencies": {
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git#pm-make-compatible",
"@solidity-parser/parser": "^0.13.2",
"graphql": "^15.5.0",
"graphql-compose": "^9.0.3",
"node-fetch": "^2",
"ts-node": "^10.2.1",
"typescript": "^4.4.2",
"yargs": "^17.1.1"
},
"devDependencies": {
"@openzeppelin/contracts": "^4.3.2",
"@types/node": "^16.9.0",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
"eslint": "^7.32.0",
"eslint-config-semistandard": "^16.0.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0"
}
}
+72
View File
@@ -0,0 +1,72 @@
//
// Copyright 2021 Vulcanize, Inc.
//
import { readFileSync, createWriteStream } from 'fs';
import fetch from 'node-fetch';
import path from 'path';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { parse, visit } from '@solidity-parser/parser';
import { Visitor } from './visitor';
const MODE_ETH_CALL = 'eth_call';
const MODE_STORAGE = 'storage';
const main = async (): Promise<void> => {
const argv = await yargs(hideBin(process.argv))
.option('input-file', {
alias: 'i',
demandOption: true,
describe: 'Input contract file path or an url.',
type: 'string'
})
.option('output-file', {
alias: 'o',
describe: 'Schema output file path.',
type: 'string'
})
.option('mode', {
alias: 'm',
type: 'string',
default: MODE_STORAGE,
choices: [MODE_ETH_CALL, MODE_STORAGE]
})
.argv;
let data: string;
if (argv['input-file'].startsWith('http')) {
const response = await fetch(argv['input-file']);
data = await response.text();
} else {
data = readFileSync(path.resolve(argv['input-file'])).toString();
}
const ast = parse(data);
// Filter out library nodes.
ast.children = ast.children.filter(child => !(child.type === 'ContractDefinition' && child.kind === 'library'));
const visitor = new Visitor();
if (argv.mode === MODE_ETH_CALL) {
visit(ast, {
FunctionDefinition: visitor.functionDefinitionVisitor.bind(visitor),
EventDefinition: visitor.eventDefinitionVisitor.bind(visitor)
});
} else {
visit(ast, {
StateVariableDeclaration: visitor.stateVariableDeclarationVisitor.bind(visitor),
EventDefinition: visitor.eventDefinitionVisitor.bind(visitor)
});
}
const outStream = argv['output-file'] ? createWriteStream(path.resolve(argv['output-file'])) : process.stdout;
visitor.exportSchema(outStream);
};
main().catch(err => {
console.error(err);
});
+228
View File
@@ -0,0 +1,228 @@
//
// Copyright 2021 Vulcanize, Inc.
//
import { GraphQLSchema, printSchema } from 'graphql';
import { SchemaComposer } from 'graphql-compose';
import { Writable } from 'stream';
export interface Param {
name: string;
type: string;
}
export class Schema {
_composer: SchemaComposer;
_typeMapping: Map<string, string>;
_events: Array<string>;
constructor () {
this._composer = new SchemaComposer();
this._typeMapping = new Map();
this._events = [];
this._addBasicTypes();
}
/**
* Adds a query to the schema with the given parameters.
* @param name Name of the query.
* @param params Parameters to the query.
* @param returnType Return type for the query.
*/
addQuery (name: string, params: Array<Param>, returnType: string): void {
// TODO: Handle cases where returnType/params type is an array.
const queryObject: { [key: string]: any; } = {};
queryObject[name] = {
// Get type composer object for return type from the schema composer.
type: this._composer.getOTC(`Result${this._typeMapping.get(returnType)}`).NonNull,
args: {
blockHash: 'String!',
contractAddress: 'String!'
}
};
if (params.length > 0) {
queryObject[name].args = params.reduce((acc, curr) => {
acc[curr.name] = this._typeMapping.get(curr.type) + '!';
return acc;
}, queryObject[name].args);
}
// Add a query to the schema composer using queryObject.
this._composer.Query.addFields(queryObject);
}
/**
* Adds a type to the schema for an event.
* @param name Event name.
* @param params Event parameters.
*/
addEventType (name: string, params: Array<Param>): void {
name = `${name}Event`;
const typeObject: any = {};
typeObject.name = name;
typeObject.fields = {};
if (params.length > 0) {
typeObject.fields = params.reduce((acc, curr) => {
acc[curr.name] = this._typeMapping.get(curr.type) + '!';
return acc;
}, typeObject.fields);
}
// Create a type composer to add the required type in the schema composer.
this._composer.createObjectTC(typeObject);
this._events.push(name);
this._addToEventUnion(name);
if (this._events.length === 1) {
this._addEventsRelatedTypes();
this._addEventsQuery();
this._addEventSubscription();
}
}
/**
* Builds the schema from the schema composer.
* @returns GraphQLSchema object.
*/
buildSchema (): GraphQLSchema {
return this._composer.buildSchema();
}
/**
* Writes schema to a stream.
* @param outStream A writable output stream to write the schema to.
*/
exportSchema (outStream: Writable): void {
// Get schema as a string from GraphQLSchema.
const schema = printSchema(this.buildSchema());
outStream.write(schema);
}
/**
* Adds basic types to the schema and typemapping.
*/
_addBasicTypes (): void {
// Create a scalar type composer to add the scalar BigInt in the schema composer.
this._composer.createScalarTC({
name: 'BigInt'
});
// Create a type composer to add the type Proof in the schema composer.
this._composer.createObjectTC({
name: 'Proof',
fields: {
data: 'String!'
}
});
this._composer.createObjectTC({
name: 'ResultBoolean',
fields: {
value: 'Boolean!',
proof: () => this._composer.getOTC('Proof')
}
});
this._composer.createObjectTC({
name: 'ResultString',
fields: {
value: 'String!',
proof: () => this._composer.getOTC('Proof')
}
});
this._composer.createObjectTC({
name: 'ResultInt',
fields: {
value: () => 'Int!',
proof: () => this._composer.getOTC('Proof')
}
});
this._composer.createObjectTC({
name: 'ResultBigInt',
fields: {
// Get type composer object for BigInt scalar from the schema composer.
value: () => this._composer.getSTC('BigInt').NonNull,
proof: () => this._composer.getOTC('Proof')
}
});
// TODO Get typemapping from ethersjs.
this._typeMapping.set('string', 'String');
this._typeMapping.set('uint8', 'Int');
this._typeMapping.set('uint256', 'BigInt');
this._typeMapping.set('address', 'String');
this._typeMapping.set('bool', 'Boolean');
this._typeMapping.set('bytes4', 'String');
}
/**
* Adds types 'ResultEvent' and 'WatchedEvent' to the schema.
*/
_addEventsRelatedTypes (): void {
// Create the ResultEvent type.
const resultEventName = 'ResultEvent';
this._composer.createObjectTC({
name: resultEventName,
fields: {
// Get type composer object for Event union from the schema composer.
event: () => this._composer.getUTC('Event').NonNull,
proof: () => this._composer.getOTC('Proof')
}
});
// Create the WatchedEvent type.
const watchedEventName = 'WatchedEvent';
this._composer.createObjectTC({
name: watchedEventName,
fields: {
blockHash: 'String!',
contractAddress: 'String!',
event: () => this._composer.getOTC(resultEventName).NonNull
}
});
}
/**
* Adds a query for events to the schema.
*/
_addEventsQuery (): void {
this._composer.Query.addFields({
events: {
type: [this._composer.getOTC('ResultEvent').NonNull],
args: {
blockHash: 'String!',
contractAddress: 'String!',
name: 'String'
}
}
});
}
/**
* Adds an event subscription to the schema.
*/
_addEventSubscription (): void {
// Add a subscription to the schema composer.
this._composer.Subscription.addFields({
onEvent: () => this._composer.getOTC('WatchedEvent').NonNull
});
}
/**
* Adds an 'Event' union (if doesn't exist) to the schema. Adds the specified event to the 'Event' union.
* @param event Event type name to add to the union.
*/
_addToEventUnion (event: string): void {
// Get (or create if doesn't exist) type composer object for Event union from the schema composer.
const eventUnion = this._composer.getOrCreateUTC('Event');
// Add a new type to the union.
eventUnion.addType(this._composer.getOTC(event));
}
}
+81
View File
@@ -0,0 +1,81 @@
//
// Copyright 2021 Vulcanize, Inc.
//
import { Writable } from 'stream';
import { Schema, Param } from './schema';
export class Visitor {
_schema: Schema;
constructor () {
this._schema = new Schema();
}
/**
* Visitor function for function definitions.
* @param node ASTNode for a function definition.
*/
functionDefinitionVisitor (node: any): void {
if (node.stateMutability === 'view' && (node.visibility === 'external' || node.visibility === 'public')) {
const name = node.name;
const params = node.parameters.map((item: any) => {
return { name: item.name, type: item.typeName.name };
});
// TODO Handle multiple return parameters and array return type.
const returnType = node.returnParameters[0].typeName.name;
this._schema.addQuery(name, params, returnType);
}
}
/**
* Visitor function for state variable declarations.
* @param node ASTNode for a state variable declaration.
*/
stateVariableDeclarationVisitor (node: any): void {
// TODO Handle multiples variables in a single line.
// TODO Handle array types.
let name: string = node.variables[0].name;
name = name.startsWith('_') ? name.substring(1) : name;
const params: Param[] = [];
let typeName = node.variables[0].typeName;
let numParams = 0;
// If the variable type is mapping, extract key as a param:
// Eg. mapping(address => mapping(address => uint256)) private _allowances;
while (typeName.type === 'Mapping') {
params.push({ name: `key${numParams.toString()}`, type: typeName.keyType.name });
typeName = typeName.valueType;
numParams++;
}
const returnType = typeName.name;
this._schema.addQuery(name, params, returnType);
}
/**
* Visitor function for event definitions.
* @param node ASTNode for an event definition.
*/
eventDefinitionVisitor (node: any): void {
const name = node.name;
const params = node.parameters.map((item: any) => {
return { name: item.name, type: item.typeName.name };
});
this._schema.addEventType(name, params);
}
/**
* Writes schema to a stream.
* @param outStream A writable output stream to write the schema to.
*/
exportSchema (outStream: Writable): void {
this._schema.exportSchema(outStream);
}
}
+73
View File
@@ -0,0 +1,73 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": ["src/**/*"]
}