From 2b4d98b9c69900019ec1e57559ae62e48a3dbd5f Mon Sep 17 00:00:00 2001 From: Nabarun Gogoi Date: Wed, 10 May 2023 18:49:11 +0530 Subject: [PATCH] Fix watch contract to update checksum address in `stateStatusMap` (#377) * Fix comment for codegen config contract kind property * Update stateStatusMap with checksum address of contract * Use non-null object composer for object types --------- Co-authored-by: prathamesh0 --- packages/codegen/README.md | 2 +- packages/codegen/src/schema.ts | 12 ++++++++---- .../src/templates/package-template.handlebars | 12 ++++++------ packages/codegen/subgraph-demo.md | 4 ++-- packages/util/src/indexer.ts | 5 +++-- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/packages/codegen/README.md b/packages/codegen/README.md index fe4da1f1..8046272f 100644 --- a/packages/codegen/README.md +++ b/packages/codegen/README.md @@ -37,7 +37,7 @@ Steps: - name: Example # Contract file path or an url. path: ../graph-node/test/contracts/Example.sol - # Contract kind (should match that in {subgraphPath}/subgraph.yaml if subgraphPath provided) + # Contract kind (should match name of dataSource in {subgraphPath}/subgraph.yaml if subgraphPath provided) kind: Example1 # Output folder path (logs output using `stdout` if not provided). diff --git a/packages/codegen/src/schema.ts b/packages/codegen/src/schema.ts index 09dac1e7..657af6f8 100644 --- a/packages/codegen/src/schema.ts +++ b/packages/codegen/src/schema.ts @@ -4,7 +4,7 @@ import assert from 'assert'; import { GraphQLSchema, parse, printSchema, print, GraphQLDirective, GraphQLInt, GraphQLBoolean } from 'graphql'; -import { ObjectTypeComposer, ObjectTypeComposerDefinition, ObjectTypeComposerFieldConfigMapDefinition, SchemaComposer } from 'graphql-compose'; +import { ObjectTypeComposer, NonNullComposer, ObjectTypeComposerDefinition, ObjectTypeComposerFieldConfigMapDefinition, SchemaComposer } from 'graphql-compose'; import { Writable } from 'stream'; import { utils } from 'ethers'; import { VariableDeclaration } from '@solidity-parser/parser/dist/src/ast-types'; @@ -519,7 +519,7 @@ export class Schema { } /** - * Create GraphQL schmea object type. + * Create GraphQL schema object type. * @param name * @param params */ @@ -547,15 +547,19 @@ export class Schema { * Get type of field in GraphQL schema for object types. * @param param */ - _getObjectTypeField (param: utils.ParamType): ObjectTypeComposer | string | any[] { + _getObjectTypeField (param: utils.ParamType): NonNullComposer | string | any[] { if (param.indexed && ['string', 'bytes', 'tuple', 'array'].includes(param.baseType)) { // Check for indexed reference type event params. param = utils.ParamType.fromObject({ type: 'bytes32', name: param.name }); } + // TODO: Get type name for tuple base types + // ethers.utils gives both param.type and param.baseType as 'tuple', but doesn't give the actual type name + // represented by 'internalType' field in the ABI + // eg. "internalType": "struct Provider" or "internalType": "struct Task[]" if (param.baseType === 'tuple') { const typeName = param.name.charAt(0).toUpperCase() + param.name.slice(1); - return this._createObjectType(typeName, param.components); + return this._createObjectType(typeName, param.components).NonNull; } if (param.baseType === 'array') { diff --git a/packages/codegen/src/templates/package-template.handlebars b/packages/codegen/src/templates/package-template.handlebars index cb0d2373..6eb7111a 100644 --- a/packages/codegen/src/templates/package-template.handlebars +++ b/packages/codegen/src/templates/package-template.handlebars @@ -41,7 +41,6 @@ "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.41", "@cerc-io/ipld-eth-client": "^0.2.41", "@cerc-io/solidity-mapper": "^0.2.41", @@ -49,23 +48,25 @@ {{#if (subgraphPath)}} "@cerc-io/graph-node": "^0.2.41", {{/if}} + "@ethersproject/providers": "^5.4.4", "apollo-type-bigint": "^0.1.3", "debug": "^4.3.1", + "decimal.js": "^10.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" + "yargs": "^17.0.1" }, "devDependencies": { "@ethersproject/abi": "^5.3.0", - "@types/yargs": "^17.0.0", "@types/debug": "^4.1.5", "@types/json-bigint": "^1.0.0", + "@types/yargs": "^17.0.0", "@typescript-eslint/eslint-plugin": "^5.47.1", "@typescript-eslint/parser": "^5.47.1", + "copyfiles": "^2.4.1", "eslint": "^8.35.0", "eslint-config-semistandard": "^15.0.1", "eslint-config-standard": "^16.0.3", @@ -75,7 +76,6 @@ "eslint-plugin-standard": "^5.0.0", "husky": "^7.0.2", "ts-node": "^10.2.1", - "typescript": "^5.0.2", - "copyfiles": "^2.4.1" + "typescript": "^5.0.2" } } diff --git a/packages/codegen/subgraph-demo.md b/packages/codegen/subgraph-demo.md index 1d644dc1..fed52b76 100644 --- a/packages/codegen/subgraph-demo.md +++ b/packages/codegen/subgraph-demo.md @@ -43,7 +43,7 @@ - name: Example # Contract file path or an url. path: ../graph-node/test/contracts/Example.sol - # Contract kind (should match that in {subgraphPath}/subgraph.yaml if subgraphPath provided) + # Contract kind (should match name of dataSource in {subgraphPath}/subgraph.yaml if subgraphPath provided) kind: Example1 # Output folder path (logs output using `stdout` if not provided). @@ -213,5 +213,5 @@ * A `checkpoint` state should be created at the latest canonical block hash. * Run the `getState` query again at the endpoint with the output `blockHash` and kind `checkpoint`. - + * All the `State` entries can be seen in `pg-admin` in table `state`. diff --git a/packages/util/src/indexer.ts b/packages/util/src/indexer.ts index 2bcac72c..e0cc2c03 100644 --- a/packages/util/src/indexer.ts +++ b/packages/util/src/indexer.ts @@ -515,14 +515,15 @@ export class Indexer { async watchContract (address: string, kind: string, checkpoint: boolean, startingBlock: number): Promise { assert(this._db.saveContract); - this.updateStateStatusMap(address, {}); - const dbTx = await this._db.createTransactionRunner(); // Use the checksum address (https://docs.ethers.io/v5/api/utils/address/#utils-getAddress) if input to address is a contract address. // If a contract identifier is passed as address instead, no need to convert to checksum address. // Customize: use the kind input to filter out non-contract-address input to address. const contractAddress = (kind === '__protocol__') ? address : ethers.utils.getAddress(address); + this.updateStateStatusMap(contractAddress, {}); + const dbTx = await this._db.createTransactionRunner(); + try { const contract = await this._db.saveContract(dbTx, contractAddress, kind, checkpoint, startingBlock); this.cacheContract(contract);