Skip to content

Commit

Permalink
Support storage mode for code generator (#251)
Browse files Browse the repository at this point in the history
* Implement storage mode in code generator.

* Fix indexer template for eth_call.

* Add support for both eth_call and storage modes.

* Add eventsInRange, events file gen and default entites gen.

* Use static property to column maps in database.

* Process events query.

* Avoid adding duplicate events in indexer.

* Fix generated watcher query with bigint arguments.

Co-authored-by: nabarun <[email protected]>
Co-authored-by: prathamesh <[email protected]>
  • Loading branch information
3 people authored Sep 27, 2021
1 parent 92b7967 commit 482be8c
Show file tree
Hide file tree
Showing 21 changed files with 1,144 additions and 115 deletions.
21 changes: 14 additions & 7 deletions packages/codegen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,34 @@
* Run the following command to generate a watcher from a contract file:

```bash
yarn codegen --input-file <input-file-path> --contract-name <contract-name> --output-folder [output-folder] --mode [eth_call | storage] --flatten [true | false]
yarn codegen --input-file <input-file-path> --contract-name <contract-name> --output-folder [output-folder] --mode [eth_call | storage | all] --flatten [true | false]
```

* `input-file`(alias: `i`): Input contract file path or an URL (required).
* `contract-name`(alias: `c`): Main contract name (required).
* `output-folder`(alias: `o`): Output folder path. (logs output using `stdout` if not provided).
* `mode`(alias: `m`): Code generation mode (default: `storage`).
* `mode`(alias: `m`): Code generation mode (default: `all`).
* `flatten`(alias: `f`): Flatten the input contract file (default: `true`).

**Note**: When passed an *URL* as `input-file`, it is assumed that it points to an already flattened contract file.

Examples:


Generate code in both eth_call and storage mode.
```bash
yarn codegen --input-file ./test/examples/contracts/ERC20.sol --contract-name ERC20 --output-folder ../my-erc20-watcher --mode eth_call
yarn codegen --input-file ./test/examples/contracts/ERC20.sol --contract-name ERC20 --output-folder ../my-erc20-watcher --mode all
```

Generate code in eth_call mode using a contract provided by URL.
```bash
yarn codegen --input-file https://git.io/Jupci --contract-name ERC721 --output-folder ../my-erc721-watcher --mode eth_call
```

Generate code in storage mode.
```bash
yarn codegen --input-file ./test/examples/contracts/ERC721.sol --contract-name ERC721 --output-folder ../my-erc721-watcher --mode storage
```

## Demo

* Install required packages:
Expand All @@ -43,15 +50,15 @@
```

* Generate a watcher from a contract file:

```bash
yarn codegen --input-file ../../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol --contract-name ERC20 --output-folder ../demo-erc20-watcher --mode eth_call
```

This will create a folder called `demo-erc20-watcher` containing the generated code at the specified path. Follow the steps in `demo-erc20-watcher/README.md` to setup and run the generated watcher.

* Generate a watcher from a flattened contract file from an URL:

```bash
yarn codegen --input-file https://git.io/Jupci --contract-name ERC721 --output-folder ../demo-erc721-watcher --mode eth_call
```
Expand All @@ -65,4 +72,4 @@

## Known Issues

* Currently, `node-fetch v2.6.2` is being used to fetch from URLs as `v3.0.0` is an [ESM-only module](https://www.npmjs.com/package/node-fetch#loading-and-configuring-the-module) and `ts-node` transpiles to import it using `require`.
* Currently, `node-fetch v2.6.2` is being used to fetch from URLs as `v3.0.0` is an [ESM-only module](https://www.npmjs.com/package/node-fetch#loading-and-configuring-the-module) and `ts-node` transpiles to import it using `require`.
3 changes: 0 additions & 3 deletions packages/codegen/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import _ from 'lodash';

import { getTsForSol } from './utils/type-mappings';
import { Param } from './utils/types';
import { capitalizeHelper } from './utils/handlebar-helpers';

const TEMPLATE_FILE = './templates/database-template.handlebars';

Expand All @@ -22,8 +21,6 @@ export class Database {
constructor () {
this._queries = [];
this._templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();

Handlebars.registerHelper('capitalize', capitalizeHelper);
}

/**
Expand Down
Loading

0 comments on commit 482be8c

Please sign in to comment.