watcher-ts/packages/graph-test-watcher
Nabarun Gogoi 1ba731915d
Log pseudonyms with peer id (#334)
* Log pseudonyms with peer id

* Upgrade package version
2023-03-06 16:26:38 +05:30
..
environments Accommodate GQL optimizations in graph-test-watcher (#249) 2022-11-22 15:20:44 +05:30
src Implement peer package to send messages between peers (#279) 2023-01-10 20:10:27 +05:30
.eslintignore Integrate generated watcher to invoke handlers in graph-node (#33) 2021-12-28 16:08:05 +05:30
.eslintrc.json Integrate generated watcher to invoke handlers in graph-node (#33) 2021-12-28 16:08:05 +05:30
.gitignore Integrate generated watcher to invoke handlers in graph-node (#33) 2021-12-28 16:08:05 +05:30
package.json Log pseudonyms with peer id (#334) 2023-03-06 16:26:38 +05:30
README.md deduplicate the Customize section 2023-01-16 07:40:40 -05:00
tsconfig.json Add a table for entites in frothy region for subgraph watchers (#231) 2022-11-16 17:12:54 +05:30

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 Example contract:

    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 Example1 datasource to the EXAMPLE_ADDRESS.
    • Set the startBlock less than or equal to the latest mined block.
  • 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-runner for 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 Test event by calling a example contract method:

    yarn example:test --address $EXAMPLE_ADDRESS
    
    • A Test event shall be visible in the subscription at endpoint.

    • The subgraph entity Category should be updated in the database.

    • An auto-generated diff-staged entry State should 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 getState query at the endpoint to get the latest State for EXAMPLE_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
      }
    }
    
  • diff states get created corresponding to the diff_staged states when their respective blocks reach the pruned region.

  • In packages/graph-test-watcher:

    • After the diff state has been created, create a checkpoint:

      yarn checkpoint create --address $EXAMPLE_ADDRESS
      
      • A checkpoint state should be created at the latest canonical block hash.

      • 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.

  • The existing example hooks in hooks.ts are for an ERC20 contract.