watcher-ts/packages/codegen
Ashwin Phatak f9934675b2
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>
2021-09-16 17:06:10 +05:30
..
src CLI to flatten and generate GQL schema from Solidity file (#245) 2021-09-16 17:06:10 +05:30
.eslintignore CLI to flatten and generate GQL schema from Solidity file (#245) 2021-09-16 17:06:10 +05:30
.eslintrc.json CLI to flatten and generate GQL schema from Solidity file (#245) 2021-09-16 17:06:10 +05:30
package.json CLI to flatten and generate GQL schema from Solidity file (#245) 2021-09-16 17:06:10 +05:30
README.md CLI to flatten and generate GQL schema from Solidity file (#245) 2021-09-16 17:06:10 +05:30
tsconfig.json CLI to flatten and generate GQL schema from Solidity file (#245) 2021-09-16 17:06:10 +05:30

Code Generator

Setup

  • Run the following command to install required packages:

    yarn
    

Run

  • Run the following command to generate a flattened contract file from a contract file:

    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:

    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:

    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:

    yarn codegen:gql --input-file ./test/examples/contracts/ERC20-flat.sol --output-file ./ERC20-schema.gql --mode eth_call
    
    yarn codegen:gql --input-file https://git.io/Jupci --output-file ./ERC721-schema.gql --mode storage
    

Demo

  • Install required packages:

    yarn
    
  • Flatten a contract file:

    # 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:

    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:

    yarn codegen:gql --input-file https://git.io/Jupci --output-file ./ERC721-schema.gql --mode eth_call
    

References