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:
2021-12-23 11:56:03 +00:00
```bash
yarn
```
2021-09-30 05:14:30 +00:00
* Build files:
2021-12-23 11:56:03 +00:00
```bash
yarn build
```
2021-09-16 11:36:10 +00:00
## Run
2022-11-23 12:12:25 +00:00
Follow the steps below or follow the demos:
* [Subgraph watcher ](./subgraph-demo.md )
* [Non subgraph watcher ](./non-subgraph-demo.md )
Steps:
2021-12-23 11:56:03 +00:00
* Create a `.yaml` config file in the following format for generating a watcher:
2021-09-16 11:36:10 +00:00
2021-12-23 11:56:03 +00:00
```yaml
# Example config.yaml
# Contracts to watch (required).
2021-12-28 10:26:28 +00:00
# Can pass empty array ([]) when using subgraphPath.
2021-12-23 11:56:03 +00:00
contracts:
# Contract name.
- name: Example
# Contract file path or an url.
path: ../graph-node/test/contracts/Example.sol
2023-05-10 13:19:11 +00:00
# Contract kind (should match name of dataSource in {subgraphPath}/subgraph.yaml if subgraphPath provided)
2021-12-23 11:56:03 +00:00
kind: Example1
2021-09-16 11:36:10 +00:00
2021-12-23 11:56:03 +00:00
# Output folder path (logs output using `stdout` if not provided).
outputFolder: ../test-watcher
2021-09-17 11:40:08 +00:00
2021-12-28 10:26:28 +00:00
# Code generation mode [eth_call | storage | all | none] (default: none).
mode: none
2021-09-16 11:36:10 +00:00
2021-12-23 11:56:03 +00:00
# Kind of watcher [lazy | active] (default: active).
kind: active
2021-09-27 04:43:50 +00:00
2021-12-23 11:56:03 +00:00
# Watcher server port (default: 3008).
port: 3008
2021-09-30 05:14:30 +00:00
2023-04-20 08:01:41 +00:00
# Solc version to use (optional)
2023-10-23 03:53:20 +00:00
# Use longVersion prefixed with v from the release list https://binaries.soliditylang.org/bin/list.json
2023-04-20 08:01:41 +00:00
# If not defined, uses solc version listed in dependencies
solc: v0.8.0+commit.c7dfd78e
2021-12-23 11:56:03 +00:00
# Flatten the input contract file(s) [true | false] (default: true).
flatten: true
2021-09-16 11:36:10 +00:00
2023-11-20 11:39:51 +00:00
# Config for subgraph (optional)
# Can set empty contracts array if this config is set
subgraph:
# Path to subgraph repo directory containing package.json
directory: ../graph-node/test/subgraph/example1
# Package manager that is used in subgraph repo for dependencies
packageManager: yarn
# Path to subgraph manifest/config file
configFile: ../graph-node/test/subgraph/example1/subgraph.yaml
# Networks config file path used when building subgraph (optional)
# networkFilePath:
# Network configuration to use from the networks config file (optional)
# network:
# Path to the subgraph build (optional)
# Subgraph build WASM files should be compiled using @cerc -io/graph-cli
# If this is set codegen does not use the build generated from subgraph.directory and subgraph.configFile
# buildPath: ../graph-node/test/subgraph/example1/build
2021-09-30 05:14:30 +00:00
2021-12-23 11:56:03 +00:00
# NOTE: When passed an *URL* as contract path, it is assumed that it points to an already flattened contract file.
2021-09-16 11:36:10 +00:00
```
2023-04-12 09:16:43 +00:00
* Ensure dependencies are installed in the contracts repository before generating the watcher
2021-12-23 11:56:03 +00:00
* Run the following command to generate a watcher from contract(s):
2021-09-30 05:14:30 +00:00
2021-09-27 04:43:50 +00:00
```bash
2021-12-23 11:56:03 +00:00
yarn codegen --config-file < config-file-path >
2021-09-27 04:43:50 +00:00
```
2021-12-23 11:56:03 +00:00
* `config-file` (alias: `c` ): Watcher generation config file path (yaml) (required).
2021-09-16 11:36:10 +00:00
2023-04-20 08:01:41 +00:00
* `continue-on-error` (alias: `e` ): To continue generation if any unhandled data type is encountered (optional).
2021-12-23 11:56:03 +00:00
Example:
2021-09-16 11:36:10 +00:00
2021-12-23 11:56:03 +00:00
* Generate code using a config file `config.yaml` :
2021-09-27 12:33:04 +00:00
2021-12-23 11:56:03 +00:00
```bash
yarn codegen --config-file ./config.yaml
```
2021-12-21 08:20:22 +00:00
2023-04-20 08:01:41 +00:00
* Generate code ignoring any unhandled data types:
```bash
yarn codegen --config-file ./config.yaml --continue-on-error
```
2021-12-23 11:56:03 +00:00
This will create a folder containing the generated code at the path provided in config. Follow the steps in [Run Generated Watcher ](#run-generated-watcher ) to setup and run the generated watcher.
2021-12-21 08:20:22 +00:00
2024-06-11 09:12:54 +00:00
* Update generated watcher's `package.json` with desired `version` , `description` , `repository` URL, etc.
* Update generated watcher's config (`environments/local.toml`) as required
2024-06-13 11:11:49 +00:00
* Update generated codegen config (`codegen-config.yml`) to remove / replace your system's absolute paths
2022-11-29 09:22:39 +00:00
## Development
* `lint`
Command to check lint issues in files
```bash
yarn lint
```
To fix lint issue
```bash
yarn lint --fix
```
* `version:set`
Command to set cerc-io package versions in package.json template
```bash
yarn version:set < VERSION >
```
Example
```bash
yarn version:set 0.2.17
```
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
```
2024-06-13 11:11:49 +00:00
* Run build:
```bash
yarn build
```
2021-11-12 12:39:03 +00:00
* In the config file (`environments/local.toml`):
* Update the state checkpoint settings.
2021-09-30 05:14:30 +00:00
2021-11-12 12:39:03 +00:00
* Create the databases configured in `environments/local.toml` .
2021-10-29 11:33:22 +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-26 11:40:03 +00:00
* While using the indexer storage methods for indexing, pass `diff` as true if default state is desired to be generated using the state variables being indexed.
2021-10-20 12:25:54 +00:00
* Generating state:
2022-10-19 09:54:14 +00:00
* Edit the custom hook function `createInitialState` (triggered if the watcher passes the start block, checkpoint: `true` ) in `src/hooks.ts` to save an initial `State` using the `Indexer` object.
2021-10-12 10:32:56 +00:00
2022-10-19 09:54:14 +00:00
* Edit the custom hook function `createStateDiff` (triggered on a block) in `src/hooks.ts` to save the state in a `diff` `State` using the `Indexer` object. The default state (if exists) is updated.
2021-10-29 11:33:22 +00:00
2022-10-19 09:54:14 +00:00
* Edit the custom hook function `createStateCheckpoint` (triggered just before default and CLI checkpoint) in `src/hooks.ts` to save the state in a `checkpoint` `State` using the `Indexer` object.
2021-10-14 10:38:45 +00:00
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-12-28 10:26:28 +00:00
2021-10-26 11:40:03 +00:00
* To reset the watcher to a previous block number:
* Reset state:
```bash
yarn reset state --block-number < previous-block-number >
```
* Reset job-queue:
```bash
2022-11-28 08:44:02 +00:00
yarn reset job-queue
2021-10-26 11:40:03 +00:00
```
2021-09-17 11:40:08 +00:00
2021-10-29 11:33:22 +00:00
* To export the watcher state:
```bash
yarn export-state --export-file [export-file-path]
```
* To import the watcher state:
```bash
yarn import-state --import-file < import-file-path >
```
2021-12-28 10:26:28 +00:00
2021-10-29 11:33:22 +00:00
* To inspect a CID:
```bash
yarn inspect-cid --cid < cid >
```
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` .