sushiswap-watcher-ts/packages/sushiswap-watcher
Nabarun 31dd608608 Add stack for sushiswap subgraph watcher (#11)
Part of [Create an external stack for sushiswap subgraph watchers](https://www.notion.so/Create-an-external-stack-for-sushiswap-subgraph-watchers-932617e953b54193af25b24a578145b7)

Co-authored-by: IshaVenikar <ishavenikar7@gmail.com>
Reviewed-on: cerc-io/sushiswap-watcher-ts#11
Co-authored-by: Nabarun <nabarun@deepstacksoft.com>
Co-committed-by: Nabarun <nabarun@deepstacksoft.com>
2024-06-19 07:47:10 +00:00
..
.husky Generate watcher for sushiswap subgraph (#4) 2024-06-12 04:49:33 +00:00
environments Add codegen configs used to generate watchers (#6) 2024-06-13 12:16:43 +00:00
gql-logs Generate watcher for sushiswap subgraph (#4) 2024-06-12 04:49:33 +00:00
src Generate watcher for sushiswap subgraph (#4) 2024-06-12 04:49:33 +00:00
subgraph-build Add stack for sushiswap subgraph watcher (#11) 2024-06-19 07:47:10 +00:00
.eslintignore Generate watcher for sushiswap subgraph (#4) 2024-06-12 04:49:33 +00:00
.eslintrc.json Generate watcher for sushiswap subgraph (#4) 2024-06-12 04:49:33 +00:00
.gitignore Generate watcher for sushiswap subgraph (#4) 2024-06-12 04:49:33 +00:00
.npmrc Generate watcher for sushiswap subgraph (#4) 2024-06-12 04:49:33 +00:00
codegen-config.yml Add codegen configs used to generate watchers (#6) 2024-06-13 12:16:43 +00:00
LICENSE Generate watcher for sushiswap subgraph (#4) 2024-06-12 04:49:33 +00:00
package.json Generate watcher for sushiswap route-processor subgraph (#5) 2024-06-12 13:10:43 +00:00
README.md Add readme step to build watchers (#7) 2024-06-13 12:21:41 +00:00
tsconfig.json Generate watcher for sushiswap subgraph (#4) 2024-06-12 04:49:33 +00:00

sushiswap-watcher

Source

Setup

  • Follow root README.md setup

  • Create a postgres12 database for the watcher:

    sudo su - postgres
    createdb sushiswap-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 sushiswap-watcher-job-queue
    
    postgres@tesla:~$ psql -U postgres -h localhost sushiswap-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.
    
    sushiswap-watcher-job-queue=# CREATE EXTENSION pgcrypto;
    CREATE EXTENSION
    sushiswap-watcher-job-queue=# exit
    
  • In the config file:

    • Update the database connection settings.

    • Update the upstream config and provide the ipld-eth-server GQL API endpoint.

    • Update the server config with state checkpoint settings.

Customize

  • Indexing on an event:

    • Edit the custom hook function handleEvent (triggered on an event) in hooks.ts to perform corresponding indexing using the Indexer object.

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

    • Edit the custom hook function createInitialState (triggered if the watcher passes the start block, checkpoint: true) in hooks.ts to save an initial State using the Indexer object.

    • Edit the custom hook function createStateDiff (triggered on a block) in hooks.ts to save the state in a diff State using the Indexer object. 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 a checkpoint State using the Indexer object.

GQL Caching

To enable GQL requests caching:

  • Update the server.gql.cache config with required settings.

  • In the GQL schema file, use the cacheControl directive to apply cache hints at schema level.

    • Eg. Set inheritMaxAge to true for non-scalar fields of a type.
  • In the GQL resolvers file, uncomment the setGQLCacheHints() calls in resolvers for required queries.

Run

  • If the watcher is a lazy watcher:

  • If the watcher is an active watcher:

    • Run the job-runner:

      yarn job-runner
      
    • Run the server:

      yarn server
      

      GQL console: http://localhost:3008/graphql

    • To watch a contract:

      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:

      Watch a contract with its address and checkpointing on:

      yarn watch:contract --address 0x1F78641644feB8b64642e833cE4AFE93DD6e7833 --kind ERC20 --checkpoint true
      

      Watch a contract with its identifier and checkpointing on:

      yarn watch:contract --address MyProtocol --kind protocol --checkpoint true
      
    • To fill a block range:

      yarn fill --start-block <from-block> --end-block <to-block>
      
      • start-block: Block number to start filling from.
      • end-block: Block number till which to fill.
    • To create a checkpoint for a contract:

      yarn checkpoint create --address <contract-address> --block-hash [block-hash]
      
      • 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).
    • To verify a checkpoint:

      yarn checkpoint verify --cid <checkpoint-cid>
      

      cid: CID of the checkpoint for which to verify.

    • To reset the watcher to a previous block number:

      • Reset watcher:

        yarn reset watcher --block-number <previous-block-number>
        
      • Reset job-queue:

        yarn reset job-queue
        
      • Reset state:

        yarn reset state --block-number <previous-block-number>
        
      • block-number: Block number to which to reset the watcher.

    • To export and import the watcher state:

      • In source watcher, export watcher state:

        yarn export-state --export-file [export-file-path] --block-number [snapshot-block-height]
        
        • export-file: Path of file to which to export the watcher data.
        • block-number: Block height at which to take snapshot for export.
      • In target watcher, run job-runner:

        yarn job-runner
        
      • Import watcher state:

        yarn import-state --import-file <import-file-path>
        
        • import-file: Path of file from which to import the watcher data.
      • Run server:

        yarn server
        
    • To inspect a CID:

      yarn inspect-cid --cid <cid>
      
      • cid: CID to be inspected.