| .. | ||
| environments | ||
| src | ||
| .eslintignore | ||
| .eslintrc.json | ||
| .gitignore | ||
| package.json | ||
| README.md | ||
| tsconfig.json | ||
Example Watcher
Setup
First try the stack orchestrator to quickly get started. Advanced users can see here for instructions on setting up a local environment by hand.
Run the following command to install required packages:
yarn
Run
-
In packages/graph-node, deploy an
Examplecontract:yarn example:deploy -
Set the returned address to the variable
$EXAMPLE_ADDRESS:EXAMPLE_ADDRESS=<EXAMPLE_ADDRESS> -
In packages/graph-node/test/subgraph/example1/subgraph.yaml:
- Set the source address for
Example1datasource to theEXAMPLE_ADDRESS. - Set the
startBlockless than or equal to the latest mined block.
- Set the source address for
-
Build the example subgraph:
yarn build:example -
Run the job-runner:
yarn job-runner -
Run the watcher:
yarn server -
The output from the block handler in the mapping code should be visible in the
job-runnerfor each block. -
Run the following GQL subscription at the graphql endpoint http://127.0.0.1:3008/graphql
subscription { onEvent { event { __typename ... on TestEvent { param1 param2 }, }, block { number hash } } } -
In packages/graph-node, trigger the
Testevent by calling a example contract method:yarn example:test --address $EXAMPLE_ADDRESS-
A
Testevent shall be visible in the subscription at endpoint. -
The subgraph entity
Categoryshould be updated in the database. -
An auto-generated
diff-stagedentryStateshould be added.
-
-
Run the query for entity in at the endpoint:
query { category( block: { hash: "EVENT_BLOCK_HASH" }, id: "1" ) { __typename id count name } } -
Run the
getStatequery at the endpoint to get the latestStateforEXAMPLE_ADDRESS: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 } } -
diffstates get created corresponding to thediff_stagedstates when their respective blocks reach the pruned region. -
In packages/graph-test-watcher:
-
After the
diffstate has been created, create acheckpoint:yarn checkpoint create --address $EXAMPLE_ADDRESS-
A
checkpointstate should be created at the latest canonical block hash. -
Run the
getStatequery again at the endpoint with the outputblockHashand kindcheckpoint.
-
-
-
All the
Stateentries can be seen inpg-adminin tablestate.
Customize
-
Indexing on an event:
-
Edit the custom hook function
handleEvent(triggered on an event) in hooks.ts to perform corresponding indexing using theIndexerobject. -
While using the indexer storage methods for indexing, pass
diffas true if default state is desired to be generated using the state variables being indexed.
-
-
Generating state:
-
Edit the custom hook function
createInitialState(triggered if the watcher passes the start block, checkpoint:true) in hooks.ts to save an initialStateusing theIndexerobject. -
Edit the custom hook function
createStateDiff(triggered on a block) in hooks.ts to save the state in adiffStateusing theIndexerobject. The default state (if exists) is updated. -
Edit the custom hook function
createStateCheckpoint(triggered just before default and CLI checkpoint) in hooks.ts to save the state in acheckpointStateusing theIndexerobject.
-
-
The existing example hooks in hooks.ts are for an
ERC20contract.