From 2b90c3891cf480a0f90da87a9626b0092da8fe53 Mon Sep 17 00:00:00 2001 From: Erik Dies Date: Wed, 12 Apr 2023 03:24:50 +0000 Subject: [PATCH] Auto generated ERC20 watcher using MuKnSys/watcher-dsl-beta --- packages/ERC20TOT/.eslintignore | 2 + packages/ERC20TOT/.eslintrc.json | 28 + packages/ERC20TOT/README.md | 204 +++++ packages/ERC20TOT/environments/local.toml | 66 ++ packages/ERC20TOT/package.json | 71 ++ packages/ERC20TOT/src/artifacts/ERC20.json | 365 ++++++++ .../src/cli/checkpoint-cmds/create.ts | 34 + packages/ERC20TOT/src/cli/checkpoint.ts | 39 + packages/ERC20TOT/src/cli/export-state.ts | 28 + packages/ERC20TOT/src/cli/import-state.ts | 29 + packages/ERC20TOT/src/cli/index-block.ts | 28 + packages/ERC20TOT/src/cli/inspect-cid.ts | 28 + .../ERC20TOT/src/cli/reset-cmds/job-queue.ts | 22 + packages/ERC20TOT/src/cli/reset-cmds/state.ts | 24 + .../ERC20TOT/src/cli/reset-cmds/watcher.ts | 27 + packages/ERC20TOT/src/cli/reset.ts | 24 + packages/ERC20TOT/src/cli/watch-contract.ts | 28 + packages/ERC20TOT/src/client.ts | 154 ++++ packages/ERC20TOT/src/database.ts | 447 ++++++++++ packages/ERC20TOT/src/entity/Allowance.ts | 34 + packages/ERC20TOT/src/entity/BalanceOf.ts | 31 + packages/ERC20TOT/src/entity/BlockProgress.ts | 48 ++ packages/ERC20TOT/src/entity/Contract.ts | 24 + packages/ERC20TOT/src/entity/Decimals.ts | 27 + packages/ERC20TOT/src/entity/Event.ts | 38 + packages/ERC20TOT/src/entity/Name.ts | 27 + packages/ERC20TOT/src/entity/State.ts | 31 + .../ERC20TOT/src/entity/StateSyncStatus.ts | 17 + packages/ERC20TOT/src/entity/Symbol.ts | 27 + packages/ERC20TOT/src/entity/SyncStatus.ts | 36 + packages/ERC20TOT/src/entity/TotalSupply.ts | 28 + packages/ERC20TOT/src/entity/_Allowances.ts | 34 + packages/ERC20TOT/src/entity/_Balances.ts | 31 + packages/ERC20TOT/src/entity/_Name.ts | 27 + packages/ERC20TOT/src/entity/_Symbol.ts | 27 + packages/ERC20TOT/src/entity/_TotalSupply.ts | 28 + packages/ERC20TOT/src/fill.ts | 33 + packages/ERC20TOT/src/gql/index.ts | 3 + packages/ERC20TOT/src/gql/mutations/index.ts | 4 + .../src/gql/mutations/watchContract.gql | 3 + .../ERC20TOT/src/gql/queries/_allowances.gql | 8 + .../ERC20TOT/src/gql/queries/_balances.gql | 8 + packages/ERC20TOT/src/gql/queries/_name.gql | 8 + packages/ERC20TOT/src/gql/queries/_symbol.gql | 8 + .../ERC20TOT/src/gql/queries/_totalSupply.gql | 8 + .../ERC20TOT/src/gql/queries/allowance.gql | 8 + .../ERC20TOT/src/gql/queries/balanceOf.gql | 8 + .../ERC20TOT/src/gql/queries/decimals.gql | 8 + packages/ERC20TOT/src/gql/queries/events.gql | 34 + .../src/gql/queries/eventsInRange.gql | 34 + .../ERC20TOT/src/gql/queries/getState.gql | 15 + .../src/gql/queries/getStateByCID.gql | 15 + .../src/gql/queries/getSyncStatus.gql | 8 + packages/ERC20TOT/src/gql/queries/index.ts | 19 + packages/ERC20TOT/src/gql/queries/name.gql | 8 + packages/ERC20TOT/src/gql/queries/symbol.gql | 8 + .../ERC20TOT/src/gql/queries/totalSupply.gql | 8 + .../ERC20TOT/src/gql/subscriptions/index.ts | 4 + .../src/gql/subscriptions/onEvent.gql | 34 + packages/ERC20TOT/src/hooks.ts | 80 ++ packages/ERC20TOT/src/indexer.ts | 799 ++++++++++++++++++ packages/ERC20TOT/src/job-runner.ts | 37 + packages/ERC20TOT/src/resolvers.ts | 305 +++++++ packages/ERC20TOT/src/schema.gql | 116 +++ packages/ERC20TOT/src/server.ts | 33 + packages/ERC20TOT/src/types.ts | 3 + packages/ERC20TOT/tsconfig.json | 74 ++ 67 files changed, 3904 insertions(+) create mode 100644 packages/ERC20TOT/.eslintignore create mode 100644 packages/ERC20TOT/.eslintrc.json create mode 100644 packages/ERC20TOT/README.md create mode 100644 packages/ERC20TOT/environments/local.toml create mode 100644 packages/ERC20TOT/package.json create mode 100644 packages/ERC20TOT/src/artifacts/ERC20.json create mode 100644 packages/ERC20TOT/src/cli/checkpoint-cmds/create.ts create mode 100644 packages/ERC20TOT/src/cli/checkpoint.ts create mode 100644 packages/ERC20TOT/src/cli/export-state.ts create mode 100644 packages/ERC20TOT/src/cli/import-state.ts create mode 100644 packages/ERC20TOT/src/cli/index-block.ts create mode 100644 packages/ERC20TOT/src/cli/inspect-cid.ts create mode 100644 packages/ERC20TOT/src/cli/reset-cmds/job-queue.ts create mode 100644 packages/ERC20TOT/src/cli/reset-cmds/state.ts create mode 100644 packages/ERC20TOT/src/cli/reset-cmds/watcher.ts create mode 100644 packages/ERC20TOT/src/cli/reset.ts create mode 100644 packages/ERC20TOT/src/cli/watch-contract.ts create mode 100644 packages/ERC20TOT/src/client.ts create mode 100644 packages/ERC20TOT/src/database.ts create mode 100644 packages/ERC20TOT/src/entity/Allowance.ts create mode 100644 packages/ERC20TOT/src/entity/BalanceOf.ts create mode 100644 packages/ERC20TOT/src/entity/BlockProgress.ts create mode 100644 packages/ERC20TOT/src/entity/Contract.ts create mode 100644 packages/ERC20TOT/src/entity/Decimals.ts create mode 100644 packages/ERC20TOT/src/entity/Event.ts create mode 100644 packages/ERC20TOT/src/entity/Name.ts create mode 100644 packages/ERC20TOT/src/entity/State.ts create mode 100644 packages/ERC20TOT/src/entity/StateSyncStatus.ts create mode 100644 packages/ERC20TOT/src/entity/Symbol.ts create mode 100644 packages/ERC20TOT/src/entity/SyncStatus.ts create mode 100644 packages/ERC20TOT/src/entity/TotalSupply.ts create mode 100644 packages/ERC20TOT/src/entity/_Allowances.ts create mode 100644 packages/ERC20TOT/src/entity/_Balances.ts create mode 100644 packages/ERC20TOT/src/entity/_Name.ts create mode 100644 packages/ERC20TOT/src/entity/_Symbol.ts create mode 100644 packages/ERC20TOT/src/entity/_TotalSupply.ts create mode 100644 packages/ERC20TOT/src/fill.ts create mode 100644 packages/ERC20TOT/src/gql/index.ts create mode 100644 packages/ERC20TOT/src/gql/mutations/index.ts create mode 100644 packages/ERC20TOT/src/gql/mutations/watchContract.gql create mode 100644 packages/ERC20TOT/src/gql/queries/_allowances.gql create mode 100644 packages/ERC20TOT/src/gql/queries/_balances.gql create mode 100644 packages/ERC20TOT/src/gql/queries/_name.gql create mode 100644 packages/ERC20TOT/src/gql/queries/_symbol.gql create mode 100644 packages/ERC20TOT/src/gql/queries/_totalSupply.gql create mode 100644 packages/ERC20TOT/src/gql/queries/allowance.gql create mode 100644 packages/ERC20TOT/src/gql/queries/balanceOf.gql create mode 100644 packages/ERC20TOT/src/gql/queries/decimals.gql create mode 100644 packages/ERC20TOT/src/gql/queries/events.gql create mode 100644 packages/ERC20TOT/src/gql/queries/eventsInRange.gql create mode 100644 packages/ERC20TOT/src/gql/queries/getState.gql create mode 100644 packages/ERC20TOT/src/gql/queries/getStateByCID.gql create mode 100644 packages/ERC20TOT/src/gql/queries/getSyncStatus.gql create mode 100644 packages/ERC20TOT/src/gql/queries/index.ts create mode 100644 packages/ERC20TOT/src/gql/queries/name.gql create mode 100644 packages/ERC20TOT/src/gql/queries/symbol.gql create mode 100644 packages/ERC20TOT/src/gql/queries/totalSupply.gql create mode 100644 packages/ERC20TOT/src/gql/subscriptions/index.ts create mode 100644 packages/ERC20TOT/src/gql/subscriptions/onEvent.gql create mode 100644 packages/ERC20TOT/src/hooks.ts create mode 100644 packages/ERC20TOT/src/indexer.ts create mode 100644 packages/ERC20TOT/src/job-runner.ts create mode 100644 packages/ERC20TOT/src/resolvers.ts create mode 100644 packages/ERC20TOT/src/schema.gql create mode 100644 packages/ERC20TOT/src/server.ts create mode 100644 packages/ERC20TOT/src/types.ts create mode 100644 packages/ERC20TOT/tsconfig.json diff --git a/packages/ERC20TOT/.eslintignore b/packages/ERC20TOT/.eslintignore new file mode 100644 index 00000000..55cb5225 --- /dev/null +++ b/packages/ERC20TOT/.eslintignore @@ -0,0 +1,2 @@ +# Don't lint build output. +dist diff --git a/packages/ERC20TOT/.eslintrc.json b/packages/ERC20TOT/.eslintrc.json new file mode 100644 index 00000000..a2b842c2 --- /dev/null +++ b/packages/ERC20TOT/.eslintrc.json @@ -0,0 +1,28 @@ +{ + "env": { + "browser": true, + "es2021": true + }, + "extends": [ + "semistandard", + "plugin:@typescript-eslint/recommended" + ], + "parser": "@typescript-eslint/parser", + "parserOptions": { + "ecmaVersion": 12, + "sourceType": "module" + }, + "plugins": [ + "@typescript-eslint" + ], + "rules": { + "indent": ["error", 2, { "SwitchCase": 1 }], + "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/explicit-module-boundary-types": [ + "warn", + { + "allowArgumentsExplicitlyTypedAsAny": true + } + ] + } +} diff --git a/packages/ERC20TOT/README.md b/packages/ERC20TOT/README.md new file mode 100644 index 00000000..ad506a02 --- /dev/null +++ b/packages/ERC20TOT/README.md @@ -0,0 +1,204 @@ +# ERC20TOT + +## Setup + +* Run the following command to install required packages: + + ```bash + yarn + ``` + +* Create a postgres12 database for the watcher: + + ```bash + sudo su - postgres + createdb ERC20TOT + ``` + +* 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 ERC20TOT-job-queue + ``` + + ``` + postgres@tesla:~$ psql -U postgres -h localhost ERC20TOT-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. + + ERC20TOT-job-queue=# CREATE EXTENSION pgcrypto; + CREATE EXTENSION + ERC20TOT-job-queue=# exit + ``` + +* In the [config file](./environments/local.toml): + + * 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](./src/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](./src/hooks.ts) to save an initial `State` using the `Indexer` object. + + * Edit the custom hook function `createStateDiff` (triggered on a block) in [hooks.ts](./src/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](./src/hooks.ts) to save the state in a `checkpoint` `State` using the `Indexer` object. + +### GQL Caching + +To enable GQL requests caching: + +* Update the `server.gqlCache` config with required settings. + +* In the GQL [schema file](./src/schema.gql), 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](./src/resolvers.ts), uncomment the `setGQLCacheHints()` calls in resolvers for required queries. + +## Run + +* If the watcher is a `lazy` watcher: + + * Run the server: + + ```bash + yarn server + ``` + + GQL console: http://localhost:3009/graphql + +* If the watcher is an `active` watcher: + + * Run the job-runner: + + ```bash + yarn job-runner + ``` + + * Run the server: + + ```bash + yarn server + ``` + + GQL console: http://localhost:3009/graphql + + * To watch a contract: + + ```bash + yarn watch:contract --address --kind --checkpoint --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: + + ```bash + yarn watch:contract --address 0x1F78641644feB8b64642e833cE4AFE93DD6e7833 --kind ERC20 --checkpoint true + ``` + + Watch a contract with its identifier and checkpointing on: + + ```bash + yarn watch:contract --address MyProtocol --kind protocol --checkpoint true + ``` + + * To fill a block range: + + ```bash + yarn fill --start-block --end-block + ``` + + * `start-block`: Block number to start filling from. + * `end-block`: Block number till which to fill. + + * To create a checkpoint for a contract: + + ```bash + yarn checkpoint create --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 reset the watcher to a previous block number: + + * Reset watcher: + + ```bash + yarn reset watcher --block-number + ``` + + * Reset job-queue: + + ```bash + yarn reset job-queue + ``` + + * Reset state: + + ```bash + yarn reset state --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: + + ```bash + 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: + + ```bash + yarn job-runner + ``` + + * Import watcher state: + + ```bash + yarn import-state --import-file + ``` + + * `import-file`: Path of file from which to import the watcher data. + + * Run server: + + ```bash + yarn server + ``` + + * To inspect a CID: + + ```bash + yarn inspect-cid --cid + ``` + + * `cid`: CID to be inspected. diff --git a/packages/ERC20TOT/environments/local.toml b/packages/ERC20TOT/environments/local.toml new file mode 100644 index 00000000..b34e1e5f --- /dev/null +++ b/packages/ERC20TOT/environments/local.toml @@ -0,0 +1,66 @@ +[server] + host = "127.0.0.1" + port = 3009 + kind = "active" + + # Checkpointing state. + checkpointing = true + + # Checkpoint interval in number of blocks. + checkpointInterval = 2000 + + # Enable state creation + # CAUTION: Disable only if state creation is not desired or can be filled subsequently + enableState = true + + # Boolean to filter logs by contract. + filterLogs = false + + # Max block range for which to return events in eventsInRange GQL query. + # Use -1 for skipping check on block range. + maxEventsBlockRange = 1000 + + # GQL cache settings + [server.gqlCache] + enabled = true + + # Max in-memory cache size (in bytes) (default 8 MB) + # maxCacheSize + + # GQL cache-control max-age settings (in seconds) + maxAge = 15 + +[metrics] + host = "127.0.0.1" + port = 9000 + [metrics.gql] + port = 9001 + +[database] + type = "postgres" + host = "localhost" + port = 5432 + database = "ERC20TOT" + username = "postgres" + password = "postgres" + synchronize = true + logging = false + +[upstream] + [upstream.ethServer] + gqlApiEndpoint = "http://127.0.0.1:8082/graphql" + rpcProviderEndpoint = "http://127.0.0.1:8081" + + [upstream.cache] + name = "requests" + enabled = false + deleteOnStart = false + +[jobQueue] + dbConnectionString = "postgres://postgres:postgres@localhost/ERC20TOT-job-queue" + maxCompletionLagInSecs = 300 + jobDelayInMilliSecs = 100 + eventsInBatch = 50 + blockDelayInMilliSecs = 2000 + prefetchBlocksInMem = true + prefetchBlockCount = 10 diff --git a/packages/ERC20TOT/package.json b/packages/ERC20TOT/package.json new file mode 100644 index 00000000..f8e3083d --- /dev/null +++ b/packages/ERC20TOT/package.json @@ -0,0 +1,71 @@ +{ + "name": "@cerc-io/ERC20TOT", + "version": "0.1.0", + "description": "ERC20TOT", + "private": true, + "main": "dist/index.js", + "scripts": { + "lint": "eslint .", + "build": "yarn clean && tsc && yarn copy-assets", + "clean": "rm -rf ./dist", + "copy-assets": "copyfiles -u 1 src/**/*.gql dist/", + "server": "DEBUG=vulcanize:* YARN_CHILD_PROCESS=true node --enable-source-maps dist/server.js", + "server:dev": "DEBUG=vulcanize:* YARN_CHILD_PROCESS=true ts-node src/server.ts", + "job-runner": "DEBUG=vulcanize:* YARN_CHILD_PROCESS=true node --enable-source-maps dist/job-runner.js", + "job-runner:dev": "DEBUG=vulcanize:* YARN_CHILD_PROCESS=true ts-node src/job-runner.ts", + "watch:contract": "DEBUG=vulcanize:* ts-node src/cli/watch-contract.ts", + "fill": "DEBUG=vulcanize:* ts-node src/fill.ts", + "reset": "DEBUG=vulcanize:* ts-node src/cli/reset.ts", + "checkpoint": "DEBUG=vulcanize:* node --enable-source-maps dist/cli/checkpoint.js", + "checkpoint:dev": "DEBUG=vulcanize:* ts-node src/cli/checkpoint.ts", + "export-state": "DEBUG=vulcanize:* node --enable-source-maps dist/cli/export-state.js", + "export-state:dev": "DEBUG=vulcanize:* ts-node src/cli/export-state.ts", + "import-state": "DEBUG=vulcanize:* node --enable-source-maps dist/cli/import-state.js", + "import-state:dev": "DEBUG=vulcanize:* ts-node src/cli/import-state.ts", + "inspect-cid": "DEBUG=vulcanize:* ts-node src/cli/inspect-cid.ts", + "index-block": "DEBUG=vulcanize:* ts-node src/cli/index-block.ts" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/cerc-io/watcher-ts.git" + }, + "author": "", + "license": "AGPL-3.0", + "bugs": { + "url": "https://github.com/cerc-io/watcher-ts/issues" + }, + "homepage": "https://github.com/cerc-io/watcher-ts#readme", + "dependencies": { + "@apollo/client": "^3.3.19", + "@ethersproject/providers": "^5.4.4", + "@cerc-io/cli": "^0.2.34", + "@cerc-io/ipld-eth-client": "^0.2.34", + "@cerc-io/solidity-mapper": "^0.2.34", + "@cerc-io/util": "^0.2.34", + "apollo-type-bigint": "^0.1.3", + "debug": "^4.3.1", + "ethers": "^5.4.4", + "graphql": "^15.5.0", + "json-bigint": "^1.0.0", + "reflect-metadata": "^0.1.13", + "typeorm": "^0.2.32", + "yargs": "^17.0.1", + "decimal.js": "^10.3.1" + }, + "devDependencies": { + "@ethersproject/abi": "^5.3.0", + "@types/yargs": "^17.0.0", + "@typescript-eslint/eslint-plugin": "^5.47.1", + "@typescript-eslint/parser": "^5.47.1", + "eslint": "^8.35.0", + "eslint-config-semistandard": "^15.0.1", + "eslint-config-standard": "^16.0.3", + "eslint-plugin-import": "^2.27.5", + "eslint-plugin-node": "^11.1.0", + "eslint-plugin-promise": "^5.1.0", + "eslint-plugin-standard": "^5.0.0", + "ts-node": "^10.2.1", + "typescript": "^5.0.2", + "copyfiles": "^2.4.1" + } +} diff --git a/packages/ERC20TOT/src/artifacts/ERC20.json b/packages/ERC20TOT/src/artifacts/ERC20.json new file mode 100644 index 00000000..e67aacd5 --- /dev/null +++ b/packages/ERC20TOT/src/artifacts/ERC20.json @@ -0,0 +1,365 @@ +{ + "abi": [ + { + "inputs": [ + { + "internalType": "string", + "name": "name_", + "type": "string" + }, + { + "internalType": "string", + "name": "symbol_", + "type": "string" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "value", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "value", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "internalType": "address", + "name": "spender", + "type": "address" + } + ], + "name": "allowance", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "account", + "type": "address" + } + ], + "name": "balanceOf", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "decimals", + "outputs": [ + { + "internalType": "uint8", + "name": "", + "type": "uint8" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "subtractedValue", + "type": "uint256" + } + ], + "name": "decreaseAllowance", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "addedValue", + "type": "uint256" + } + ], + "name": "increaseAllowance", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "name", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "symbol", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "totalSupply", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "transfer", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + } + ], + "storageLayout": { + "storage": [ + { + "astId": 130, + "contract": "ERC20.sol:ERC20", + "label": "_balances", + "offset": 0, + "slot": "0", + "type": "t_mapping(t_address,t_uint256)" + }, + { + "astId": 136, + "contract": "ERC20.sol:ERC20", + "label": "_allowances", + "offset": 0, + "slot": "1", + "type": "t_mapping(t_address,t_mapping(t_address,t_uint256))" + }, + { + "astId": 138, + "contract": "ERC20.sol:ERC20", + "label": "_totalSupply", + "offset": 0, + "slot": "2", + "type": "t_uint256" + }, + { + "astId": 140, + "contract": "ERC20.sol:ERC20", + "label": "_name", + "offset": 0, + "slot": "3", + "type": "t_string_storage" + }, + { + "astId": 142, + "contract": "ERC20.sol:ERC20", + "label": "_symbol", + "offset": 0, + "slot": "4", + "type": "t_string_storage" + } + ], + "types": { + "t_address": { + "encoding": "inplace", + "label": "address", + "numberOfBytes": "20" + }, + "t_mapping(t_address,t_mapping(t_address,t_uint256))": { + "encoding": "mapping", + "key": "t_address", + "label": "mapping(address => mapping(address => uint256))", + "numberOfBytes": "32", + "value": "t_mapping(t_address,t_uint256)" + }, + "t_mapping(t_address,t_uint256)": { + "encoding": "mapping", + "key": "t_address", + "label": "mapping(address => uint256)", + "numberOfBytes": "32", + "value": "t_uint256" + }, + "t_string_storage": { + "encoding": "bytes", + "label": "string", + "numberOfBytes": "32" + }, + "t_uint256": { + "encoding": "inplace", + "label": "uint256", + "numberOfBytes": "32" + } + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/cli/checkpoint-cmds/create.ts b/packages/ERC20TOT/src/cli/checkpoint-cmds/create.ts new file mode 100644 index 00000000..b7d343aa --- /dev/null +++ b/packages/ERC20TOT/src/cli/checkpoint-cmds/create.ts @@ -0,0 +1,34 @@ +// +// Copyright 2022 Vulcanize, Inc. +// + +import { CreateCheckpointCmd } from '@cerc-io/cli'; + +import { Database } from '../../database'; +import { Indexer } from '../../indexer'; + +export const command = 'create'; + +export const desc = 'Create checkpoint'; + +export const builder = { + address: { + type: 'string', + require: true, + demandOption: true, + describe: 'Contract address to create the checkpoint for.' + }, + blockHash: { + type: 'string', + describe: 'Blockhash at which to create the checkpoint.' + } +}; + +export const handler = async (argv: any): Promise => { + const createCheckpointCmd = new CreateCheckpointCmd(); + await createCheckpointCmd.init(argv, Database); + + await createCheckpointCmd.initIndexer(Indexer); + + await createCheckpointCmd.exec(); +}; diff --git a/packages/ERC20TOT/src/cli/checkpoint.ts b/packages/ERC20TOT/src/cli/checkpoint.ts new file mode 100644 index 00000000..d05ad8ad --- /dev/null +++ b/packages/ERC20TOT/src/cli/checkpoint.ts @@ -0,0 +1,39 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import yargs from 'yargs'; +import 'reflect-metadata'; +import debug from 'debug'; + +import { DEFAULT_CONFIG_PATH } from '@cerc-io/util'; + +import { hideBin } from 'yargs/helpers'; + +const log = debug('vulcanize:checkpoint'); + +const main = async () => { + return yargs(hideBin(process.argv)) + .parserConfiguration({ + 'parse-numbers': false + }).options({ + configFile: { + alias: 'f', + type: 'string', + require: true, + demandOption: true, + describe: 'configuration file path (toml)', + default: DEFAULT_CONFIG_PATH + } + }) + .commandDir('checkpoint-cmds', { extensions: ['ts', 'js'], exclude: /([a-zA-Z0-9\s_\\.\-:])+(.d.ts)$/ }) + .demandCommand(1) + .help() + .argv; +}; + +main().then(() => { + process.exit(); +}).catch(err => { + log(err); +}); diff --git a/packages/ERC20TOT/src/cli/export-state.ts b/packages/ERC20TOT/src/cli/export-state.ts new file mode 100644 index 00000000..6af4a609 --- /dev/null +++ b/packages/ERC20TOT/src/cli/export-state.ts @@ -0,0 +1,28 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import 'reflect-metadata'; +import debug from 'debug'; + +import { ExportStateCmd } from '@cerc-io/cli'; + +import { Database } from '../database'; +import { Indexer } from '../indexer'; + +const log = debug('vulcanize:export-state'); + +const main = async (): Promise => { + const exportStateCmd = new ExportStateCmd(); + await exportStateCmd.init(Database); + + await exportStateCmd.initIndexer(Indexer); + + await exportStateCmd.exec(); +}; + +main().catch(err => { + log(err); +}).finally(() => { + process.exit(0); +}); diff --git a/packages/ERC20TOT/src/cli/import-state.ts b/packages/ERC20TOT/src/cli/import-state.ts new file mode 100644 index 00000000..fcc48024 --- /dev/null +++ b/packages/ERC20TOT/src/cli/import-state.ts @@ -0,0 +1,29 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import 'reflect-metadata'; +import debug from 'debug'; + +import { ImportStateCmd } from '@cerc-io/cli'; + +import { Database } from '../database'; +import { Indexer } from '../indexer'; +import { State } from '../entity/State'; + +const log = debug('vulcanize:import-state'); + +export const main = async (): Promise => { + const importStateCmd = new ImportStateCmd(); + await importStateCmd.init(Database); + + await importStateCmd.initIndexer(Indexer); + + await importStateCmd.exec(State); +}; + +main().catch(err => { + log(err); +}).finally(() => { + process.exit(0); +}); diff --git a/packages/ERC20TOT/src/cli/index-block.ts b/packages/ERC20TOT/src/cli/index-block.ts new file mode 100644 index 00000000..587c1cc1 --- /dev/null +++ b/packages/ERC20TOT/src/cli/index-block.ts @@ -0,0 +1,28 @@ +// +// Copyright 2022 Vulcanize, Inc. +// + +import 'reflect-metadata'; +import debug from 'debug'; + +import { IndexBlockCmd } from '@cerc-io/cli'; + +import { Database } from '../database'; +import { Indexer } from '../indexer'; + +const log = debug('vulcanize:index-block'); + +const main = async (): Promise => { + const indexBlockCmd = new IndexBlockCmd(); + await indexBlockCmd.init(Database); + + await indexBlockCmd.initIndexer(Indexer); + + await indexBlockCmd.exec(); +}; + +main().catch(err => { + log(err); +}).finally(() => { + process.exit(0); +}); diff --git a/packages/ERC20TOT/src/cli/inspect-cid.ts b/packages/ERC20TOT/src/cli/inspect-cid.ts new file mode 100644 index 00000000..3e93d1cf --- /dev/null +++ b/packages/ERC20TOT/src/cli/inspect-cid.ts @@ -0,0 +1,28 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import 'reflect-metadata'; +import debug from 'debug'; + +import { InspectCIDCmd } from '@cerc-io/cli'; + +import { Database } from '../database'; +import { Indexer } from '../indexer'; + +const log = debug('vulcanize:inspect-cid'); + +const main = async (): Promise => { + const inspectCIDCmd = new InspectCIDCmd(); + await inspectCIDCmd.init(Database); + + await inspectCIDCmd.initIndexer(Indexer); + + await inspectCIDCmd.exec(); +}; + +main().catch(err => { + log(err); +}).finally(() => { + process.exit(0); +}); diff --git a/packages/ERC20TOT/src/cli/reset-cmds/job-queue.ts b/packages/ERC20TOT/src/cli/reset-cmds/job-queue.ts new file mode 100644 index 00000000..c33cbfd6 --- /dev/null +++ b/packages/ERC20TOT/src/cli/reset-cmds/job-queue.ts @@ -0,0 +1,22 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import debug from 'debug'; + +import { getConfig, resetJobs, Config } from '@cerc-io/util'; + +const log = debug('vulcanize:reset-job-queue'); + +export const command = 'job-queue'; + +export const desc = 'Reset job queue'; + +export const builder = {}; + +export const handler = async (argv: any): Promise => { + const config: Config = await getConfig(argv.configFile); + await resetJobs(config); + + log('Job queue reset successfully'); +}; diff --git a/packages/ERC20TOT/src/cli/reset-cmds/state.ts b/packages/ERC20TOT/src/cli/reset-cmds/state.ts new file mode 100644 index 00000000..33211d6e --- /dev/null +++ b/packages/ERC20TOT/src/cli/reset-cmds/state.ts @@ -0,0 +1,24 @@ +// +// Copyright 2022 Vulcanize, Inc. +// + +import { ResetStateCmd } from '@cerc-io/cli'; + +import { Database } from '../../database'; + +export const command = 'state'; + +export const desc = 'Reset State to a given block number'; + +export const builder = { + blockNumber: { + type: 'number' + } +}; + +export const handler = async (argv: any): Promise => { + const resetStateCmd = new ResetStateCmd(); + await resetStateCmd.init(argv, Database); + + await resetStateCmd.exec(); +}; diff --git a/packages/ERC20TOT/src/cli/reset-cmds/watcher.ts b/packages/ERC20TOT/src/cli/reset-cmds/watcher.ts new file mode 100644 index 00000000..8e825d19 --- /dev/null +++ b/packages/ERC20TOT/src/cli/reset-cmds/watcher.ts @@ -0,0 +1,27 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { ResetWatcherCmd } from '@cerc-io/cli'; + +import { Database } from '../../database'; +import { Indexer } from '../../indexer'; + +export const command = 'watcher'; + +export const desc = 'Reset watcher to a block number'; + +export const builder = { + blockNumber: { + type: 'number' + } +}; + +export const handler = async (argv: any): Promise => { + const resetWatcherCmd = new ResetWatcherCmd(); + await resetWatcherCmd.init(argv, Database); + + await resetWatcherCmd.initIndexer(Indexer); + + await resetWatcherCmd.exec(); +}; diff --git a/packages/ERC20TOT/src/cli/reset.ts b/packages/ERC20TOT/src/cli/reset.ts new file mode 100644 index 00000000..95648c88 --- /dev/null +++ b/packages/ERC20TOT/src/cli/reset.ts @@ -0,0 +1,24 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import 'reflect-metadata'; +import debug from 'debug'; + +import { getResetYargs } from '@cerc-io/util'; + +const log = debug('vulcanize:reset'); + +const main = async () => { + return getResetYargs() + .commandDir('reset-cmds', { extensions: ['ts', 'js'], exclude: /([a-zA-Z0-9\s_\\.\-:])+(.d.ts)$/ }) + .demandCommand(1) + .help() + .argv; +}; + +main().then(() => { + process.exit(); +}).catch(err => { + log(err); +}); diff --git a/packages/ERC20TOT/src/cli/watch-contract.ts b/packages/ERC20TOT/src/cli/watch-contract.ts new file mode 100644 index 00000000..200a9e9f --- /dev/null +++ b/packages/ERC20TOT/src/cli/watch-contract.ts @@ -0,0 +1,28 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import 'reflect-metadata'; +import debug from 'debug'; + +import { WatchContractCmd } from '@cerc-io/cli'; + +import { Database } from '../database'; +import { Indexer } from '../indexer'; + +const log = debug('vulcanize:watch-contract'); + +const main = async (): Promise => { + const watchContractCmd = new WatchContractCmd(); + await watchContractCmd.init(Database); + + await watchContractCmd.initIndexer(Indexer); + + await watchContractCmd.exec(); +}; + +main().catch(err => { + log(err); +}).finally(() => { + process.exit(0); +}); diff --git a/packages/ERC20TOT/src/client.ts b/packages/ERC20TOT/src/client.ts new file mode 100644 index 00000000..7f59f6c6 --- /dev/null +++ b/packages/ERC20TOT/src/client.ts @@ -0,0 +1,154 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { gql } from '@apollo/client/core'; +import { GraphQLClient, GraphQLConfig } from '@cerc-io/ipld-eth-client'; + +import { queries, mutations, subscriptions } from './gql'; + +export class Client { + _config: GraphQLConfig; + _client: GraphQLClient; + + constructor (config: GraphQLConfig) { + this._config = config; + + this._client = new GraphQLClient(config); + } + + async getTotalSupply (blockHash: string, contractAddress: string): Promise { + const { totalSupply } = await this._client.query( + gql(queries.totalSupply), + { blockHash, contractAddress } + ); + + return totalSupply; + } + + async getBalanceOf (blockHash: string, contractAddress: string, account: string): Promise { + const { balanceOf } = await this._client.query( + gql(queries.balanceOf), + { blockHash, contractAddress, account } + ); + + return balanceOf; + } + + async getAllowance (blockHash: string, contractAddress: string, owner: string, spender: string): Promise { + const { allowance } = await this._client.query( + gql(queries.allowance), + { blockHash, contractAddress, owner, spender } + ); + + return allowance; + } + + async getName (blockHash: string, contractAddress: string): Promise { + const { name } = await this._client.query( + gql(queries.name), + { blockHash, contractAddress } + ); + + return name; + } + + async getSymbol (blockHash: string, contractAddress: string): Promise { + const { symbol } = await this._client.query( + gql(queries.symbol), + { blockHash, contractAddress } + ); + + return symbol; + } + + async getDecimals (blockHash: string, contractAddress: string): Promise { + const { decimals } = await this._client.query( + gql(queries.decimals), + { blockHash, contractAddress } + ); + + return decimals; + } + + async _getBalances (blockHash: string, contractAddress: string, key0: string): Promise { + const { _balances } = await this._client.query( + gql(queries._balances), + { blockHash, contractAddress, key0 } + ); + + return _balances; + } + + async _getAllowances (blockHash: string, contractAddress: string, key0: string, key1: string): Promise { + const { _allowances } = await this._client.query( + gql(queries._allowances), + { blockHash, contractAddress, key0, key1 } + ); + + return _allowances; + } + + async _getTotalSupply (blockHash: string, contractAddress: string): Promise { + const { _totalSupply } = await this._client.query( + gql(queries._totalSupply), + { blockHash, contractAddress } + ); + + return _totalSupply; + } + + async _getName (blockHash: string, contractAddress: string): Promise { + const { _name } = await this._client.query( + gql(queries._name), + { blockHash, contractAddress } + ); + + return _name; + } + + async _getSymbol (blockHash: string, contractAddress: string): Promise { + const { _symbol } = await this._client.query( + gql(queries._symbol), + { blockHash, contractAddress } + ); + + return _symbol; + } + + async getEvents (blockHash: string, contractAddress: string, name: string): Promise { + const { events } = await this._client.query( + gql(queries.events), + { blockHash, contractAddress, name } + ); + + return events; + } + + async getEventsInRange (fromBlockNumber: number, toBlockNumber: number): Promise { + const { eventsInRange } = await this._client.query( + gql(queries.eventsInRange), + { fromBlockNumber, toBlockNumber } + ); + + return eventsInRange; + } + + async watchContract (contractAddress: string, startingBlock?: number): Promise { + const { watchContract } = await this._client.mutate( + gql(mutations.watchContract), + { contractAddress, startingBlock } + ); + + return watchContract; + } + + async watchEvents (onNext: (value: any) => void): Promise { + return this._client.subscribe( + gql(subscriptions.onEvent), + ({ data }) => { + onNext(data.onEvent); + } + ); + } +} diff --git a/packages/ERC20TOT/src/database.ts b/packages/ERC20TOT/src/database.ts new file mode 100644 index 00000000..ce6782a2 --- /dev/null +++ b/packages/ERC20TOT/src/database.ts @@ -0,0 +1,447 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import assert from 'assert'; +import { Connection, ConnectionOptions, DeepPartial, FindConditions, QueryRunner, FindManyOptions, EntityTarget } from 'typeorm'; +import path from 'path'; + +import { + Database as BaseDatabase, + DatabaseInterface, + QueryOptions, + StateKind, + Where +} from '@cerc-io/util'; + +import { Contract } from './entity/Contract'; +import { Event } from './entity/Event'; +import { SyncStatus } from './entity/SyncStatus'; +import { StateSyncStatus } from './entity/StateSyncStatus'; +import { BlockProgress } from './entity/BlockProgress'; +import { State } from './entity/State'; +import { TotalSupply } from './entity/TotalSupply'; +import { BalanceOf } from './entity/BalanceOf'; +import { Allowance } from './entity/Allowance'; +import { Name } from './entity/Name'; +import { Symbol } from './entity/Symbol'; +import { Decimals } from './entity/Decimals'; +import { _Balances } from './entity/_Balances'; +import { _Allowances } from './entity/_Allowances'; +import { _TotalSupply } from './entity/_TotalSupply'; +import { _Name } from './entity/_Name'; +import { _Symbol } from './entity/_Symbol'; + +export const ENTITIES = [TotalSupply, BalanceOf, Allowance, Name, Symbol, Decimals, _Balances, _Allowances, _TotalSupply, _Name, _Symbol]; + +export class Database implements DatabaseInterface { + _config: ConnectionOptions; + _conn!: Connection; + _baseDatabase: BaseDatabase; + _propColMaps: { [key: string]: Map; }; + + constructor (config: ConnectionOptions) { + assert(config); + + this._config = { + ...config, + entities: [path.join(__dirname, 'entity/*')] + }; + + this._baseDatabase = new BaseDatabase(this._config); + this._propColMaps = {}; + } + + get baseDatabase (): BaseDatabase { + return this._baseDatabase; + } + + async init (): Promise { + this._conn = await this._baseDatabase.init(); + this._setPropColMaps(); + } + + async close (): Promise { + return this._baseDatabase.close(); + } + + async getTotalSupply ({ blockHash, contractAddress }: { blockHash: string, contractAddress: string }): Promise { + return this._conn.getRepository(TotalSupply) + .findOne({ + blockHash, + contractAddress + }); + } + + async getBalanceOf ({ blockHash, contractAddress, account }: { blockHash: string, contractAddress: string, account: string }): Promise { + return this._conn.getRepository(BalanceOf) + .findOne({ + blockHash, + contractAddress, + account + }); + } + + async getAllowance ({ blockHash, contractAddress, owner, spender }: { blockHash: string, contractAddress: string, owner: string, spender: string }): Promise { + return this._conn.getRepository(Allowance) + .findOne({ + blockHash, + contractAddress, + owner, + spender + }); + } + + async getName ({ blockHash, contractAddress }: { blockHash: string, contractAddress: string }): Promise { + return this._conn.getRepository(Name) + .findOne({ + blockHash, + contractAddress + }); + } + + // eslint-disable-next-line @typescript-eslint/ban-types + async getSymbol ({ blockHash, contractAddress }: { blockHash: string, contractAddress: string }): Promise { + return this._conn.getRepository(Symbol) + .findOne({ + blockHash, + contractAddress + }); + } + + async getDecimals ({ blockHash, contractAddress }: { blockHash: string, contractAddress: string }): Promise { + return this._conn.getRepository(Decimals) + .findOne({ + blockHash, + contractAddress + }); + } + + async _getBalances ({ blockHash, contractAddress, key0 }: { blockHash: string, contractAddress: string, key0: string }): Promise<_Balances | undefined> { + return this._conn.getRepository(_Balances) + .findOne({ + blockHash, + contractAddress, + key0 + }); + } + + async _getAllowances ({ blockHash, contractAddress, key0, key1 }: { blockHash: string, contractAddress: string, key0: string, key1: string }): Promise<_Allowances | undefined> { + return this._conn.getRepository(_Allowances) + .findOne({ + blockHash, + contractAddress, + key0, + key1 + }); + } + + async _getTotalSupply ({ blockHash, contractAddress }: { blockHash: string, contractAddress: string }): Promise<_TotalSupply | undefined> { + return this._conn.getRepository(_TotalSupply) + .findOne({ + blockHash, + contractAddress + }); + } + + async _getName ({ blockHash, contractAddress }: { blockHash: string, contractAddress: string }): Promise<_Name | undefined> { + return this._conn.getRepository(_Name) + .findOne({ + blockHash, + contractAddress + }); + } + + async _getSymbol ({ blockHash, contractAddress }: { blockHash: string, contractAddress: string }): Promise<_Symbol | undefined> { + return this._conn.getRepository(_Symbol) + .findOne({ + blockHash, + contractAddress + }); + } + + async saveTotalSupply ({ blockHash, blockNumber, contractAddress, value, proof }: DeepPartial): Promise { + const repo = this._conn.getRepository(TotalSupply); + const entity = repo.create({ blockHash, blockNumber, contractAddress, value, proof }); + return repo.save(entity); + } + + async saveBalanceOf ({ blockHash, blockNumber, contractAddress, account, value, proof }: DeepPartial): Promise { + const repo = this._conn.getRepository(BalanceOf); + const entity = repo.create({ blockHash, blockNumber, contractAddress, account, value, proof }); + return repo.save(entity); + } + + async saveAllowance ({ blockHash, blockNumber, contractAddress, owner, spender, value, proof }: DeepPartial): Promise { + const repo = this._conn.getRepository(Allowance); + const entity = repo.create({ blockHash, blockNumber, contractAddress, owner, spender, value, proof }); + return repo.save(entity); + } + + async saveName ({ blockHash, blockNumber, contractAddress, value, proof }: DeepPartial): Promise { + const repo = this._conn.getRepository(Name); + const entity = repo.create({ blockHash, blockNumber, contractAddress, value, proof }); + return repo.save(entity); + } + + // eslint-disable-next-line @typescript-eslint/ban-types + async saveSymbol ({ blockHash, blockNumber, contractAddress, value, proof }: DeepPartial): Promise { + const repo = this._conn.getRepository(Symbol); + const entity = repo.create({ blockHash, blockNumber, contractAddress, value, proof }); + return repo.save(entity); + } + + async saveDecimals ({ blockHash, blockNumber, contractAddress, value, proof }: DeepPartial): Promise { + const repo = this._conn.getRepository(Decimals); + const entity = repo.create({ blockHash, blockNumber, contractAddress, value, proof }); + return repo.save(entity); + } + + async _saveBalances ({ blockHash, blockNumber, contractAddress, key0, value, proof }: DeepPartial<_Balances>): Promise<_Balances> { + const repo = this._conn.getRepository(_Balances); + const entity = repo.create({ blockHash, blockNumber, contractAddress, key0, value, proof }); + return repo.save(entity); + } + + async _saveAllowances ({ blockHash, blockNumber, contractAddress, key0, key1, value, proof }: DeepPartial<_Allowances>): Promise<_Allowances> { + const repo = this._conn.getRepository(_Allowances); + const entity = repo.create({ blockHash, blockNumber, contractAddress, key0, key1, value, proof }); + return repo.save(entity); + } + + async _saveTotalSupply ({ blockHash, blockNumber, contractAddress, value, proof }: DeepPartial<_TotalSupply>): Promise<_TotalSupply> { + const repo = this._conn.getRepository(_TotalSupply); + const entity = repo.create({ blockHash, blockNumber, contractAddress, value, proof }); + return repo.save(entity); + } + + async _saveName ({ blockHash, blockNumber, contractAddress, value, proof }: DeepPartial<_Name>): Promise<_Name> { + const repo = this._conn.getRepository(_Name); + const entity = repo.create({ blockHash, blockNumber, contractAddress, value, proof }); + return repo.save(entity); + } + + async _saveSymbol ({ blockHash, blockNumber, contractAddress, value, proof }: DeepPartial<_Symbol>): Promise<_Symbol> { + const repo = this._conn.getRepository(_Symbol); + const entity = repo.create({ blockHash, blockNumber, contractAddress, value, proof }); + return repo.save(entity); + } + + getNewState (): State { + return new State(); + } + + async getStates (where: FindConditions): Promise { + const repo = this._conn.getRepository(State); + + return this._baseDatabase.getStates(repo, where); + } + + async getLatestState (contractAddress: string, kind: StateKind | null, blockNumber?: number): Promise { + const repo = this._conn.getRepository(State); + + return this._baseDatabase.getLatestState(repo, contractAddress, kind, blockNumber); + } + + async getPrevState (blockHash: string, contractAddress: string, kind?: string): Promise { + const repo = this._conn.getRepository(State); + + return this._baseDatabase.getPrevState(repo, blockHash, contractAddress, kind); + } + + // Fetch all diff States after the specified block number. + async getDiffStatesInRange (contractAddress: string, startblock: number, endBlock: number): Promise { + const repo = this._conn.getRepository(State); + + return this._baseDatabase.getDiffStatesInRange(repo, contractAddress, startblock, endBlock); + } + + async saveOrUpdateState (dbTx: QueryRunner, state: State): Promise { + const repo = dbTx.manager.getRepository(State); + + return this._baseDatabase.saveOrUpdateState(repo, state); + } + + async removeStates (dbTx: QueryRunner, blockNumber: number, kind: string): Promise { + const repo = dbTx.manager.getRepository(State); + + await this._baseDatabase.removeStates(repo, blockNumber, kind); + } + + async removeStatesAfterBlock (dbTx: QueryRunner, blockNumber: number): Promise { + const repo = dbTx.manager.getRepository(State); + + await this._baseDatabase.removeStatesAfterBlock(repo, blockNumber); + } + + async getStateSyncStatus (): Promise { + const repo = this._conn.getRepository(StateSyncStatus); + + return this._baseDatabase.getStateSyncStatus(repo); + } + + async updateStateSyncStatusIndexedBlock (queryRunner: QueryRunner, blockNumber: number, force?: boolean): Promise { + const repo = queryRunner.manager.getRepository(StateSyncStatus); + + return this._baseDatabase.updateStateSyncStatusIndexedBlock(repo, blockNumber, force); + } + + async updateStateSyncStatusCheckpointBlock (queryRunner: QueryRunner, blockNumber: number, force?: boolean): Promise { + const repo = queryRunner.manager.getRepository(StateSyncStatus); + + return this._baseDatabase.updateStateSyncStatusCheckpointBlock(repo, blockNumber, force); + } + + async getContracts (): Promise { + const repo = this._conn.getRepository(Contract); + + return this._baseDatabase.getContracts(repo); + } + + async createTransactionRunner (): Promise { + return this._baseDatabase.createTransactionRunner(); + } + + async getProcessedBlockCountForRange (fromBlockNumber: number, toBlockNumber: number): Promise<{ expected: number, actual: number }> { + const repo = this._conn.getRepository(BlockProgress); + + return this._baseDatabase.getProcessedBlockCountForRange(repo, fromBlockNumber, toBlockNumber); + } + + async getEventsInRange (fromBlockNumber: number, toBlockNumber: number): Promise> { + const repo = this._conn.getRepository(Event); + + return this._baseDatabase.getEventsInRange(repo, fromBlockNumber, toBlockNumber); + } + + async saveEventEntity (queryRunner: QueryRunner, entity: Event): Promise { + const repo = queryRunner.manager.getRepository(Event); + return this._baseDatabase.saveEventEntity(repo, entity); + } + + async getBlockEvents (blockHash: string, where: Where, queryOptions: QueryOptions): Promise { + const repo = this._conn.getRepository(Event); + + return this._baseDatabase.getBlockEvents(repo, blockHash, where, queryOptions); + } + + async saveBlockWithEvents (queryRunner: QueryRunner, block: DeepPartial, events: DeepPartial[]): Promise { + const blockRepo = queryRunner.manager.getRepository(BlockProgress); + const eventRepo = queryRunner.manager.getRepository(Event); + + return this._baseDatabase.saveBlockWithEvents(blockRepo, eventRepo, block, events); + } + + async saveEvents (queryRunner: QueryRunner, events: Event[]): Promise { + const eventRepo = queryRunner.manager.getRepository(Event); + + return this._baseDatabase.saveEvents(eventRepo, events); + } + + async saveBlockProgress (queryRunner: QueryRunner, block: DeepPartial): Promise { + const repo = queryRunner.manager.getRepository(BlockProgress); + + return this._baseDatabase.saveBlockProgress(repo, block); + } + + async saveContract (queryRunner: QueryRunner, address: string, kind: string, checkpoint: boolean, startingBlock: number): Promise { + const repo = queryRunner.manager.getRepository(Contract); + + return this._baseDatabase.saveContract(repo, address, kind, checkpoint, startingBlock); + } + + async updateSyncStatusIndexedBlock (queryRunner: QueryRunner, blockHash: string, blockNumber: number, force = false): Promise { + const repo = queryRunner.manager.getRepository(SyncStatus); + + return this._baseDatabase.updateSyncStatusIndexedBlock(repo, blockHash, blockNumber, force); + } + + async updateSyncStatusCanonicalBlock (queryRunner: QueryRunner, blockHash: string, blockNumber: number, force = false): Promise { + const repo = queryRunner.manager.getRepository(SyncStatus); + + return this._baseDatabase.updateSyncStatusCanonicalBlock(repo, blockHash, blockNumber, force); + } + + async updateSyncStatusChainHead (queryRunner: QueryRunner, blockHash: string, blockNumber: number, force = false): Promise { + const repo = queryRunner.manager.getRepository(SyncStatus); + + return this._baseDatabase.updateSyncStatusChainHead(repo, blockHash, blockNumber, force); + } + + async getSyncStatus (queryRunner: QueryRunner): Promise { + const repo = queryRunner.manager.getRepository(SyncStatus); + + return this._baseDatabase.getSyncStatus(repo); + } + + async getEvent (id: string): Promise { + const repo = this._conn.getRepository(Event); + + return this._baseDatabase.getEvent(repo, id); + } + + async getBlocksAtHeight (height: number, isPruned: boolean): Promise { + const repo = this._conn.getRepository(BlockProgress); + + return this._baseDatabase.getBlocksAtHeight(repo, height, isPruned); + } + + async markBlocksAsPruned (queryRunner: QueryRunner, blocks: BlockProgress[]): Promise { + const repo = queryRunner.manager.getRepository(BlockProgress); + + return this._baseDatabase.markBlocksAsPruned(repo, blocks); + } + + async getBlockProgress (blockHash: string): Promise { + const repo = this._conn.getRepository(BlockProgress); + return this._baseDatabase.getBlockProgress(repo, blockHash); + } + + async getBlockProgressEntities (where: FindConditions, options: FindManyOptions): Promise { + const repo = this._conn.getRepository(BlockProgress); + + return this._baseDatabase.getBlockProgressEntities(repo, where, options); + } + + async getEntitiesForBlock (blockHash: string, tableName: string): Promise { + return this._baseDatabase.getEntitiesForBlock(blockHash, tableName); + } + + async updateBlockProgress (queryRunner: QueryRunner, block: BlockProgress, lastProcessedEventIndex: number): Promise { + const repo = queryRunner.manager.getRepository(BlockProgress); + + return this._baseDatabase.updateBlockProgress(repo, block, lastProcessedEventIndex); + } + + async removeEntities (queryRunner: QueryRunner, entity: new () => Entity, findConditions?: FindManyOptions | FindConditions): Promise { + return this._baseDatabase.removeEntities(queryRunner, entity, findConditions); + } + + async deleteEntitiesByConditions (queryRunner: QueryRunner, entity: EntityTarget, findConditions: FindConditions): Promise { + await this._baseDatabase.deleteEntitiesByConditions(queryRunner, entity, findConditions); + } + + async getAncestorAtDepth (blockHash: string, depth: number): Promise { + return this._baseDatabase.getAncestorAtDepth(blockHash, depth); + } + + _getPropertyColumnMapForEntity (entityName: string): Map { + return this._conn.getMetadata(entityName).ownColumns.reduce((acc, curr) => { + return acc.set(curr.propertyName, curr.databaseName); + }, new Map()); + } + + _setPropColMaps (): void { + this._propColMaps.TotalSupply = this._getPropertyColumnMapForEntity('TotalSupply'); + this._propColMaps.BalanceOf = this._getPropertyColumnMapForEntity('BalanceOf'); + this._propColMaps.Allowance = this._getPropertyColumnMapForEntity('Allowance'); + this._propColMaps.Name = this._getPropertyColumnMapForEntity('Name'); + this._propColMaps.Symbol = this._getPropertyColumnMapForEntity('Symbol'); + this._propColMaps.Decimals = this._getPropertyColumnMapForEntity('Decimals'); + this._propColMaps._Balances = this._getPropertyColumnMapForEntity('_Balances'); + this._propColMaps._Allowances = this._getPropertyColumnMapForEntity('_Allowances'); + this._propColMaps._TotalSupply = this._getPropertyColumnMapForEntity('_TotalSupply'); + this._propColMaps._Name = this._getPropertyColumnMapForEntity('_Name'); + this._propColMaps._Symbol = this._getPropertyColumnMapForEntity('_Symbol'); + } +} diff --git a/packages/ERC20TOT/src/entity/Allowance.ts b/packages/ERC20TOT/src/entity/Allowance.ts new file mode 100644 index 00000000..f58b2123 --- /dev/null +++ b/packages/ERC20TOT/src/entity/Allowance.ts @@ -0,0 +1,34 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm'; +import { bigintTransformer } from '@cerc-io/util'; + +@Entity() +@Index(['blockHash', 'contractAddress', 'owner', 'spender'], { unique: true }) +export class Allowance { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar', { length: 66 }) + blockHash!: string; + + @Column('integer') + blockNumber!: number; + + @Column('varchar', { length: 42 }) + contractAddress!: string; + + @Column('varchar', { length: 42 }) + owner!: string; + + @Column('varchar', { length: 42 }) + spender!: string; + + @Column('numeric', { transformer: bigintTransformer }) + value!: bigint; + + @Column('text', { nullable: true }) + proof!: string; +} diff --git a/packages/ERC20TOT/src/entity/BalanceOf.ts b/packages/ERC20TOT/src/entity/BalanceOf.ts new file mode 100644 index 00000000..437371fd --- /dev/null +++ b/packages/ERC20TOT/src/entity/BalanceOf.ts @@ -0,0 +1,31 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm'; +import { bigintTransformer } from '@cerc-io/util'; + +@Entity() +@Index(['blockHash', 'contractAddress', 'account'], { unique: true }) +export class BalanceOf { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar', { length: 66 }) + blockHash!: string; + + @Column('integer') + blockNumber!: number; + + @Column('varchar', { length: 42 }) + contractAddress!: string; + + @Column('varchar', { length: 42 }) + account!: string; + + @Column('numeric', { transformer: bigintTransformer }) + value!: bigint; + + @Column('text', { nullable: true }) + proof!: string; +} diff --git a/packages/ERC20TOT/src/entity/BlockProgress.ts b/packages/ERC20TOT/src/entity/BlockProgress.ts new file mode 100644 index 00000000..e744aae4 --- /dev/null +++ b/packages/ERC20TOT/src/entity/BlockProgress.ts @@ -0,0 +1,48 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index, CreateDateColumn } from 'typeorm'; +import { BlockProgressInterface } from '@cerc-io/util'; + +@Entity() +@Index(['blockHash'], { unique: true }) +@Index(['blockNumber']) +@Index(['parentHash']) +export class BlockProgress implements BlockProgressInterface { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar') + cid!: string; + + @Column('varchar', { length: 66 }) + blockHash!: string; + + @Column('varchar', { length: 66 }) + parentHash!: string; + + @Column('integer') + blockNumber!: number; + + @Column('integer') + blockTimestamp!: number; + + @Column('integer') + numEvents!: number; + + @Column('integer') + numProcessedEvents!: number; + + @Column('integer') + lastProcessedEventIndex!: number; + + @Column('boolean') + isComplete!: boolean; + + @Column('boolean', { default: false }) + isPruned!: boolean; + + @CreateDateColumn() + createdAt!: Date; +} diff --git a/packages/ERC20TOT/src/entity/Contract.ts b/packages/ERC20TOT/src/entity/Contract.ts new file mode 100644 index 00000000..de66b387 --- /dev/null +++ b/packages/ERC20TOT/src/entity/Contract.ts @@ -0,0 +1,24 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm'; + +@Entity() +@Index(['address'], { unique: true }) +export class Contract { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar', { length: 42 }) + address!: string; + + @Column('varchar') + kind!: string; + + @Column('boolean') + checkpoint!: boolean; + + @Column('integer') + startingBlock!: number; +} diff --git a/packages/ERC20TOT/src/entity/Decimals.ts b/packages/ERC20TOT/src/entity/Decimals.ts new file mode 100644 index 00000000..e55367ff --- /dev/null +++ b/packages/ERC20TOT/src/entity/Decimals.ts @@ -0,0 +1,27 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm'; + +@Entity() +@Index(['blockHash', 'contractAddress'], { unique: true }) +export class Decimals { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar', { length: 66 }) + blockHash!: string; + + @Column('integer') + blockNumber!: number; + + @Column('varchar', { length: 42 }) + contractAddress!: string; + + @Column('integer') + value!: number; + + @Column('text', { nullable: true }) + proof!: string; +} diff --git a/packages/ERC20TOT/src/entity/Event.ts b/packages/ERC20TOT/src/entity/Event.ts new file mode 100644 index 00000000..91f1e6d3 --- /dev/null +++ b/packages/ERC20TOT/src/entity/Event.ts @@ -0,0 +1,38 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index, ManyToOne } from 'typeorm'; +import { BlockProgress } from './BlockProgress'; + +@Entity() +@Index(['block', 'contract']) +@Index(['block', 'contract', 'eventName']) +export class Event { + @PrimaryGeneratedColumn() + id!: number; + + @ManyToOne(() => BlockProgress, { onDelete: 'CASCADE' }) + block!: BlockProgress; + + @Column('varchar', { length: 66 }) + txHash!: string; + + @Column('integer') + index!: number; + + @Column('varchar', { length: 42 }) + contract!: string; + + @Column('varchar', { length: 256 }) + eventName!: string; + + @Column('text') + eventInfo!: string; + + @Column('text') + extraInfo!: string; + + @Column('text') + proof!: string; +} diff --git a/packages/ERC20TOT/src/entity/Name.ts b/packages/ERC20TOT/src/entity/Name.ts new file mode 100644 index 00000000..c24431cc --- /dev/null +++ b/packages/ERC20TOT/src/entity/Name.ts @@ -0,0 +1,27 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm'; + +@Entity() +@Index(['blockHash', 'contractAddress'], { unique: true }) +export class Name { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar', { length: 66 }) + blockHash!: string; + + @Column('integer') + blockNumber!: number; + + @Column('varchar', { length: 42 }) + contractAddress!: string; + + @Column('varchar') + value!: string; + + @Column('text', { nullable: true }) + proof!: string; +} diff --git a/packages/ERC20TOT/src/entity/State.ts b/packages/ERC20TOT/src/entity/State.ts new file mode 100644 index 00000000..bc05bca1 --- /dev/null +++ b/packages/ERC20TOT/src/entity/State.ts @@ -0,0 +1,31 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index, ManyToOne } from 'typeorm'; +import { StateKind } from '@cerc-io/util'; +import { BlockProgress } from './BlockProgress'; + +@Entity() +@Index(['cid'], { unique: true }) +@Index(['block', 'contractAddress']) +@Index(['block', 'contractAddress', 'kind'], { unique: true }) +export class State { + @PrimaryGeneratedColumn() + id!: number; + + @ManyToOne(() => BlockProgress, { onDelete: 'CASCADE' }) + block!: BlockProgress; + + @Column('varchar', { length: 42 }) + contractAddress!: string; + + @Column('varchar') + cid!: string; + + @Column({ type: 'enum', enum: StateKind }) + kind!: StateKind; + + @Column('bytea') + data!: Buffer; +} diff --git a/packages/ERC20TOT/src/entity/StateSyncStatus.ts b/packages/ERC20TOT/src/entity/StateSyncStatus.ts new file mode 100644 index 00000000..c795212f --- /dev/null +++ b/packages/ERC20TOT/src/entity/StateSyncStatus.ts @@ -0,0 +1,17 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; + +@Entity() +export class StateSyncStatus { + @PrimaryGeneratedColumn() + id!: number; + + @Column('integer') + latestIndexedBlockNumber!: number; + + @Column('integer', { nullable: true }) + latestCheckpointBlockNumber!: number; +} diff --git a/packages/ERC20TOT/src/entity/Symbol.ts b/packages/ERC20TOT/src/entity/Symbol.ts new file mode 100644 index 00000000..e80126af --- /dev/null +++ b/packages/ERC20TOT/src/entity/Symbol.ts @@ -0,0 +1,27 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm'; + +@Entity() +@Index(['blockHash', 'contractAddress'], { unique: true }) +export class Symbol { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar', { length: 66 }) + blockHash!: string; + + @Column('integer') + blockNumber!: number; + + @Column('varchar', { length: 42 }) + contractAddress!: string; + + @Column('varchar') + value!: string; + + @Column('text', { nullable: true }) + proof!: string; +} diff --git a/packages/ERC20TOT/src/entity/SyncStatus.ts b/packages/ERC20TOT/src/entity/SyncStatus.ts new file mode 100644 index 00000000..19d0dfa7 --- /dev/null +++ b/packages/ERC20TOT/src/entity/SyncStatus.ts @@ -0,0 +1,36 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; +import { SyncStatusInterface } from '@cerc-io/util'; + +@Entity() +export class SyncStatus implements SyncStatusInterface { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar', { length: 66 }) + chainHeadBlockHash!: string; + + @Column('integer') + chainHeadBlockNumber!: number; + + @Column('varchar', { length: 66 }) + latestIndexedBlockHash!: string; + + @Column('integer') + latestIndexedBlockNumber!: number; + + @Column('varchar', { length: 66 }) + latestCanonicalBlockHash!: string; + + @Column('integer') + latestCanonicalBlockNumber!: number; + + @Column('varchar', { length: 66 }) + initialIndexedBlockHash!: string; + + @Column('integer') + initialIndexedBlockNumber!: number; +} diff --git a/packages/ERC20TOT/src/entity/TotalSupply.ts b/packages/ERC20TOT/src/entity/TotalSupply.ts new file mode 100644 index 00000000..867c01ae --- /dev/null +++ b/packages/ERC20TOT/src/entity/TotalSupply.ts @@ -0,0 +1,28 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm'; +import { bigintTransformer } from '@cerc-io/util'; + +@Entity() +@Index(['blockHash', 'contractAddress'], { unique: true }) +export class TotalSupply { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar', { length: 66 }) + blockHash!: string; + + @Column('integer') + blockNumber!: number; + + @Column('varchar', { length: 42 }) + contractAddress!: string; + + @Column('numeric', { transformer: bigintTransformer }) + value!: bigint; + + @Column('text', { nullable: true }) + proof!: string; +} diff --git a/packages/ERC20TOT/src/entity/_Allowances.ts b/packages/ERC20TOT/src/entity/_Allowances.ts new file mode 100644 index 00000000..8dbb2766 --- /dev/null +++ b/packages/ERC20TOT/src/entity/_Allowances.ts @@ -0,0 +1,34 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm'; +import { bigintTransformer } from '@cerc-io/util'; + +@Entity() +@Index(['blockHash', 'contractAddress', 'key0', 'key1'], { unique: true }) +export class _Allowances { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar', { length: 66 }) + blockHash!: string; + + @Column('integer') + blockNumber!: number; + + @Column('varchar', { length: 42 }) + contractAddress!: string; + + @Column('varchar', { length: 42 }) + key0!: string; + + @Column('varchar', { length: 42 }) + key1!: string; + + @Column('numeric', { transformer: bigintTransformer }) + value!: bigint; + + @Column('text', { nullable: true }) + proof!: string; +} diff --git a/packages/ERC20TOT/src/entity/_Balances.ts b/packages/ERC20TOT/src/entity/_Balances.ts new file mode 100644 index 00000000..559c77a3 --- /dev/null +++ b/packages/ERC20TOT/src/entity/_Balances.ts @@ -0,0 +1,31 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm'; +import { bigintTransformer } from '@cerc-io/util'; + +@Entity() +@Index(['blockHash', 'contractAddress', 'key0'], { unique: true }) +export class _Balances { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar', { length: 66 }) + blockHash!: string; + + @Column('integer') + blockNumber!: number; + + @Column('varchar', { length: 42 }) + contractAddress!: string; + + @Column('varchar', { length: 42 }) + key0!: string; + + @Column('numeric', { transformer: bigintTransformer }) + value!: bigint; + + @Column('text', { nullable: true }) + proof!: string; +} diff --git a/packages/ERC20TOT/src/entity/_Name.ts b/packages/ERC20TOT/src/entity/_Name.ts new file mode 100644 index 00000000..5c6ba24a --- /dev/null +++ b/packages/ERC20TOT/src/entity/_Name.ts @@ -0,0 +1,27 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm'; + +@Entity() +@Index(['blockHash', 'contractAddress'], { unique: true }) +export class _Name { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar', { length: 66 }) + blockHash!: string; + + @Column('integer') + blockNumber!: number; + + @Column('varchar', { length: 42 }) + contractAddress!: string; + + @Column('varchar') + value!: string; + + @Column('text', { nullable: true }) + proof!: string; +} diff --git a/packages/ERC20TOT/src/entity/_Symbol.ts b/packages/ERC20TOT/src/entity/_Symbol.ts new file mode 100644 index 00000000..b3643d83 --- /dev/null +++ b/packages/ERC20TOT/src/entity/_Symbol.ts @@ -0,0 +1,27 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm'; + +@Entity() +@Index(['blockHash', 'contractAddress'], { unique: true }) +export class _Symbol { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar', { length: 66 }) + blockHash!: string; + + @Column('integer') + blockNumber!: number; + + @Column('varchar', { length: 42 }) + contractAddress!: string; + + @Column('varchar') + value!: string; + + @Column('text', { nullable: true }) + proof!: string; +} diff --git a/packages/ERC20TOT/src/entity/_TotalSupply.ts b/packages/ERC20TOT/src/entity/_TotalSupply.ts new file mode 100644 index 00000000..97b85cc5 --- /dev/null +++ b/packages/ERC20TOT/src/entity/_TotalSupply.ts @@ -0,0 +1,28 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { Entity, PrimaryGeneratedColumn, Column, Index } from 'typeorm'; +import { bigintTransformer } from '@cerc-io/util'; + +@Entity() +@Index(['blockHash', 'contractAddress'], { unique: true }) +export class _TotalSupply { + @PrimaryGeneratedColumn() + id!: number; + + @Column('varchar', { length: 66 }) + blockHash!: string; + + @Column('integer') + blockNumber!: number; + + @Column('varchar', { length: 42 }) + contractAddress!: string; + + @Column('numeric', { transformer: bigintTransformer }) + value!: bigint; + + @Column('text', { nullable: true }) + proof!: string; +} diff --git a/packages/ERC20TOT/src/fill.ts b/packages/ERC20TOT/src/fill.ts new file mode 100644 index 00000000..4db5b46a --- /dev/null +++ b/packages/ERC20TOT/src/fill.ts @@ -0,0 +1,33 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import 'reflect-metadata'; +import debug from 'debug'; + +import { FillCmd } from '@cerc-io/cli'; + +import { Database } from './database'; +import { Indexer } from './indexer'; + +const log = debug('vulcanize:fill'); + +export const main = async (): Promise => { + const fillCmd = new FillCmd(); + await fillCmd.init(Database); + + await fillCmd.initIndexer(Indexer); + + await fillCmd.exec(); +}; + +main().catch(err => { + log(err); +}).finally(() => { + process.exit(); +}); + +process.on('SIGINT', () => { + log(`Exiting process ${process.pid} with code 0`); + process.exit(0); +}); diff --git a/packages/ERC20TOT/src/gql/index.ts b/packages/ERC20TOT/src/gql/index.ts new file mode 100644 index 00000000..4732f682 --- /dev/null +++ b/packages/ERC20TOT/src/gql/index.ts @@ -0,0 +1,3 @@ +export * as mutations from './mutations'; +export * as queries from './queries'; +export * as subscriptions from './subscriptions'; diff --git a/packages/ERC20TOT/src/gql/mutations/index.ts b/packages/ERC20TOT/src/gql/mutations/index.ts new file mode 100644 index 00000000..0c3bd853 --- /dev/null +++ b/packages/ERC20TOT/src/gql/mutations/index.ts @@ -0,0 +1,4 @@ +import fs from 'fs'; +import path from 'path'; + +export const watchContract = fs.readFileSync(path.join(__dirname, 'watchContract.gql'), 'utf8'); diff --git a/packages/ERC20TOT/src/gql/mutations/watchContract.gql b/packages/ERC20TOT/src/gql/mutations/watchContract.gql new file mode 100644 index 00000000..2ecc74f7 --- /dev/null +++ b/packages/ERC20TOT/src/gql/mutations/watchContract.gql @@ -0,0 +1,3 @@ +mutation watchContract($address: String!, $kind: String!, $checkpoint: Boolean!, $startingBlock: Int){ + watchContract(address: $address, kind: $kind, checkpoint: $checkpoint, startingBlock: $startingBlock) +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/_allowances.gql b/packages/ERC20TOT/src/gql/queries/_allowances.gql new file mode 100644 index 00000000..8ed5aa98 --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/_allowances.gql @@ -0,0 +1,8 @@ +query _allowances($blockHash: String!, $contractAddress: String!, $key0: String!, $key1: String!){ + _allowances(blockHash: $blockHash, contractAddress: $contractAddress, key0: $key0, key1: $key1){ + value + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/_balances.gql b/packages/ERC20TOT/src/gql/queries/_balances.gql new file mode 100644 index 00000000..2dd7805e --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/_balances.gql @@ -0,0 +1,8 @@ +query _balances($blockHash: String!, $contractAddress: String!, $key0: String!){ + _balances(blockHash: $blockHash, contractAddress: $contractAddress, key0: $key0){ + value + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/_name.gql b/packages/ERC20TOT/src/gql/queries/_name.gql new file mode 100644 index 00000000..58ba0554 --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/_name.gql @@ -0,0 +1,8 @@ +query _name($blockHash: String!, $contractAddress: String!){ + _name(blockHash: $blockHash, contractAddress: $contractAddress){ + value + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/_symbol.gql b/packages/ERC20TOT/src/gql/queries/_symbol.gql new file mode 100644 index 00000000..a5794794 --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/_symbol.gql @@ -0,0 +1,8 @@ +query _symbol($blockHash: String!, $contractAddress: String!){ + _symbol(blockHash: $blockHash, contractAddress: $contractAddress){ + value + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/_totalSupply.gql b/packages/ERC20TOT/src/gql/queries/_totalSupply.gql new file mode 100644 index 00000000..524c6d22 --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/_totalSupply.gql @@ -0,0 +1,8 @@ +query _totalSupply($blockHash: String!, $contractAddress: String!){ + _totalSupply(blockHash: $blockHash, contractAddress: $contractAddress){ + value + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/allowance.gql b/packages/ERC20TOT/src/gql/queries/allowance.gql new file mode 100644 index 00000000..af86aab5 --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/allowance.gql @@ -0,0 +1,8 @@ +query allowance($blockHash: String!, $contractAddress: String!, $owner: String!, $spender: String!){ + allowance(blockHash: $blockHash, contractAddress: $contractAddress, owner: $owner, spender: $spender){ + value + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/balanceOf.gql b/packages/ERC20TOT/src/gql/queries/balanceOf.gql new file mode 100644 index 00000000..a2c060c3 --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/balanceOf.gql @@ -0,0 +1,8 @@ +query balanceOf($blockHash: String!, $contractAddress: String!, $account: String!){ + balanceOf(blockHash: $blockHash, contractAddress: $contractAddress, account: $account){ + value + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/decimals.gql b/packages/ERC20TOT/src/gql/queries/decimals.gql new file mode 100644 index 00000000..c24b2302 --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/decimals.gql @@ -0,0 +1,8 @@ +query decimals($blockHash: String!, $contractAddress: String!){ + decimals(blockHash: $blockHash, contractAddress: $contractAddress){ + value + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/events.gql b/packages/ERC20TOT/src/gql/queries/events.gql new file mode 100644 index 00000000..480060fe --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/events.gql @@ -0,0 +1,34 @@ +query events($blockHash: String!, $contractAddress: String!, $name: String){ + events(blockHash: $blockHash, contractAddress: $contractAddress, name: $name){ + block{ + cid + hash + number + timestamp + parentHash + } + tx{ + hash + index + from + to + } + contract + eventIndex + event{ + ... on ApprovalEvent { + owner + spender + value + } + ... on TransferEvent { + from + to + value + } + } + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/eventsInRange.gql b/packages/ERC20TOT/src/gql/queries/eventsInRange.gql new file mode 100644 index 00000000..4f88ce8f --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/eventsInRange.gql @@ -0,0 +1,34 @@ +query eventsInRange($fromBlockNumber: Int!, $toBlockNumber: Int!){ + eventsInRange(fromBlockNumber: $fromBlockNumber, toBlockNumber: $toBlockNumber){ + block{ + cid + hash + number + timestamp + parentHash + } + tx{ + hash + index + from + to + } + contract + eventIndex + event{ + ... on ApprovalEvent { + owner + spender + value + } + ... on TransferEvent { + from + to + value + } + } + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/getState.gql b/packages/ERC20TOT/src/gql/queries/getState.gql new file mode 100644 index 00000000..3b8f6050 --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/getState.gql @@ -0,0 +1,15 @@ +query getState($blockHash: String!, $contractAddress: String!, $kind: String){ + getState(blockHash: $blockHash, contractAddress: $contractAddress, kind: $kind){ + block{ + cid + hash + number + timestamp + parentHash + } + contractAddress + cid + kind + data + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/getStateByCID.gql b/packages/ERC20TOT/src/gql/queries/getStateByCID.gql new file mode 100644 index 00000000..6c3c4fd8 --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/getStateByCID.gql @@ -0,0 +1,15 @@ +query getStateByCID($cid: String!){ + getStateByCID(cid: $cid){ + block{ + cid + hash + number + timestamp + parentHash + } + contractAddress + cid + kind + data + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/getSyncStatus.gql b/packages/ERC20TOT/src/gql/queries/getSyncStatus.gql new file mode 100644 index 00000000..2f80efdf --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/getSyncStatus.gql @@ -0,0 +1,8 @@ +query getSyncStatus{ + getSyncStatus{ + latestIndexedBlockHash + latestIndexedBlockNumber + latestCanonicalBlockHash + latestCanonicalBlockNumber + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/index.ts b/packages/ERC20TOT/src/gql/queries/index.ts new file mode 100644 index 00000000..c8eb8b18 --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/index.ts @@ -0,0 +1,19 @@ +import fs from 'fs'; +import path from 'path'; + +export const events = fs.readFileSync(path.join(__dirname, 'events.gql'), 'utf8'); +export const eventsInRange = fs.readFileSync(path.join(__dirname, 'eventsInRange.gql'), 'utf8'); +export const totalSupply = fs.readFileSync(path.join(__dirname, 'totalSupply.gql'), 'utf8'); +export const balanceOf = fs.readFileSync(path.join(__dirname, 'balanceOf.gql'), 'utf8'); +export const allowance = fs.readFileSync(path.join(__dirname, 'allowance.gql'), 'utf8'); +export const name = fs.readFileSync(path.join(__dirname, 'name.gql'), 'utf8'); +export const symbol = fs.readFileSync(path.join(__dirname, 'symbol.gql'), 'utf8'); +export const decimals = fs.readFileSync(path.join(__dirname, 'decimals.gql'), 'utf8'); +export const _balances = fs.readFileSync(path.join(__dirname, '_balances.gql'), 'utf8'); +export const _allowances = fs.readFileSync(path.join(__dirname, '_allowances.gql'), 'utf8'); +export const _totalSupply = fs.readFileSync(path.join(__dirname, '_totalSupply.gql'), 'utf8'); +export const _name = fs.readFileSync(path.join(__dirname, '_name.gql'), 'utf8'); +export const _symbol = fs.readFileSync(path.join(__dirname, '_symbol.gql'), 'utf8'); +export const getSyncStatus = fs.readFileSync(path.join(__dirname, 'getSyncStatus.gql'), 'utf8'); +export const getStateByCID = fs.readFileSync(path.join(__dirname, 'getStateByCID.gql'), 'utf8'); +export const getState = fs.readFileSync(path.join(__dirname, 'getState.gql'), 'utf8'); diff --git a/packages/ERC20TOT/src/gql/queries/name.gql b/packages/ERC20TOT/src/gql/queries/name.gql new file mode 100644 index 00000000..0431f0d6 --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/name.gql @@ -0,0 +1,8 @@ +query name($blockHash: String!, $contractAddress: String!){ + name(blockHash: $blockHash, contractAddress: $contractAddress){ + value + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/symbol.gql b/packages/ERC20TOT/src/gql/queries/symbol.gql new file mode 100644 index 00000000..fa278b80 --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/symbol.gql @@ -0,0 +1,8 @@ +query symbol($blockHash: String!, $contractAddress: String!){ + symbol(blockHash: $blockHash, contractAddress: $contractAddress){ + value + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/queries/totalSupply.gql b/packages/ERC20TOT/src/gql/queries/totalSupply.gql new file mode 100644 index 00000000..d1be1e4a --- /dev/null +++ b/packages/ERC20TOT/src/gql/queries/totalSupply.gql @@ -0,0 +1,8 @@ +query totalSupply($blockHash: String!, $contractAddress: String!){ + totalSupply(blockHash: $blockHash, contractAddress: $contractAddress){ + value + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/gql/subscriptions/index.ts b/packages/ERC20TOT/src/gql/subscriptions/index.ts new file mode 100644 index 00000000..f12910c5 --- /dev/null +++ b/packages/ERC20TOT/src/gql/subscriptions/index.ts @@ -0,0 +1,4 @@ +import fs from 'fs'; +import path from 'path'; + +export const onEvent = fs.readFileSync(path.join(__dirname, 'onEvent.gql'), 'utf8'); diff --git a/packages/ERC20TOT/src/gql/subscriptions/onEvent.gql b/packages/ERC20TOT/src/gql/subscriptions/onEvent.gql new file mode 100644 index 00000000..23e0479d --- /dev/null +++ b/packages/ERC20TOT/src/gql/subscriptions/onEvent.gql @@ -0,0 +1,34 @@ +subscription onEvent{ + onEvent{ + block{ + cid + hash + number + timestamp + parentHash + } + tx{ + hash + index + from + to + } + contract + eventIndex + event{ + ... on ApprovalEvent { + owner + spender + value + } + ... on TransferEvent { + from + to + value + } + } + proof{ + data + } + } +} \ No newline at end of file diff --git a/packages/ERC20TOT/src/hooks.ts b/packages/ERC20TOT/src/hooks.ts new file mode 100644 index 00000000..77d99109 --- /dev/null +++ b/packages/ERC20TOT/src/hooks.ts @@ -0,0 +1,80 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import assert from 'assert'; + +import { updateStateForMappingType, updateStateForElementaryType, ResultEvent } from '@cerc-io/util'; + +import { Indexer } from './indexer'; + +/** + * Hook function to store an initial state. + * @param indexer Indexer instance. + * @param blockHash Hash of the concerned block. + * @param contractAddress Address of the concerned contract. + * @returns Data block to be stored. + */ +export async function createInitialState (indexer: Indexer, contractAddress: string, blockHash: string): Promise { + assert(indexer); + assert(blockHash); + assert(contractAddress); + + // Store an empty State. + const stateData: any = { + state: {} + }; + + // Use updateStateForElementaryType to update initial state with an elementary property. + // Eg. const stateData = updateStateForElementaryType(stateData, '_totalBalance', result.value.toString()); + + // Use updateStateForMappingType to update initial state with a nested property. + // Eg. const stateData = updateStateForMappingType(stateData, '_allowances', [owner, spender], allowance.value.toString()); + + // Return initial state data to be saved. + return stateData; +} + +/** + * Hook function to create state diff. + * @param indexer Indexer instance that contains methods to fetch the contract variable values. + * @param blockHash Block hash of the concerned block. + */ +export async function createStateDiff (indexer: Indexer, blockHash: string): Promise { + assert(indexer); + assert(blockHash); + + // Use indexer.createDiff() method to save custom state diff(s). +} + +/** + * Hook function to create state checkpoint + * @param indexer Indexer instance. + * @param contractAddress Address of the concerned contract. + * @param blockHash Block hash of the concerned block. + * @returns Whether to disable default checkpoint. If false, the state from this hook is updated with that from default checkpoint. + */ +export async function createStateCheckpoint (indexer: Indexer, contractAddress: string, blockHash: string): Promise { + assert(indexer); + assert(blockHash); + assert(contractAddress); + + // Use indexer.createStateCheckpoint() method to create a custom checkpoint. + + // Return false to update the state created by this hook by auto-generated checkpoint state. + // Return true to disable update of the state created by this hook by auto-generated checkpoint state. + return false; +} + +/** + * Event hook function. + * @param indexer Indexer instance that contains methods to fetch and update the contract values in the database. + * @param eventData ResultEvent object containing event information. + */ +export async function handleEvent (indexer: Indexer, eventData: ResultEvent): Promise { + assert(indexer); + assert(eventData); + + // Use indexer methods to index data. + // Pass `diff` parameter to indexer methods as true to save an auto-generated state from the indexed data. +} diff --git a/packages/ERC20TOT/src/indexer.ts b/packages/ERC20TOT/src/indexer.ts new file mode 100644 index 00000000..78d4fec9 --- /dev/null +++ b/packages/ERC20TOT/src/indexer.ts @@ -0,0 +1,799 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import assert from 'assert'; +import debug from 'debug'; +import { DeepPartial, FindConditions, FindManyOptions, ObjectLiteral } from 'typeorm'; +import JSONbig from 'json-bigint'; +import { ethers } from 'ethers'; + +import { JsonFragment } from '@ethersproject/abi'; +import { BaseProvider } from '@ethersproject/providers'; +import { EthClient } from '@cerc-io/ipld-eth-client'; +import { MappingKey, StorageLayout } from '@cerc-io/solidity-mapper'; +import { + Indexer as BaseIndexer, + IndexerInterface, + ValueResult, + ServerConfig, + JobQueue, + Where, + QueryOptions, + updateStateForElementaryType, + updateStateForMappingType, + StateKind, + StateStatus, + ResultEvent, + getResultEvent, + DatabaseInterface, + Clients +} from '@cerc-io/util'; + +import ERC20Artifacts from './artifacts/ERC20.json'; +import { Database, ENTITIES } from './database'; +import { createInitialState, handleEvent, createStateDiff, createStateCheckpoint } from './hooks'; +import { Contract } from './entity/Contract'; +import { Event } from './entity/Event'; +import { SyncStatus } from './entity/SyncStatus'; +import { StateSyncStatus } from './entity/StateSyncStatus'; +import { BlockProgress } from './entity/BlockProgress'; +import { State } from './entity/State'; + +const log = debug('vulcanize:indexer'); +const JSONbigNative = JSONbig({ useNativeBigInt: true }); + +const KIND_ERC20 = 'ERC20'; + +export class Indexer implements IndexerInterface { + _db: Database; + _ethClient: EthClient; + _ethProvider: BaseProvider; + _baseIndexer: BaseIndexer; + _serverConfig: ServerConfig; + + _abiMap: Map; + _storageLayoutMap: Map; + _contractMap: Map; + + constructor (serverConfig: ServerConfig, db: DatabaseInterface, clients: Clients, ethProvider: BaseProvider, jobQueue: JobQueue) { + assert(db); + assert(clients.ethClient); + + this._db = db as Database; + this._ethClient = clients.ethClient; + this._ethProvider = ethProvider; + this._serverConfig = serverConfig; + this._baseIndexer = new BaseIndexer(this._serverConfig, this._db, this._ethClient, this._ethProvider, jobQueue); + + this._abiMap = new Map(); + this._storageLayoutMap = new Map(); + this._contractMap = new Map(); + + const { abi: ERC20ABI, storageLayout: ERC20StorageLayout } = ERC20Artifacts; + + assert(ERC20ABI); + this._abiMap.set(KIND_ERC20, ERC20ABI); + assert(ERC20StorageLayout); + this._storageLayoutMap.set(KIND_ERC20, ERC20StorageLayout); + this._contractMap.set(KIND_ERC20, new ethers.utils.Interface(ERC20ABI)); + } + + get serverConfig (): ServerConfig { + return this._serverConfig; + } + + get storageLayoutMap (): Map { + return this._storageLayoutMap; + } + + async init (): Promise { + await this._baseIndexer.fetchContracts(); + await this._baseIndexer.fetchStateStatus(); + } + + getResultEvent (event: Event): ResultEvent { + return getResultEvent(event); + } + + async totalSupply (blockHash: string, contractAddress: string): Promise { + const entity = await this._db.getTotalSupply({ blockHash, contractAddress }); + if (entity) { + log('totalSupply: db hit.'); + + return { + value: entity.value, + proof: JSON.parse(entity.proof) + }; + } + + log('totalSupply: db miss, fetching from upstream server'); + + const { block: { number } } = await this._ethClient.getBlockByHash(blockHash); + const blockNumber = ethers.BigNumber.from(number).toNumber(); + + const abi = this._abiMap.get(KIND_ERC20); + assert(abi); + + const contract = new ethers.Contract(contractAddress, abi, this._ethProvider); + let value = await contract.totalSupply({ blockTag: blockHash }); + value = value.toString(); + value = BigInt(value); + + const result: ValueResult = { value }; + + await this._db.saveTotalSupply({ blockHash, blockNumber, contractAddress, value: result.value, proof: JSONbigNative.stringify(result.proof) }); + + return result; + } + + async balanceOf (blockHash: string, contractAddress: string, account: string): Promise { + const entity = await this._db.getBalanceOf({ blockHash, contractAddress, account }); + if (entity) { + log('balanceOf: db hit.'); + + return { + value: entity.value, + proof: JSON.parse(entity.proof) + }; + } + + log('balanceOf: db miss, fetching from upstream server'); + + const { block: { number } } = await this._ethClient.getBlockByHash(blockHash); + const blockNumber = ethers.BigNumber.from(number).toNumber(); + + const abi = this._abiMap.get(KIND_ERC20); + assert(abi); + + const contract = new ethers.Contract(contractAddress, abi, this._ethProvider); + let value = await contract.balanceOf(account, { blockTag: blockHash }); + value = value.toString(); + value = BigInt(value); + + const result: ValueResult = { value }; + + await this._db.saveBalanceOf({ blockHash, blockNumber, contractAddress, account, value: result.value, proof: JSONbigNative.stringify(result.proof) }); + + return result; + } + + async allowance (blockHash: string, contractAddress: string, owner: string, spender: string): Promise { + const entity = await this._db.getAllowance({ blockHash, contractAddress, owner, spender }); + if (entity) { + log('allowance: db hit.'); + + return { + value: entity.value, + proof: JSON.parse(entity.proof) + }; + } + + log('allowance: db miss, fetching from upstream server'); + + const { block: { number } } = await this._ethClient.getBlockByHash(blockHash); + const blockNumber = ethers.BigNumber.from(number).toNumber(); + + const abi = this._abiMap.get(KIND_ERC20); + assert(abi); + + const contract = new ethers.Contract(contractAddress, abi, this._ethProvider); + let value = await contract.allowance(owner, spender, { blockTag: blockHash }); + value = value.toString(); + value = BigInt(value); + + const result: ValueResult = { value }; + + await this._db.saveAllowance({ blockHash, blockNumber, contractAddress, owner, spender, value: result.value, proof: JSONbigNative.stringify(result.proof) }); + + return result; + } + + async name (blockHash: string, contractAddress: string): Promise { + const entity = await this._db.getName({ blockHash, contractAddress }); + if (entity) { + log('name: db hit.'); + + return { + value: entity.value, + proof: JSON.parse(entity.proof) + }; + } + + log('name: db miss, fetching from upstream server'); + + const { block: { number } } = await this._ethClient.getBlockByHash(blockHash); + const blockNumber = ethers.BigNumber.from(number).toNumber(); + + const abi = this._abiMap.get(KIND_ERC20); + assert(abi); + + const contract = new ethers.Contract(contractAddress, abi, this._ethProvider); + const value = await contract.name({ blockTag: blockHash }); + + const result: ValueResult = { value }; + + await this._db.saveName({ blockHash, blockNumber, contractAddress, value: result.value, proof: JSONbigNative.stringify(result.proof) }); + + return result; + } + + async symbol (blockHash: string, contractAddress: string): Promise { + const entity = await this._db.getSymbol({ blockHash, contractAddress }); + if (entity) { + log('symbol: db hit.'); + + return { + value: entity.value, + proof: JSON.parse(entity.proof) + }; + } + + log('symbol: db miss, fetching from upstream server'); + + const { block: { number } } = await this._ethClient.getBlockByHash(blockHash); + const blockNumber = ethers.BigNumber.from(number).toNumber(); + + const abi = this._abiMap.get(KIND_ERC20); + assert(abi); + + const contract = new ethers.Contract(contractAddress, abi, this._ethProvider); + const value = await contract.symbol({ blockTag: blockHash }); + + const result: ValueResult = { value }; + + await this._db.saveSymbol({ blockHash, blockNumber, contractAddress, value: result.value, proof: JSONbigNative.stringify(result.proof) }); + + return result; + } + + async decimals (blockHash: string, contractAddress: string): Promise { + const entity = await this._db.getDecimals({ blockHash, contractAddress }); + if (entity) { + log('decimals: db hit.'); + + return { + value: entity.value, + proof: JSON.parse(entity.proof) + }; + } + + log('decimals: db miss, fetching from upstream server'); + + const { block: { number } } = await this._ethClient.getBlockByHash(blockHash); + const blockNumber = ethers.BigNumber.from(number).toNumber(); + + const abi = this._abiMap.get(KIND_ERC20); + assert(abi); + + const contract = new ethers.Contract(contractAddress, abi, this._ethProvider); + const value = await contract.decimals({ blockTag: blockHash }); + + const result: ValueResult = { value }; + + await this._db.saveDecimals({ blockHash, blockNumber, contractAddress, value: result.value, proof: JSONbigNative.stringify(result.proof) }); + + return result; + } + + async _balances (blockHash: string, contractAddress: string, key0: string, diff = false): Promise { + const entity = await this._db._getBalances({ blockHash, contractAddress, key0 }); + if (entity) { + log('_balances: db hit.'); + + return { + value: entity.value, + proof: JSON.parse(entity.proof) + }; + } + + log('_balances: db miss, fetching from upstream server'); + + const { block: { number } } = await this._ethClient.getBlockByHash(blockHash); + const blockNumber = ethers.BigNumber.from(number).toNumber(); + + const storageLayout = this._storageLayoutMap.get(KIND_ERC20); + assert(storageLayout); + + const result = await this._baseIndexer.getStorageValue( + storageLayout, + blockHash, + contractAddress, + '_balances', + key0 + ); + + await this._db._saveBalances({ blockHash, blockNumber, contractAddress, key0, value: result.value, proof: JSONbigNative.stringify(result.proof) }); + + if (diff) { + const stateUpdate = updateStateForMappingType({}, '_balances', [key0.toString()], result.value.toString()); + await this.createDiffStaged(contractAddress, blockHash, stateUpdate); + } + + return result; + } + + async _allowances (blockHash: string, contractAddress: string, key0: string, key1: string, diff = false): Promise { + const entity = await this._db._getAllowances({ blockHash, contractAddress, key0, key1 }); + if (entity) { + log('_allowances: db hit.'); + + return { + value: entity.value, + proof: JSON.parse(entity.proof) + }; + } + + log('_allowances: db miss, fetching from upstream server'); + + const { block: { number } } = await this._ethClient.getBlockByHash(blockHash); + const blockNumber = ethers.BigNumber.from(number).toNumber(); + + const storageLayout = this._storageLayoutMap.get(KIND_ERC20); + assert(storageLayout); + + const result = await this._baseIndexer.getStorageValue( + storageLayout, + blockHash, + contractAddress, + '_allowances', + key0, + key1 + ); + + await this._db._saveAllowances({ blockHash, blockNumber, contractAddress, key0, key1, value: result.value, proof: JSONbigNative.stringify(result.proof) }); + + if (diff) { + const stateUpdate = updateStateForMappingType({}, '_allowances', [key0.toString(), key1.toString()], result.value.toString()); + await this.createDiffStaged(contractAddress, blockHash, stateUpdate); + } + + return result; + } + + async _totalSupply (blockHash: string, contractAddress: string, diff = false): Promise { + const entity = await this._db._getTotalSupply({ blockHash, contractAddress }); + if (entity) { + log('_totalSupply: db hit.'); + + return { + value: entity.value, + proof: JSON.parse(entity.proof) + }; + } + + log('_totalSupply: db miss, fetching from upstream server'); + + const { block: { number } } = await this._ethClient.getBlockByHash(blockHash); + const blockNumber = ethers.BigNumber.from(number).toNumber(); + + const storageLayout = this._storageLayoutMap.get(KIND_ERC20); + assert(storageLayout); + + const result = await this._baseIndexer.getStorageValue( + storageLayout, + blockHash, + contractAddress, + '_totalSupply' + ); + + await this._db._saveTotalSupply({ blockHash, blockNumber, contractAddress, value: result.value, proof: JSONbigNative.stringify(result.proof) }); + + if (diff) { + const stateUpdate = updateStateForElementaryType({}, '_totalSupply', result.value.toString()); + await this.createDiffStaged(contractAddress, blockHash, stateUpdate); + } + + return result; + } + + async _name (blockHash: string, contractAddress: string, diff = false): Promise { + const entity = await this._db._getName({ blockHash, contractAddress }); + if (entity) { + log('_name: db hit.'); + + return { + value: entity.value, + proof: JSON.parse(entity.proof) + }; + } + + log('_name: db miss, fetching from upstream server'); + + const { block: { number } } = await this._ethClient.getBlockByHash(blockHash); + const blockNumber = ethers.BigNumber.from(number).toNumber(); + + const storageLayout = this._storageLayoutMap.get(KIND_ERC20); + assert(storageLayout); + + const result = await this._baseIndexer.getStorageValue( + storageLayout, + blockHash, + contractAddress, + '_name' + ); + + await this._db._saveName({ blockHash, blockNumber, contractAddress, value: result.value, proof: JSONbigNative.stringify(result.proof) }); + + if (diff) { + const stateUpdate = updateStateForElementaryType({}, '_name', result.value.toString()); + await this.createDiffStaged(contractAddress, blockHash, stateUpdate); + } + + return result; + } + + async _symbol (blockHash: string, contractAddress: string, diff = false): Promise { + const entity = await this._db._getSymbol({ blockHash, contractAddress }); + if (entity) { + log('_symbol: db hit.'); + + return { + value: entity.value, + proof: JSON.parse(entity.proof) + }; + } + + log('_symbol: db miss, fetching from upstream server'); + + const { block: { number } } = await this._ethClient.getBlockByHash(blockHash); + const blockNumber = ethers.BigNumber.from(number).toNumber(); + + const storageLayout = this._storageLayoutMap.get(KIND_ERC20); + assert(storageLayout); + + const result = await this._baseIndexer.getStorageValue( + storageLayout, + blockHash, + contractAddress, + '_symbol' + ); + + await this._db._saveSymbol({ blockHash, blockNumber, contractAddress, value: result.value, proof: JSONbigNative.stringify(result.proof) }); + + if (diff) { + const stateUpdate = updateStateForElementaryType({}, '_symbol', result.value.toString()); + await this.createDiffStaged(contractAddress, blockHash, stateUpdate); + } + + return result; + } + + async getStorageValue (storageLayout: StorageLayout, blockHash: string, contractAddress: string, variable: string, ...mappingKeys: MappingKey[]): Promise { + return this._baseIndexer.getStorageValue( + storageLayout, + blockHash, + contractAddress, + variable, + ...mappingKeys + ); + } + + async getEntitiesForBlock (blockHash: string, tableName: string): Promise { + return this._db.getEntitiesForBlock(blockHash, tableName); + } + + async processInitialState (contractAddress: string, blockHash: string): Promise { + // Call initial state hook. + return createInitialState(this, contractAddress, blockHash); + } + + async processStateCheckpoint (contractAddress: string, blockHash: string): Promise { + // Call checkpoint hook. + return createStateCheckpoint(this, contractAddress, blockHash); + } + + async processCanonicalBlock (blockHash: string, blockNumber: number): Promise { + console.time('time:indexer#processCanonicalBlock-finalize_auto_diffs'); + // Finalize staged diff blocks if any. + await this._baseIndexer.finalizeDiffStaged(blockHash); + console.timeEnd('time:indexer#processCanonicalBlock-finalize_auto_diffs'); + + // Call custom stateDiff hook. + await createStateDiff(this, blockHash); + } + + async processCheckpoint (blockHash: string): Promise { + // Return if checkpointInterval is <= 0. + const checkpointInterval = this._serverConfig.checkpointInterval; + if (checkpointInterval <= 0) return; + + console.time('time:indexer#processCheckpoint-checkpoint'); + await this._baseIndexer.processCheckpoint(this, blockHash, checkpointInterval); + console.timeEnd('time:indexer#processCheckpoint-checkpoint'); + } + + async processCLICheckpoint (contractAddress: string, blockHash?: string): Promise { + return this._baseIndexer.processCLICheckpoint(this, contractAddress, blockHash); + } + + async getPrevState (blockHash: string, contractAddress: string, kind?: string): Promise { + return this._db.getPrevState(blockHash, contractAddress, kind); + } + + async getLatestState (contractAddress: string, kind: StateKind | null, blockNumber?: number): Promise { + return this._db.getLatestState(contractAddress, kind, blockNumber); + } + + async getStatesByHash (blockHash: string): Promise { + return this._baseIndexer.getStatesByHash(blockHash); + } + + async getStateByCID (cid: string): Promise { + return this._baseIndexer.getStateByCID(cid); + } + + async getStates (where: FindConditions): Promise { + return this._db.getStates(where); + } + + getStateData (state: State): any { + return this._baseIndexer.getStateData(state); + } + + // Method used to create auto diffs (diff_staged). + async createDiffStaged (contractAddress: string, blockHash: string, data: any): Promise { + console.time('time:indexer#createDiffStaged-auto_diff'); + await this._baseIndexer.createDiffStaged(contractAddress, blockHash, data); + console.timeEnd('time:indexer#createDiffStaged-auto_diff'); + } + + // Method to be used by createStateDiff hook. + async createDiff (contractAddress: string, blockHash: string, data: any): Promise { + const block = await this.getBlockProgress(blockHash); + assert(block); + + await this._baseIndexer.createDiff(contractAddress, block, data); + } + + // Method to be used by createStateCheckpoint hook. + async createStateCheckpoint (contractAddress: string, blockHash: string, data: any): Promise { + const block = await this.getBlockProgress(blockHash); + assert(block); + + return this._baseIndexer.createStateCheckpoint(contractAddress, block, data); + } + + // Method to be used by export-state CLI. + async createCheckpoint (contractAddress: string, blockHash: string): Promise { + const block = await this.getBlockProgress(blockHash); + assert(block); + + return this._baseIndexer.createCheckpoint(this, contractAddress, block); + } + + async saveOrUpdateState (state: State): Promise { + return this._baseIndexer.saveOrUpdateState(state); + } + + async removeStates (blockNumber: number, kind: StateKind): Promise { + await this._baseIndexer.removeStates(blockNumber, kind); + } + + async triggerIndexingOnEvent (event: Event): Promise { + const resultEvent = this.getResultEvent(event); + + // Call custom hook function for indexing on event. + await handleEvent(this, resultEvent); + } + + async processEvent (event: Event): Promise { + // Trigger indexing of data based on the event. + await this.triggerIndexingOnEvent(event); + } + + async processBlock (blockProgress: BlockProgress): Promise { + console.time('time:indexer#processBlock-init_state'); + // Call a function to create initial state for contracts. + await this._baseIndexer.createInit(this, blockProgress.blockHash, blockProgress.blockNumber); + console.timeEnd('time:indexer#processBlock-init_state'); + } + + parseEventNameAndArgs (kind: string, logObj: any): any { + const { topics, data } = logObj; + + const contract = this._contractMap.get(kind); + assert(contract); + + const logDescription = contract.parseLog({ data, topics }); + + const { eventName, eventInfo, eventSignature } = this._baseIndexer.parseEvent(logDescription); + + return { + eventName, + eventInfo, + eventSignature + }; + } + + async getStateSyncStatus (): Promise { + return this._db.getStateSyncStatus(); + } + + async updateStateSyncStatusIndexedBlock (blockNumber: number, force?: boolean): Promise { + const dbTx = await this._db.createTransactionRunner(); + let res; + + try { + res = await this._db.updateStateSyncStatusIndexedBlock(dbTx, blockNumber, force); + await dbTx.commitTransaction(); + } catch (error) { + await dbTx.rollbackTransaction(); + throw error; + } finally { + await dbTx.release(); + } + + return res; + } + + async updateStateSyncStatusCheckpointBlock (blockNumber: number, force?: boolean): Promise { + const dbTx = await this._db.createTransactionRunner(); + let res; + + try { + res = await this._db.updateStateSyncStatusCheckpointBlock(dbTx, blockNumber, force); + await dbTx.commitTransaction(); + } catch (error) { + await dbTx.rollbackTransaction(); + throw error; + } finally { + await dbTx.release(); + } + + return res; + } + + async getLatestCanonicalBlock (): Promise { + const syncStatus = await this.getSyncStatus(); + assert(syncStatus); + + const latestCanonicalBlock = await this.getBlockProgress(syncStatus.latestCanonicalBlockHash); + assert(latestCanonicalBlock); + + return latestCanonicalBlock; + } + + async getLatestStateIndexedBlock (): Promise { + return this._baseIndexer.getLatestStateIndexedBlock(); + } + + async watchContract (address: string, kind: string, checkpoint: boolean, startingBlock: number): Promise { + return this._baseIndexer.watchContract(address, kind, checkpoint, startingBlock); + } + + updateStateStatusMap (address: string, stateStatus: StateStatus): void { + this._baseIndexer.updateStateStatusMap(address, stateStatus); + } + + cacheContract (contract: Contract): void { + return this._baseIndexer.cacheContract(contract); + } + + async saveEventEntity (dbEvent: Event): Promise { + return this._baseIndexer.saveEventEntity(dbEvent); + } + + async getEventsByFilter (blockHash: string, contract?: string, name?: string): Promise> { + return this._baseIndexer.getEventsByFilter(blockHash, contract, name); + } + + isWatchedContract (address : string): Contract | undefined { + return this._baseIndexer.isWatchedContract(address); + } + + getContractsByKind (kind: string): Contract[] { + return this._baseIndexer.getContractsByKind(kind); + } + + async getProcessedBlockCountForRange (fromBlockNumber: number, toBlockNumber: number): Promise<{ expected: number, actual: number }> { + return this._baseIndexer.getProcessedBlockCountForRange(fromBlockNumber, toBlockNumber); + } + + async getEventsInRange (fromBlockNumber: number, toBlockNumber: number): Promise> { + return this._baseIndexer.getEventsInRange(fromBlockNumber, toBlockNumber, this._serverConfig.maxEventsBlockRange); + } + + async getSyncStatus (): Promise { + return this._baseIndexer.getSyncStatus(); + } + + async getBlocks (blockFilter: { blockHash?: string, blockNumber?: number }): Promise { + return this._baseIndexer.getBlocks(blockFilter); + } + + async updateSyncStatusIndexedBlock (blockHash: string, blockNumber: number, force = false): Promise { + return this._baseIndexer.updateSyncStatusIndexedBlock(blockHash, blockNumber, force); + } + + async updateSyncStatusChainHead (blockHash: string, blockNumber: number, force = false): Promise { + return this._baseIndexer.updateSyncStatusChainHead(blockHash, blockNumber, force); + } + + async updateSyncStatusCanonicalBlock (blockHash: string, blockNumber: number, force = false): Promise { + const syncStatus = this._baseIndexer.updateSyncStatusCanonicalBlock(blockHash, blockNumber, force); + + return syncStatus; + } + + async getEvent (id: string): Promise { + return this._baseIndexer.getEvent(id); + } + + async getBlockProgress (blockHash: string): Promise { + return this._baseIndexer.getBlockProgress(blockHash); + } + + async getBlockProgressEntities (where: FindConditions, options: FindManyOptions): Promise { + return this._baseIndexer.getBlockProgressEntities(where, options); + } + + async getBlocksAtHeight (height: number, isPruned: boolean): Promise { + return this._baseIndexer.getBlocksAtHeight(height, isPruned); + } + + async saveBlockAndFetchEvents (block: DeepPartial): Promise<[BlockProgress, DeepPartial[]]> { + return this._saveBlockAndFetchEvents(block); + } + + async getBlockEvents (blockHash: string, where: Where, queryOptions: QueryOptions): Promise> { + return this._baseIndexer.getBlockEvents(blockHash, where, queryOptions); + } + + async removeUnknownEvents (block: BlockProgress): Promise { + return this._baseIndexer.removeUnknownEvents(Event, block); + } + + async markBlocksAsPruned (blocks: BlockProgress[]): Promise { + await this._baseIndexer.markBlocksAsPruned(blocks); + } + + async updateBlockProgress (block: BlockProgress, lastProcessedEventIndex: number): Promise { + return this._baseIndexer.updateBlockProgress(block, lastProcessedEventIndex); + } + + async getAncestorAtDepth (blockHash: string, depth: number): Promise { + return this._baseIndexer.getAncestorAtDepth(blockHash, depth); + } + + async resetWatcherToBlock (blockNumber: number): Promise { + const entities = [...ENTITIES]; + await this._baseIndexer.resetWatcherToBlock(blockNumber, entities); + } + + async _saveBlockAndFetchEvents ({ + cid: blockCid, + blockHash, + blockNumber, + blockTimestamp, + parentHash + }: DeepPartial): Promise<[BlockProgress, DeepPartial[]]> { + assert(blockHash); + assert(blockNumber); + + const dbEvents = await this._baseIndexer.fetchEvents(blockHash, blockNumber, this.parseEventNameAndArgs.bind(this)); + + const dbTx = await this._db.createTransactionRunner(); + try { + const block = { + cid: blockCid, + blockHash, + blockNumber, + blockTimestamp, + parentHash + }; + + console.time(`time:indexer#_saveBlockAndFetchEvents-db-save-${blockNumber}`); + const blockProgress = await this._db.saveBlockWithEvents(dbTx, block, dbEvents); + await dbTx.commitTransaction(); + console.timeEnd(`time:indexer#_saveBlockAndFetchEvents-db-save-${blockNumber}`); + + return [blockProgress, []]; + } catch (error) { + await dbTx.rollbackTransaction(); + throw error; + } finally { + await dbTx.release(); + } + } +} diff --git a/packages/ERC20TOT/src/job-runner.ts b/packages/ERC20TOT/src/job-runner.ts new file mode 100644 index 00000000..27cfa691 --- /dev/null +++ b/packages/ERC20TOT/src/job-runner.ts @@ -0,0 +1,37 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import debug from 'debug'; + +import { JobRunnerCmd } from '@cerc-io/cli'; +import { JobRunner } from '@cerc-io/util'; + +import { Indexer } from './indexer'; +import { Database } from './database'; + +const log = debug('vulcanize:job-runner'); + +export const main = async (): Promise => { + const jobRunnerCmd = new JobRunnerCmd(); + await jobRunnerCmd.init(Database); + + await jobRunnerCmd.initIndexer(Indexer); + + await jobRunnerCmd.exec(async (jobRunner: JobRunner): Promise => { + await jobRunner.subscribeBlockProcessingQueue(); + await jobRunner.subscribeEventProcessingQueue(); + await jobRunner.subscribeBlockCheckpointQueue(); + await jobRunner.subscribeHooksQueue(); + }); +}; + +main().then(() => { + log('Starting job runner...'); +}).catch(err => { + log(err); +}); + +process.on('uncaughtException', err => { + log('uncaughtException', err); +}); diff --git a/packages/ERC20TOT/src/resolvers.ts b/packages/ERC20TOT/src/resolvers.ts new file mode 100644 index 00000000..fe0bfb00 --- /dev/null +++ b/packages/ERC20TOT/src/resolvers.ts @@ -0,0 +1,305 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import assert from 'assert'; +import BigInt from 'apollo-type-bigint'; +import debug from 'debug'; +import Decimal from 'decimal.js'; +import { GraphQLResolveInfo, GraphQLScalarType } from 'graphql'; + +import { + ValueResult, + BlockHeight, + gqlTotalQueryCount, + gqlQueryCount, + jsonBigIntStringReplacer, + getResultState, + setGQLCacheHints, + IndexerInterface, + EventWatcher +} from '@cerc-io/util'; + +import { Indexer } from './indexer'; + +const log = debug('vulcanize:resolver'); + +export const createResolvers = async (indexerArg: IndexerInterface, eventWatcher: EventWatcher): Promise => { + const indexer = indexerArg as Indexer; + + const gqlCacheConfig = indexer.serverConfig.gqlCache; + + return { + BigInt: new BigInt('bigInt'), + + BigDecimal: new GraphQLScalarType({ + name: 'BigDecimal', + description: 'BigDecimal custom scalar type', + parseValue (value) { + // value from the client + return new Decimal(value); + }, + serialize (value: Decimal) { + // value sent to the client + return value.toFixed(); + } + }), + + Event: { + __resolveType: (obj: any) => { + assert(obj.__typename); + + return obj.__typename; + } + }, + + Subscription: { + onEvent: { + subscribe: () => eventWatcher.getEventIterator() + } + }, + + Mutation: { + watchContract: async (_: any, { address, kind, checkpoint, startingBlock = 1 }: { address: string, kind: string, checkpoint: boolean, startingBlock: number }): Promise => { + log('watchContract', address, kind, checkpoint, startingBlock); + await indexer.watchContract(address, kind, checkpoint, startingBlock); + + return true; + } + }, + + Query: { + totalSupply: ( + _: any, + { blockHash, contractAddress }: { blockHash: string, contractAddress: string }, + __: any, + info: GraphQLResolveInfo + ): Promise => { + log('totalSupply', blockHash, contractAddress); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('totalSupply').inc(1); + + // Set cache-control hints + // setGQLCacheHints(info, {}, gqlCacheConfig); + + return indexer.totalSupply(blockHash, contractAddress); + }, + + balanceOf: ( + _: any, + { blockHash, contractAddress, account }: { blockHash: string, contractAddress: string, account: string }, + __: any, + info: GraphQLResolveInfo + ): Promise => { + log('balanceOf', blockHash, contractAddress, account); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('balanceOf').inc(1); + + // Set cache-control hints + // setGQLCacheHints(info, {}, gqlCacheConfig); + + return indexer.balanceOf(blockHash, contractAddress, account); + }, + + allowance: ( + _: any, + { blockHash, contractAddress, owner, spender }: { blockHash: string, contractAddress: string, owner: string, spender: string }, + __: any, + info: GraphQLResolveInfo + ): Promise => { + log('allowance', blockHash, contractAddress, owner, spender); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('allowance').inc(1); + + // Set cache-control hints + // setGQLCacheHints(info, {}, gqlCacheConfig); + + return indexer.allowance(blockHash, contractAddress, owner, spender); + }, + + name: ( + _: any, + { blockHash, contractAddress }: { blockHash: string, contractAddress: string }, + __: any, + info: GraphQLResolveInfo + ): Promise => { + log('name', blockHash, contractAddress); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('name').inc(1); + + // Set cache-control hints + // setGQLCacheHints(info, {}, gqlCacheConfig); + + return indexer.name(blockHash, contractAddress); + }, + + symbol: ( + _: any, + { blockHash, contractAddress }: { blockHash: string, contractAddress: string }, + __: any, + info: GraphQLResolveInfo + ): Promise => { + log('symbol', blockHash, contractAddress); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('symbol').inc(1); + + // Set cache-control hints + // setGQLCacheHints(info, {}, gqlCacheConfig); + + return indexer.symbol(blockHash, contractAddress); + }, + + decimals: ( + _: any, + { blockHash, contractAddress }: { blockHash: string, contractAddress: string }, + __: any, + info: GraphQLResolveInfo + ): Promise => { + log('decimals', blockHash, contractAddress); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('decimals').inc(1); + + // Set cache-control hints + // setGQLCacheHints(info, {}, gqlCacheConfig); + + return indexer.decimals(blockHash, contractAddress); + }, + + _balances: ( + _: any, + { blockHash, contractAddress, key0 }: { blockHash: string, contractAddress: string, key0: string }, + __: any, + info: GraphQLResolveInfo + ): Promise => { + log('_balances', blockHash, contractAddress, key0); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('_balances').inc(1); + + // Set cache-control hints + // setGQLCacheHints(info, {}, gqlCacheConfig); + + return indexer._balances(blockHash, contractAddress, key0); + }, + + _allowances: ( + _: any, + { blockHash, contractAddress, key0, key1 }: { blockHash: string, contractAddress: string, key0: string, key1: string }, + __: any, + info: GraphQLResolveInfo + ): Promise => { + log('_allowances', blockHash, contractAddress, key0, key1); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('_allowances').inc(1); + + // Set cache-control hints + // setGQLCacheHints(info, {}, gqlCacheConfig); + + return indexer._allowances(blockHash, contractAddress, key0, key1); + }, + + _totalSupply: ( + _: any, + { blockHash, contractAddress }: { blockHash: string, contractAddress: string }, + __: any, + info: GraphQLResolveInfo + ): Promise => { + log('_totalSupply', blockHash, contractAddress); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('_totalSupply').inc(1); + + // Set cache-control hints + // setGQLCacheHints(info, {}, gqlCacheConfig); + + return indexer._totalSupply(blockHash, contractAddress); + }, + + _name: ( + _: any, + { blockHash, contractAddress }: { blockHash: string, contractAddress: string }, + __: any, + info: GraphQLResolveInfo + ): Promise => { + log('_name', blockHash, contractAddress); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('_name').inc(1); + + // Set cache-control hints + // setGQLCacheHints(info, {}, gqlCacheConfig); + + return indexer._name(blockHash, contractAddress); + }, + + _symbol: ( + _: any, + { blockHash, contractAddress }: { blockHash: string, contractAddress: string }, + __: any, + info: GraphQLResolveInfo + ): Promise => { + log('_symbol', blockHash, contractAddress); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('_symbol').inc(1); + + // Set cache-control hints + // setGQLCacheHints(info, {}, gqlCacheConfig); + + return indexer._symbol(blockHash, contractAddress); + }, + + events: async (_: any, { blockHash, contractAddress, name }: { blockHash: string, contractAddress: string, name?: string }) => { + log('events', blockHash, contractAddress, name); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('events').inc(1); + + const block = await indexer.getBlockProgress(blockHash); + if (!block || !block.isComplete) { + throw new Error(`Block hash ${blockHash} number ${block?.blockNumber} not processed yet`); + } + + const events = await indexer.getEventsByFilter(blockHash, contractAddress, name); + return events.map(event => indexer.getResultEvent(event)); + }, + + eventsInRange: async (_: any, { fromBlockNumber, toBlockNumber }: { fromBlockNumber: number, toBlockNumber: number }) => { + log('eventsInRange', fromBlockNumber, toBlockNumber); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('eventsInRange').inc(1); + + const { expected, actual } = await indexer.getProcessedBlockCountForRange(fromBlockNumber, toBlockNumber); + if (expected !== actual) { + throw new Error(`Range not available, expected ${expected}, got ${actual} blocks in range`); + } + + const events = await indexer.getEventsInRange(fromBlockNumber, toBlockNumber); + return events.map(event => indexer.getResultEvent(event)); + }, + + getStateByCID: async (_: any, { cid }: { cid: string }) => { + log('getStateByCID', cid); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('getStateByCID').inc(1); + + const state = await indexer.getStateByCID(cid); + + return state && state.block.isComplete ? getResultState(state) : undefined; + }, + + getState: async (_: any, { blockHash, contractAddress, kind }: { blockHash: string, contractAddress: string, kind: string }) => { + log('getState', blockHash, contractAddress, kind); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('getState').inc(1); + + const state = await indexer.getPrevState(blockHash, contractAddress, kind); + + return state && state.block.isComplete ? getResultState(state) : undefined; + }, + + getSyncStatus: async () => { + log('getSyncStatus'); + gqlTotalQueryCount.inc(1); + gqlQueryCount.labels('getSyncStatus').inc(1); + + return indexer.getSyncStatus(); + } + } + }; +}; diff --git a/packages/ERC20TOT/src/schema.gql b/packages/ERC20TOT/src/schema.gql new file mode 100644 index 00000000..11bf2ede --- /dev/null +++ b/packages/ERC20TOT/src/schema.gql @@ -0,0 +1,116 @@ +directive @cacheControl(maxAge: Int, inheritMaxAge: Boolean, scope: CacheControlScope) on FIELD_DEFINITION | OBJECT | INTERFACE | UNION + +enum CacheControlScope { + PUBLIC + PRIVATE +} + +scalar BigInt + +scalar BigDecimal + +scalar Bytes + +type Proof { + data: String! +} + +type ResultBoolean { + value: Boolean! + proof: Proof +} + +type ResultString { + value: String! + proof: Proof +} + +type ResultInt { + value: Int! + proof: Proof +} + +type ResultBigInt { + value: BigInt! + proof: Proof +} + +type _Block_ { + cid: String! + hash: String! + number: Int! + timestamp: Int! + parentHash: String! +} + +type _Transaction_ { + hash: String! + index: Int! + from: String! + to: String! +} + +type ResultEvent { + block: _Block_! + tx: _Transaction_! + contract: String! + eventIndex: Int! + event: Event! + proof: Proof +} + +union Event = ApprovalEvent | TransferEvent + +type ApprovalEvent { + owner: String! + spender: String! + value: BigInt! +} + +type TransferEvent { + from: String! + to: String! + value: BigInt! +} + +type SyncStatus { + latestIndexedBlockHash: String! + latestIndexedBlockNumber: Int! + latestCanonicalBlockHash: String! + latestCanonicalBlockNumber: Int! +} + +type ResultState { + block: _Block_! + contractAddress: String! + cid: String! + kind: String! + data: String! +} + +type Query { + events(blockHash: String!, contractAddress: String!, name: String): [ResultEvent!] + eventsInRange(fromBlockNumber: Int!, toBlockNumber: Int!): [ResultEvent!] + totalSupply(blockHash: String!, contractAddress: String!): ResultBigInt! + balanceOf(blockHash: String!, contractAddress: String!, account: String!): ResultBigInt! + allowance(blockHash: String!, contractAddress: String!, owner: String!, spender: String!): ResultBigInt! + name(blockHash: String!, contractAddress: String!): ResultString! + symbol(blockHash: String!, contractAddress: String!): ResultString! + decimals(blockHash: String!, contractAddress: String!): ResultInt! + _balances(blockHash: String!, contractAddress: String!, key0: String!): ResultBigInt! + _allowances(blockHash: String!, contractAddress: String!, key0: String!, key1: String!): ResultBigInt! + _totalSupply(blockHash: String!, contractAddress: String!): ResultBigInt! + _name(blockHash: String!, contractAddress: String!): ResultString! + _symbol(blockHash: String!, contractAddress: String!): ResultString! + getSyncStatus: SyncStatus + getStateByCID(cid: String!): ResultState + getState(blockHash: String!, contractAddress: String!, kind: String): ResultState +} + +type Mutation { + watchContract(address: String!, kind: String!, checkpoint: Boolean!, startingBlock: Int): Boolean! +} + +type Subscription { + onEvent: ResultEvent! +} diff --git a/packages/ERC20TOT/src/server.ts b/packages/ERC20TOT/src/server.ts new file mode 100644 index 00000000..83ca7029 --- /dev/null +++ b/packages/ERC20TOT/src/server.ts @@ -0,0 +1,33 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import fs from 'fs'; +import path from 'path'; +import 'reflect-metadata'; +import debug from 'debug'; + +import { ServerCmd } from '@cerc-io/cli'; + +import { createResolvers } from './resolvers'; +import { Indexer } from './indexer'; +import { Database } from './database'; + +const log = debug('vulcanize:server'); + +export const main = async (): Promise => { + const serverCmd = new ServerCmd(); + await serverCmd.init(Database); + + await serverCmd.initIndexer(Indexer); + + const typeDefs = fs.readFileSync(path.join(__dirname, 'schema.gql')).toString(); + + return serverCmd.exec(createResolvers, typeDefs); +}; + +main().then(() => { + log('Starting server...'); +}).catch(err => { + log(err); +}); diff --git a/packages/ERC20TOT/src/types.ts b/packages/ERC20TOT/src/types.ts new file mode 100644 index 00000000..c4562172 --- /dev/null +++ b/packages/ERC20TOT/src/types.ts @@ -0,0 +1,3 @@ +// +// Copyright 2021 Vulcanize, Inc. +// diff --git a/packages/ERC20TOT/tsconfig.json b/packages/ERC20TOT/tsconfig.json new file mode 100644 index 00000000..f4b8852b --- /dev/null +++ b/packages/ERC20TOT/tsconfig.json @@ -0,0 +1,74 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig.json to read more about this file */ + + /* Basic Options */ + // "incremental": true, /* Enable incremental compilation */ + "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */ + "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ + "lib": ["es2019"], /* Specify library files to be included in the compilation. */ + // "allowJs": true, /* Allow javascript files to be compiled. */ + // "checkJs": true, /* Report errors in .js files. */ + // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */ + // "declaration": true, /* Generates corresponding '.d.ts' file. */ + // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ + "sourceMap": true, /* Generates corresponding '.map' file. */ + // "outFile": "./", /* Concatenate and emit output to single file. */ + "outDir": "dist", /* Redirect output structure to the directory. */ + // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + // "composite": true, /* Enable project compilation */ + // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ + // "removeComments": true, /* Do not emit comments to output. */ + // "noEmit": true, /* Do not emit outputs. */ + // "importHelpers": true, /* Import emit helpers from 'tslib'. */ + "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ + // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ + + /* Strict Type-Checking Options */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* Enable strict null checks. */ + // "strictFunctionTypes": true, /* Enable strict checking of function types. */ + // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ + // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ + // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ + // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + + /* Additional Checks */ + // "noUnusedLocals": true, /* Report errors on unused locals. */ + // "noUnusedParameters": true, /* Report errors on unused parameters. */ + // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */ + + /* Module Resolution Options */ + // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ + // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ + // "typeRoots": [], /* List of folders to include type definitions from. */ + // "types": [], /* Type declaration files to be included in compilation. */ + // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ + "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + + /* Source Map Options */ + // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ + // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ + + /* Experimental Options */ + "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ + // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ + + /* Advanced Options */ + "skipLibCheck": true, /* Skip type checking of declaration files. */ + "forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */ + "resolveJsonModule": true /* Enabling the option allows importing JSON, and validating the types in that JSON file. */ + }, + "include": ["src/**/*"] +}