From 847837c2fb1d23adf4443c651e83017d6970f37f Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Mon, 28 Nov 2022 07:24:09 -0600 Subject: [PATCH] Add a demo for erc20-watcher (#268) * Fix event subscription in erc20-watcher * Add demo for erc20-watcher * Update README --- README.md | 87 +---------- packages/erc20-watcher/README.md | 9 +- packages/erc20-watcher/demo.md | 200 ++++++++++++++++++++++++ packages/erc20-watcher/src/indexer.ts | 5 + packages/erc20-watcher/src/resolvers.ts | 2 +- packages/erc20-watcher/src/schema.ts | 14 +- 6 files changed, 219 insertions(+), 98 deletions(-) create mode 100644 packages/erc20-watcher/demo.md diff --git a/README.md b/README.md index 48426a5a..c2083968 100644 --- a/README.md +++ b/README.md @@ -45,10 +45,6 @@ yarn build * [graph-node](./packages/graph-node/README.md) -## Demos - -* [IPLD statediff and checkpointing](./ipld-demo.md) - ## Services The default config files used by the watchers assume the following services are setup and running on localhost: @@ -65,82 +61,7 @@ The default config files used by the watchers assume the following services are ## Watchers * [eden-watcher](./packages/eden-watcher/README.md) - -## Databases - -Note: Requires `postgres12`. - -Login as the postgres user: - -```bash -sudo su - postgres -``` - -Create the databases for the watchers: - -``` -createdb erc20-watcher -createdb address-watcher -``` - -Create the databases for the job queues and enable the `pgcrypto` extension on them (https://github.com/timgit/pg-boss/blob/master/docs/usage.md#intro): - -``` -createdb erc20-watcher-job-queue -createdb address-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 -``` - -``` -postgres@tesla:~$ psql -U postgres -h localhost address-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. - -address-watcher-job-queue=# CREATE EXTENSION pgcrypto; -CREATE EXTENSION -address-watcher-job-queue=# exit -``` - -#### Reset - -Reset the databases used by the watchers: - -```bash -yarn db:reset -``` - -## Run - -Build the files in packages: - -```bash -yarn build - -# To watch for changes and build (used in development). -yarn build:watch -``` - -To run any watcher, `cd` into their package folder and run: - -```bash -yarn server -``` - -If the watcher uses a job queue, start the job runner in another terminal: - -```bash -yarn job-runner -``` +* [erc20-watcher](./packages/erc20-watcher/README.md) +* [erc721-watcher](./packages/erc721-watcher/README.md) +* [graph-test-watcher](./packages/graph-test-watcher/README.md) +* [mobymask-watcher](./packages/mobymask-watcher/README.md) diff --git a/packages/erc20-watcher/README.md b/packages/erc20-watcher/README.md index 81815056..eaab7273 100644 --- a/packages/erc20-watcher/README.md +++ b/packages/erc20-watcher/README.md @@ -62,6 +62,8 @@ Ensure that watcher is of active kind. Update the kind in `server` config to act ## Run +Follow the steps below or follow the [Demo](./demo.md) + Build files: ```bash @@ -104,16 +106,16 @@ $ yarn token:deploy Start watching a token: ```bash -$ yarn watch:contract --address 0xTokenAddress --startingBlock +$ yarn watch:contract --address 0xTokenAddress --startingBlock --kind ERC20 --checkpoint false # For specifying config file. -$ yarn watch:contract -f environments/local.toml --address 0xTokenAddress --startingBlock +$ yarn watch:contract -f environments/local.toml --address 0xTokenAddress --startingBlock --kind ERC20 --checkpoint false ``` Example: ```bash -$ yarn watch:contract --address 0xfE0034a874c2707c23F91D7409E9036F5e08ac34 --startingBlock 100 +$ yarn watch:contract --address 0xfE0034a874c2707c23F91D7409E9036F5e08ac34 --startingBlock 100 --kind ERC20 --checkpoint false ``` To fill a block range: @@ -189,5 +191,4 @@ $ yarn fill --startBlock 1000 --endBlock 2000 } } } - ``` diff --git a/packages/erc20-watcher/demo.md b/packages/erc20-watcher/demo.md new file mode 100644 index 00000000..a27dfa41 --- /dev/null +++ b/packages/erc20-watcher/demo.md @@ -0,0 +1,200 @@ +# Demo + +* The following core services need to be running for the demo: + * [ipld-eth-db](https://github.com/cerc-io/ipld-eth-db) + * Version: [v4.2.3-alpha](https://github.com/cerc-io/ipld-eth-db/releases/tag/v4.2.3-alpha) + * [geth](https://github.com/cerc-io/go-ethereum) + * State diffing service should use `ipld-eth-db` for database. + * Version: [v1.10.26-statediff-4.2.2-alpha](https://github.com/cerc-io/go-ethereum/releases/tag/v1.10.26-statediff-4.2.2-alpha) + * Endpoint: http://127.0.0.1:8545 + * [ipld-eth-server](https://github.com/cerc-io/ipld-eth-server) + * Should use `ipld-eth-db` for database. + * Version: [v4.2.3-alpha](https://github.com/cerc-io/ipld-eth-server/releases/tag/v4.2.3-alpha) + * Endpoints: + * GQL: http://127.0.0.1:8082/graphql + * RPC: http://127.0.0.1:8081 + +* Create a postgres12 database for the watcher: + + ```bash + 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): + + ```bash + # 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](./environments/local.toml) update the `database` connection settings. + +* In `watcher-ts` repo, follow the instructions in [Setup](../../README.md#setup) for installing and building packages. + + ```bash + # After setup + yarn && yarn build + ``` + +* Run the job-runner: + + ```bash + yarn job-runner + ``` + +* Run the watcher: + + ```bash + yarn server + ``` + +* Deploy an ERC20 token: + + ```bash + yarn token:deploy + # GLD Token deployed to: TOKEN_ADDRESS + ``` + + Export the address of the deployed token to a shell variable for later use: + + ```bash + export TOKEN_ADDRESS="" + ``` + +* Run the following command to watch the contract: + + ```bash + 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: + + ```bash + export RECIPIENT_ADDRESS="" + ``` + +* To get the current block hash at any time, run: + + ```bash + yarn block:latest + ``` + +* Run the following GQL query against the [GraphQL endpoint](http://127.0.0.1:3001/graphql) to get name, symbol and total supply of the deployed token: + + ```graphql + 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](http://127.0.0.1:3001/graphql) to get balances for the main and the recipient account at the latest block hash: + + ```graphql + 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: + + ```graphql + subscription { + onEvent { + blockHash + contract + event { + __typename + ... on TransferEvent { + from + to + value + }, + ... on ApprovalEvent { + owner + spender + value + } + } + proof { + data + } + } + } + ``` + +* Transfer tokens to the recipient account: + + ```bash + 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. diff --git a/packages/erc20-watcher/src/indexer.ts b/packages/erc20-watcher/src/indexer.ts index e76d8179..b4ece063 100644 --- a/packages/erc20-watcher/src/indexer.ts +++ b/packages/erc20-watcher/src/indexer.ts @@ -47,6 +47,8 @@ const TRANSFER_EVENT = 'Transfer'; const APPROVAL_EVENT = 'Approval'; interface EventResult { + blockHash: string; + contract: string; event: { from?: string; to?: string; @@ -105,9 +107,12 @@ export class Indexer implements IndexerInterface { } getResultEvent (event: Event): EventResult { + const block = event.block; const eventFields = JSON.parse(event.eventInfo); return { + blockHash: block.blockHash, + contract: event.contract, event: { __typename: `${event.eventName}Event`, ...eventFields diff --git a/packages/erc20-watcher/src/resolvers.ts b/packages/erc20-watcher/src/resolvers.ts index dd14bee1..fa7d54e3 100644 --- a/packages/erc20-watcher/src/resolvers.ts +++ b/packages/erc20-watcher/src/resolvers.ts @@ -28,7 +28,7 @@ export const createResolvers = async (indexerArg: IndexerInterface, eventWatcher }, Subscription: { - onTokenEvent: { + onEvent: { subscribe: () => eventWatcher.getEventIterator() } }, diff --git a/packages/erc20-watcher/src/schema.ts b/packages/erc20-watcher/src/schema.ts index a75ba539..cbb44c3f 100644 --- a/packages/erc20-watcher/src/schema.ts +++ b/packages/erc20-watcher/src/schema.ts @@ -58,20 +58,14 @@ union TokenEvent = TransferEvent | ApprovalEvent # Result type, with proof, for event return values. type ResultEvent { - event: TokenEvent! + blockHash: String! + contract: String! + event: TokenEvent! # Proof from receipts trie. proof: Proof } -# Watched event, include additional context over and above the event data. -type WatchedEvent { - blockHash: String! - token: String! - - event: ResultEvent! -} - type SyncStatus { latestIndexedBlockHash: String! latestIndexedBlockNumber: Int! @@ -156,7 +150,7 @@ type Query { type Subscription { # Watch for token events (at head of chain). - onTokenEvent: WatchedEvent! + onEvent: ResultEvent! } #