2021-09-16 11:36:10 +00:00
# Code Generator
## Setup
2021-09-30 05:14:30 +00:00
* In root of the repository:
2021-09-16 11:36:10 +00:00
2021-09-30 05:14:30 +00:00
* Install required packages:
```bash
yarn
```
* Build files:
```bash
yarn build
```
2021-09-16 11:36:10 +00:00
## Run
2021-09-23 11:25:46 +00:00
* Run the following command to generate a watcher from a contract file:
2021-09-16 11:36:10 +00:00
```bash
2021-10-22 09:17:56 +00:00
yarn codegen --input-file < input-file-path > --contract-name < contract-name > --output-folder [output-folder] --mode [eth_call | storage | all] --flatten [true | false] --kind [lazy | active] --port [server-port]
2021-09-16 11:36:10 +00:00
```
2021-09-17 11:40:08 +00:00
* `input-file` (alias: `i` ): Input contract file path or an URL (required).
2021-09-23 11:25:46 +00:00
* `contract-name` (alias: `c` ): Main contract name (required).
* `output-folder` (alias: `o` ): Output folder path. (logs output using `stdout` if not provided).
2021-09-27 04:43:50 +00:00
* `mode` (alias: `m` ): Code generation mode (default: `all` ).
2021-09-17 11:40:08 +00:00
* `flatten` (alias: `f` ): Flatten the input contract file (default: `true` ).
2021-10-20 12:25:54 +00:00
* `kind` (alias: `k` ): Kind of watcher (default: `active` ).
2021-10-22 09:17:56 +00:00
* `port` (alias: `p` ): Server port (default: `3008` ).
2021-09-17 11:40:08 +00:00
**Note** : When passed an *URL* as `input-file` , it is assumed that it points to an already flattened contract file.
2021-09-16 11:36:10 +00:00
Examples:
2021-09-27 04:43:50 +00:00
2021-10-20 12:25:54 +00:00
Generate code in `storage` mode, `lazy` kind.
2021-09-30 05:14:30 +00:00
2021-09-16 11:36:10 +00:00
```bash
2021-10-20 12:25:54 +00:00
yarn codegen --input-file ./test/examples/contracts/ERC721.sol --contract-name ERC721 --output-folder ../my-erc721-watcher --mode storage --kind lazy
2021-09-16 11:36:10 +00:00
```
2021-09-28 04:52:21 +00:00
Generate code in `eth_call` mode using a contract provided by an URL.
2021-09-30 05:14:30 +00:00
2021-09-16 11:36:10 +00:00
```bash
2021-09-23 11:25:46 +00:00
yarn codegen --input-file https://git.io/Jupci --contract-name ERC721 --output-folder ../my-erc721-watcher --mode eth_call
2021-09-16 11:36:10 +00:00
```
2021-10-20 12:25:54 +00:00
Generate code for `ERC721` in both `eth_call` and `storage` mode, `active` kind.
2021-09-30 05:14:30 +00:00
2021-09-27 04:43:50 +00:00
```bash
2021-10-20 12:25:54 +00:00
yarn codegen --input-file ../../node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol --contract-name ERC721 --output-folder ../demo-erc721-watcher --mode all --kind active
2021-09-27 04:43:50 +00:00
```
2021-10-12 10:32:56 +00:00
Generate code for `ERC20` contract in both `eth_call` and `storage` mode, `active` kind:
2021-09-16 11:36:10 +00:00
2021-09-30 05:14:30 +00:00
```bash
2021-10-12 10:32:56 +00:00
yarn codegen --input-file ../../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol --contract-name ERC20 --output-folder ../demo-erc20-watcher --mode all --kind active
2021-09-30 05:14:30 +00:00
```
2021-09-16 11:36:10 +00:00
2021-10-12 10:32:56 +00:00
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.
2021-09-27 12:33:04 +00:00
2021-09-30 05:14:30 +00:00
## Run Generated Watcher
2021-09-27 12:33:04 +00:00
2021-09-30 05:14:30 +00:00
### Setup
2021-09-16 11:36:10 +00:00
2021-09-30 05:14:30 +00:00
* Run the following command to install required packages:
2021-09-27 04:43:50 +00:00
2021-09-16 11:36:10 +00:00
```bash
2021-09-30 05:14:30 +00:00
yarn
2021-09-16 11:36:10 +00:00
```
2021-09-30 05:14:30 +00:00
* Create the databases configured in `environments/local.toml` .
2021-10-20 12:25:54 +00:00
* Update the state checkpoint settings in `environments/local.toml` .
2021-10-14 10:38:45 +00:00
2021-09-30 05:14:30 +00:00
### Customize
* Indexing on an event:
* Edit the custom hook function `handleEvent` (triggered on an event) in `src/hooks.ts` to perform corresponding indexing using the `Indexer` object.
2021-09-23 11:25:46 +00:00
2021-10-20 12:25:54 +00:00
* While using the indexer storage methods for indexing, pass the optional arg. `state` as `diff` or `checkpoint` if default state is desired to be generated using the state variables being indexed else pass `none` .
* Generating state:
* Edit the custom hook function `createInitialCheckpoint` (triggered on watch-contract, checkpoint: `true` ) in `src/hooks.ts` to save an initial checkpoint `IPLDBlock` using the `Indexer` object.
2021-10-12 10:32:56 +00:00
2021-10-20 12:25:54 +00:00
* Edit the custom hook function `createStateDiff` (triggered on a block) in `src/hooks.ts` to save the state in an `IPLDBlock` using the `Indexer` object. The default state (if exists) is updated.
2021-10-14 10:38:45 +00:00
2021-10-20 12:25:54 +00:00
* The existing example hooks in `src/hooks.ts` are for an `ERC20` contract.
2021-09-30 05:14:30 +00:00
### Run
* Run lint:
```bash
yarn lint
```
* Run the watcher:
2021-09-27 04:43:50 +00:00
2021-09-16 11:36:10 +00:00
```bash
2021-09-30 05:14:30 +00:00
yarn server
2021-09-16 11:36:10 +00:00
```
2021-09-30 05:14:30 +00:00
* If the watcher is an `active` watcher:
* Run the job-runner:
```bash
yarn job-runner
```
* To watch a contract:
```bash
2021-10-20 12:25:54 +00:00
yarn watch:contract --address < contract-address > --kind < contract-kind > --checkpoint < true | false > --starting-block [block-number]
2021-09-30 05:14:30 +00:00
```
* To fill a block range:
2021-09-16 11:36:10 +00:00
2021-09-30 05:14:30 +00:00
```bash
2021-10-14 10:38:45 +00:00
yarn fill --start-block < from-block > --end-block < to-block >
```
* To create a checkpoint for a contract:
```bash
yarn checkpoint --address < contract-address > --block-hash [block-hash]
2021-09-30 05:14:30 +00:00
```
2021-09-17 11:40:08 +00:00
## Known Issues
2021-09-27 04:43:50 +00:00
* 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` .