2021-10-28 11:01:56 +00:00
# Example Watcher
## Setup
* Run the following command to install required packages:
```bash
yarn
```
* Create a postgres12 database for the watcher:
```bash
sudo su - postgres
createdb graph-test-watcher
```
* If the watcher is an `active` watcher:
Create database for the job queue and enable the `pgcrypto` extension on them (https://github.com/timgit/pg-boss/blob/master/docs/usage.md#intro):
```
createdb graph-test-watcher-job-queue
```
```
postgres@tesla:~$ psql -U postgres -h localhost graph-test-watcher-job-queue
Password for user postgres:
psql (12.7 (Ubuntu 12.7-1.pgdg18.04+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.
graph-test-watcher-job-queue=# CREATE EXTENSION pgcrypto;
CREATE EXTENSION
graph-test-watcher-job-queue=# exit
```
2021-11-15 14:35:43 +00:00
* In the [config file ](./environments/local.toml ):
* Update the database connection settings.
2022-06-08 06:43:52 +00:00
* Update the `upstream` config and provide the `ipld-eth-server` GQL API endpoint.
2021-10-28 11:01:56 +00:00
2022-10-19 09:54:14 +00:00
* Update the `server` config with state checkpoint settings.
2021-10-28 11:01:56 +00:00
## Customize
* Indexing on an event:
* Edit the custom hook function `handleEvent` (triggered on an event) in [hooks.ts ](./src/hooks.ts ) to perform corresponding indexing using the `Indexer` object.
2021-11-15 14:35:43 +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.
* Generating state:
2022-11-22 09:50:44 +00:00
* Edit the custom hook function `createInitialState` (triggered if the watcher passes the start block, checkpoint: `true` ) in [hooks.ts ](./src/hooks.ts ) to save an initial `State` using the `Indexer` object.
2021-11-15 14:35:43 +00:00
2022-10-19 09:54:14 +00:00
* Edit the custom hook function `createStateDiff` (triggered on a block) in [hooks.ts ](./src/hooks.ts ) to save the state in a `diff` `State` using the `Indexer` object. The default state (if exists) is updated.
2021-11-15 14:35:43 +00:00
2022-10-19 09:54:14 +00:00
* Edit the custom hook function `createStateCheckpoint` (triggered just before default and CLI checkpoint) in [hooks.ts ](./src/hooks.ts ) to save the state in a `checkpoint` `State` using the `Indexer` object.
2021-11-15 14:35:43 +00:00
* The existing example hooks in [hooks.ts ](./src/hooks.ts ) are for an `ERC20` contract.
2021-10-28 11:01:56 +00:00
## Run
2022-11-25 06:01:20 +00:00
* Run the server:
2021-10-28 11:01:56 +00:00
2022-11-25 06:01:20 +00:00
```bash
yarn server
```
2021-10-28 11:01:56 +00:00
2022-11-25 06:01:20 +00:00
GQL console: http://localhost:3008/graphql
2021-10-28 11:01:56 +00:00
* If the watcher is an `active` watcher:
* Run the job-runner:
```bash
yarn job-runner
```
2022-11-25 06:01:20 +00:00
* Run the server:
```bash
yarn server
```
GQL console: http://localhost:3008/graphql
2021-10-28 11:01:56 +00:00
* To watch a contract:
```bash
2021-11-15 14:35:43 +00:00
yarn watch:contract --address < contract-address > --kind < contract-kind > --checkpoint < true | false > --starting-block [block-number]
```
* `address` : Address or identifier of the contract to be watched.
* `kind` : Kind of the contract.
* `checkpoint` : Turn checkpointing on (`true` | `false` ).
* `starting-block` : Starting block for the contract (default: `1` ).
Examples:
2022-05-26 14:31:31 +00:00
2021-11-15 14:35:43 +00:00
Watch a contract with its address and checkpointing on:
```bash
yarn watch:contract --address 0x1F78641644feB8b64642e833cE4AFE93DD6e7833 --kind ERC20 --checkpoint true
```
Watch a contract with its identifier and checkpointing on:
```bash
yarn watch:contract --address MyProtocol --kind protocol --checkpoint true
2021-10-28 11:01:56 +00:00
```
* To fill a block range:
```bash
2021-11-15 14:35:43 +00:00
yarn fill --start-block < from-block > --end-block < to-block >
```
* `start-block` : Block number to start filling from.
2022-05-26 14:31:31 +00:00
* `end-block` : Block number till which to fill.
2021-11-15 14:35:43 +00:00
* To create a checkpoint for a contract:
```bash
2022-09-23 10:05:15 +00:00
yarn checkpoint create --address < contract-address > --block-hash [block-hash]
2021-11-15 14:35:43 +00:00
```
* `address` : Address or identifier of the contract for which to create a checkpoint.
* `block-hash` : Hash of a block (in the pruned region) at which to create the checkpoint (default: latest canonical block hash).
2022-09-23 10:05:15 +00:00
* To verify a checkpoint:
```bash
yarn checkpoint verify --cid < checkpoint-cid >
```
`cid` : CID of the checkpoint for which to verify.
2021-11-15 14:35:43 +00:00
* To reset the watcher to a previous block number:
2022-10-19 09:54:14 +00:00
* Reset watcher:
2021-11-15 14:35:43 +00:00
```bash
2022-10-19 09:54:14 +00:00
yarn reset watcher --block-number < previous-block-number >
2021-11-15 14:35:43 +00:00
```
* Reset job-queue:
```bash
yarn reset job-queue --block-number < previous-block-number >
```
2022-05-26 14:31:31 +00:00
2022-11-22 09:50:44 +00:00
* Reset state:
```bash
yarn reset state --block-number < previous-block-number >
```
2021-11-15 14:35:43 +00:00
* `block-number` : Block number to which to reset the watcher.
* To export and import the watcher state:
* In source watcher, export watcher state:
```bash
2022-09-23 10:05:15 +00:00
yarn export-state --export-file [export-file-path] --block-number [snapshot-block-height]
2021-11-15 14:35:43 +00:00
```
2022-09-22 09:56:06 +00:00
* `export-file` : Path of file to which to export the watcher data.
2022-09-23 10:05:15 +00:00
* `block-number` : Block height at which to take snapshot for export.
2021-11-15 14:35:43 +00:00
* In target watcher, run job-runner:
```bash
yarn job-runner
```
2022-05-26 14:31:31 +00:00
2021-11-15 14:35:43 +00:00
* Import watcher state:
```bash
yarn import-state --import-file < import-file-path >
```
2022-09-22 09:56:06 +00:00
* `import-file` : Path of file from which to import the watcher data.
2021-11-15 14:35:43 +00:00
* Run server:
```bash
yarn server
```
* To inspect a CID:
```bash
yarn inspect-cid --cid < cid >
2021-10-28 11:01:56 +00:00
```
2021-11-15 14:35:43 +00:00
* `cid` : CID to be inspected.