watcher-ts/packages/erc20-watcher/demo.md
prathamesh0 847837c2fb
Add a demo for erc20-watcher (#268)
* Fix event subscription in erc20-watcher

* Add demo for erc20-watcher

* Update README
2022-11-28 18:54:09 +05:30

4.6 KiB

Demo

  • The following core services need to be running for the demo:

  • Create a postgres12 database for the watcher:

    sudo su - postgres
    
    # If database already exists
    # dropdb erc20-watcher
    
    createdb erc20-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):

    # If database already exists
    # dropdb erc20-watcher-job-queue
    
    createdb erc20-watcher-job-queue
    
    postgres@tesla:~$ psql -U postgres -h localhost erc20-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.
    
    erc20-watcher-job-queue=# CREATE EXTENSION pgcrypto;
    CREATE EXTENSION
    erc20-watcher-job-queue=# exit
    
  • In the config file update the database connection settings.

  • In watcher-ts repo, follow the instructions in Setup for installing and building packages.

    # After setup
    yarn && yarn build
    
  • Run the job-runner:

    yarn job-runner
    
  • Run the watcher:

    yarn server
    
  • Deploy an ERC20 token:

    yarn token:deploy
    # GLD Token deployed to: TOKEN_ADDRESS
    

    Export the address of the deployed token to a shell variable for later use:

    export TOKEN_ADDRESS="<TOKEN_ADDRESS>"
    
  • Run the following command to watch the contract:

    yarn watch:contract --address $TOKEN_ADDRESS --kind ERC20 --checkpoint false
    
  • Add a second account to Metamask and export the account address to a shell variable for later use:

    export RECIPIENT_ADDRESS="<RECIPIENT_ADDRESS>"
    
  • To get the current block hash at any time, run:

    yarn block:latest
    
  • Run the following GQL query against the GraphQL endpoint to get name, symbol and total supply of the deployed token:

    query {
      name(
        blockHash: "LATEST_BLOCK_HASH"
        token: "TOKEN_ADDRESS"
      ) {
        value
        proof {
          data
        }
      }
    
      symbol(
        blockHash: "LATEST_BLOCK_HASH"
        token: "TOKEN_ADDRESS"
      ) {
        value
        proof {
          data
        }
      }
    
      totalSupply(
        blockHash: "LATEST_BLOCK_HASH"
        token: "TOKEN_ADDRESS"
      ) {
        value
        proof {
          data
        }
      }
    }
    
  • Run the following GQL query against the GraphQL endpoint to get balances for the main and the recipient account at the latest block hash:

    query {
      fromBalanceOf: balanceOf(
          blockHash: "LATEST_BLOCK_HASH"
          token: "TOKEN_ADDRESS",
          # main account having all the balance initially
          owner: "0xDC7d7A8920C8Eecc098da5B7522a5F31509b5Bfc"
        ) {
        value
        proof {
          data
        }
      }
      toBalanceOf: balanceOf(
          blockHash: "LATEST_BLOCK_HASH"
          token: "TOKEN_ADDRESS",
          owner: "RECIPIENT_ADDRESS"
        ) {
        value
        proof {
          data
        }
      }
    }
    
  • Run the following GQL subscription at the GraphQL endpoint:

    subscription {
      onEvent {
        blockHash
        contract
        event {
          __typename
          ... on TransferEvent {
            from
            to
            value
          },
          ... on ApprovalEvent {
            owner
            spender
            value
          }
        }
        proof {
          data
        }
      }
    }
    
  • Transfer tokens to the recipient account:

    yarn token:transfer --token $TOKEN_ADDRESS --to $RECIPIENT_ADDRESS --amount 100
    
    • A Transfer event to RECIPIENT_ADDRESS shall be visible in the subscription at endpoint.

    • Fire the GQL query above to get updated balances for the main (from) and the recipient (to) account.