2021-10-28 11:01:56 +00:00
# Example Watcher
## Setup
2023-01-10 12:39:29 +00:00
First try the [stack orchestrator ](https://github.com/cerc-io/stack-orchestrator ) to quickly get started. Advanced users can see [here ](/docs/README.md ) for instructions on setting up a local environment by hand.
2021-10-28 11:01:56 +00:00
2023-01-10 12:39:29 +00:00
Run the following command to install required packages:
2021-11-15 14:35:43 +00:00
2023-01-10 12:39:29 +00:00
```bash
yarn
```
2021-10-28 11:01:56 +00:00
## Run
2023-01-10 12:39:29 +00:00
* In [packages/graph-node ](../graph-node/ ), deploy an `Example` contract:
2021-10-28 11:01:56 +00:00
2022-11-28 11:05:19 +00:00
```bash
2023-01-10 12:39:29 +00:00
yarn example:deploy
2022-11-28 11:05:19 +00:00
```
2023-01-10 12:39:29 +00:00
* Set the returned address to the variable `$EXAMPLE_ADDRESS` :
2021-10-28 11:01:56 +00:00
2023-01-10 12:39:29 +00:00
```bash
EXAMPLE_ADDRESS=< EXAMPLE_ADDRESS >
```
2021-10-28 11:01:56 +00:00
2023-01-10 12:39:29 +00:00
* In [packages/graph-node/test/subgraph/example1/subgraph.yaml ](../graph-node/test/subgraph/example1/subgraph.yaml ):
* Set the source address for `Example1` datasource to the `EXAMPLE_ADDRESS` .
* Set the `startBlock` less than or equal to the latest mined block.
2021-10-28 11:01:56 +00:00
2023-01-10 12:39:29 +00:00
* Build the example subgraph:
2022-11-25 06:01:20 +00:00
2023-01-10 12:39:29 +00:00
```bash
yarn build:example
```
2022-11-25 06:01:20 +00:00
2023-01-10 12:39:29 +00:00
* Run the job-runner:
2022-11-25 06:01:20 +00:00
2023-01-10 12:39:29 +00:00
```bash
yarn job-runner
```
2021-10-28 11:01:56 +00:00
2023-01-10 12:39:29 +00:00
* Run the watcher:
2021-11-15 14:35:43 +00:00
2023-01-10 12:39:29 +00:00
```bash
yarn server
```
2021-11-15 14:35:43 +00:00
2023-01-10 12:39:29 +00:00
* The output from the block handler in the mapping code should be visible in the `job-runner` for each block.
* Run the following GQL subscription at the graphql endpoint http://127.0.0.1:3008/graphql
```graphql
subscription {
onEvent {
event {
__typename
... on TestEvent {
param1
param2
},
},
block {
number
hash
}
}
}
```
2022-05-26 14:31:31 +00:00
2023-01-10 12:39:29 +00:00
* In [packages/graph-node ](../graph-node/ ), trigger the `Test` event by calling a example contract method:
2021-11-15 14:35:43 +00:00
2023-01-10 12:39:29 +00:00
```bash
yarn example:test --address $EXAMPLE_ADDRESS
```
2021-11-15 14:35:43 +00:00
2023-01-10 12:39:29 +00:00
* A `Test` event shall be visible in the subscription at endpoint.
2021-11-15 14:35:43 +00:00
2023-01-10 12:39:29 +00:00
* The subgraph entity `Category` should be updated in the database.
2021-10-28 11:01:56 +00:00
2023-01-10 12:39:29 +00:00
* An auto-generated `diff-staged` entry `State` should be added.
2021-10-28 11:01:56 +00:00
2023-01-10 12:39:29 +00:00
* Run the query for entity in at the endpoint:
2021-11-15 14:35:43 +00:00
2023-01-10 12:39:29 +00:00
```graphql
query {
category(
block: { hash: "EVENT_BLOCK_HASH" },
id: "1"
) {
__typename
id
count
name
}
}
```
2021-11-15 14:35:43 +00:00
2023-01-10 12:39:29 +00:00
* Run the `getState` query at the endpoint to get the latest `State` for `EXAMPLE_ADDRESS` :
```graphql
query {
getState (
blockHash: "EVENT_BLOCK_HASH"
contractAddress: "EXAMPLE_ADDRESS"
# kind: "checkpoint"
# kind: "diff"
kind: "diff_staged"
) {
cid
block {
cid
hash
number
timestamp
parentHash
}
contractAddress
data
}
}
```
2021-11-15 14:35:43 +00:00
2023-01-10 12:39:29 +00:00
* `diff` states get created corresponding to the `diff_staged` states when their respective blocks reach the pruned region.
2021-11-15 14:35:43 +00:00
2023-01-10 12:39:29 +00:00
* In [packages/graph-test-watcher ](./ ):
2021-11-15 14:35:43 +00:00
2023-01-10 12:39:29 +00:00
* After the `diff` state has been created, create a `checkpoint` :
2022-09-23 10:05:15 +00:00
```bash
2023-01-10 12:39:29 +00:00
yarn checkpoint create --address $EXAMPLE_ADDRESS
2022-09-23 10:05:15 +00:00
```
2023-01-10 12:39:29 +00:00
* A `checkpoint` state should be created at the latest canonical block hash.
2021-11-15 14:35:43 +00:00
2023-01-10 12:39:29 +00:00
* Run the `getState` query again at the endpoint with the output `blockHash` and kind `checkpoint` .
* All the `State` entries can be seen in `pg-admin` in table `state` .
2022-05-26 14:31:31 +00:00
2023-01-10 12:39:29 +00:00
* The existing example hooks in [hooks.ts ](./src/hooks.ts ) are for an `ERC20` contract.