watcher-ts/packages/erc20-watcher
prathamesh0 0d22915d59
Add support to use stored peer id (#318)
* Add support to use stored peer id

* Upgrade package version

* Add a type for peer id object

* Use Buffer from buffer package for creating peer id
2023-02-14 11:27:36 +05:30
..
environments Implement peer package to send messages between peers (#279) 2023-01-10 20:10:27 +05:30
src Implement peer package to send messages between peers (#279) 2023-01-10 20:10:27 +05:30
test Update demo hardhat configs to use env variable for eth endpoint (#277) 2022-12-21 17:59:31 +05:30
.eslintignore Address watcher package scaffolding (#75) 2021-06-17 17:56:38 +05:30
.eslintrc.json Address watcher package scaffolding (#75) 2021-06-17 17:56:38 +05:30
.gitignore Tasks in erc20-watcher to deploy and transfer tokens. (#233) 2021-08-27 17:47:23 +05:30
hardhat.config.ts Update demo hardhat configs to use env variable for eth endpoint (#277) 2022-12-21 17:59:31 +05:30
package.json Add support to use stored peer id (#318) 2023-02-14 11:27:36 +05:30
README.md fixup main README, link to an SO stack where applicable 2023-01-16 07:40:40 -05:00
tsconfig.json Enable source maps for transpiled files (#242) 2021-09-07 16:01:15 +05:30

ERC20 Watcher

First try the erc20 demo in stack orchestrator to quickly get started. Advanced users can see here for instructions on setting up a local environment by hand.

Build

Build files:

yarn build

Run

Start the job runner:

yarn job-runner

For development or to specify the config file:

 yarn job-runner:dev
 yarn job-runner -f environments/local.toml

Then, Start the server:

yarn server

For development or to specify the config file:

yarn server:dev
yarn server -f environments/local.toml

See the GQL console at: http://localhost:3001/graphql Note: the port may be different depending on your configuration.

Deploy an ERC20 token:

yarn token:deploy

In the output you'll see:

GLD Token deployed to: 0xTokenAddress

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

export TOKEN_ADDRESS=0xTokenAddress

Get the main account address:

yarn account

and export it as well:

export PRIMARY_ACCOUNT=0xPrimaryAccount

Run the following command to watch the contract:

yarn watch:contract --address $TOKEN_ADDRESS --kind ERC20 --checkpoint false

For specifying a config file:

yarn watch:contract -f environments/local.toml --address 0xTokenAddress --kind ERC20 --checkpoint false

To fill a block range:

yarn fill --startBlock <from-block> --endBlock <to-block>

To get the current block hash at any time, run:

yarn block:latest

Add a new account to Metamask and export the account address to a shell variable for later use:

export RECIPIENT_ADDRESS=0xRecipientAddress

Run the following GQL query against the http://127.0.0.1:3001/graphql to get the name, symbol and total supply of the deployed token:

query {
  name(
    blockHash: "LATEST_BLOCK_HASH"
    token: "0xTokenAddress"
  ) {
    value
    proof {
      data
    }
  }

  symbol(
    blockHash: "LATEST_BLOCK_HASH"
    token: "0xTokenAddress"
  ) {
    value
    proof {
      data
    }
  }

  totalSupply(
    blockHash: "LATEST_BLOCK_HASH"
    token: "0xTokenAddress"
  ) {
    value
    proof {
      data
    }
  }
}

Run the following GQL query to get balances for the main and the recipient account at the latest block hash:

query {
  fromBalanceOf: balanceOf(
      blockHash: "LATEST_BLOCK_HASH"
      token: "0xTokenAddress",
      # main/primary account having all the balance initially
      owner: "0xPrimaryAccount"
    ) {
    value
    proof {
      data
    }
  }
  toBalanceOf: balanceOf(
      blockHash: "LATEST_BLOCK_HASH"
      token: "0xTokenAddress",
      owner: "0xRecipientAddress"
    ) {
    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 the RECIPIENT_ADDRESS should be visible in the subscription.

Get the latest block hash again, then fire the GQL query above to get updated balances for the main (from) and the recipient (to) account.