From f8d5404c25e525956412c28a24df16d401d1809c Mon Sep 17 00:00:00 2001 From: Nabarun Gogoi Date: Mon, 23 Oct 2023 09:23:20 +0530 Subject: [PATCH] Fix codegen for creating subgraph watcher for Sushiswap (#432) * Fix EthClient import in codegen and graph-node package * Fix codegen for resolving common events between contracts --- packages/codegen/README.md | 1 + packages/codegen/src/client.ts | 1 - packages/codegen/src/database.ts | 1 - packages/codegen/src/entity.ts | 2 -- packages/codegen/src/indexer.ts | 2 -- packages/codegen/src/resolvers.ts | 1 - packages/codegen/src/schema.ts | 5 ++--- .../codegen/src/templates/indexer-template.handlebars | 4 ++-- packages/codegen/src/utils/type-mappings.ts | 9 +++++++-- packages/graph-node/src/watcher.ts | 2 +- packages/graph-node/test/utils/indexer.ts | 4 ++-- packages/ipld-eth-client/package.json | 1 + packages/ipld-eth-client/src/eth-client.ts | 3 ++- packages/rpc-eth-client/src/eth-client.ts | 4 ++-- packages/solidity-mapper/package.json | 1 - 15 files changed, 20 insertions(+), 21 deletions(-) diff --git a/packages/codegen/README.md b/packages/codegen/README.md index 8046272f..06af180e 100644 --- a/packages/codegen/README.md +++ b/packages/codegen/README.md @@ -53,6 +53,7 @@ Steps: port: 3008 # Solc version to use (optional) + # Use longVersion prefixed with v from the release list https://binaries.soliditylang.org/bin/list.json # If not defined, uses solc version listed in dependencies solc: v0.8.0+commit.c7dfd78e diff --git a/packages/codegen/src/client.ts b/packages/codegen/src/client.ts index 01bb79ae..72c4ba78 100644 --- a/packages/codegen/src/client.ts +++ b/packages/codegen/src/client.ts @@ -47,7 +47,6 @@ export class Client { queryObject.params = queryObject.params.map((param) => { const gqlParamType = getGqlForSol(param.type); - assert(gqlParamType); const tsParamType = getTsForGql(gqlParamType); assert(tsParamType); param.type = tsParamType; diff --git a/packages/codegen/src/database.ts b/packages/codegen/src/database.ts index 9f0ff200..84ccffb8 100644 --- a/packages/codegen/src/database.ts +++ b/packages/codegen/src/database.ts @@ -64,7 +64,6 @@ export class Database { queryObject.params = queryObject.params.map((param) => { const gqlParamType = getGqlForSol(param.type); - assert(gqlParamType); const tsParamType = getTsForGql(gqlParamType); assert(tsParamType); param.type = tsParamType; diff --git a/packages/codegen/src/entity.ts b/packages/codegen/src/entity.ts index 524f695b..374ccb1b 100644 --- a/packages/codegen/src/entity.ts +++ b/packages/codegen/src/entity.ts @@ -113,7 +113,6 @@ export class Entity { const name = param.name; const gqlType = getGqlForSol(param.type); - assert(gqlType); const tsType = getTsForGql(gqlType); assert(tsType); const pgType = getPgForTs(tsType); @@ -153,7 +152,6 @@ export class Entity { const baseType = getBaseType(typeName); assert(baseType); const gqlReturnType = getGqlForSol(baseType); - assert(gqlReturnType); let tsReturnType = getTsForGql(gqlReturnType); assert(tsReturnType); const pgReturnType = getPgForTs(tsReturnType); diff --git a/packages/codegen/src/indexer.ts b/packages/codegen/src/indexer.ts index 1131601c..363820c2 100644 --- a/packages/codegen/src/indexer.ts +++ b/packages/codegen/src/indexer.ts @@ -69,7 +69,6 @@ export class Indexer { const baseType = getBaseType(typeName); assert(baseType); const gqlReturnType = getGqlForSol(baseType); - assert(gqlReturnType); let tsReturnType = getTsForGql(gqlReturnType); assert(tsReturnType); @@ -107,7 +106,6 @@ export class Indexer { queryObject.params = queryObject.params.map((param) => { const gqlParamType = getGqlForSol(param.type); - assert(gqlParamType); const tsParamType = getTsForGql(gqlParamType); assert(tsParamType); param.type = tsParamType; diff --git a/packages/codegen/src/resolvers.ts b/packages/codegen/src/resolvers.ts index 8e997721..502fd040 100644 --- a/packages/codegen/src/resolvers.ts +++ b/packages/codegen/src/resolvers.ts @@ -43,7 +43,6 @@ export class Resolvers { queryObject.params = queryObject.params.map((param) => { const gqlParamType = getGqlForSol(param.type); - assert(gqlParamType); const tsParamType = getTsForGql(gqlParamType); assert(tsParamType); param.type = tsParamType; diff --git a/packages/codegen/src/schema.ts b/packages/codegen/src/schema.ts index 657af6f8..fa5f9cf8 100644 --- a/packages/codegen/src/schema.ts +++ b/packages/codegen/src/schema.ts @@ -9,7 +9,7 @@ import { Writable } from 'stream'; import { utils } from 'ethers'; import { VariableDeclaration } from '@solidity-parser/parser/dist/src/ast-types'; -import { getGqlForTs, getGqlForSol } from './utils/type-mappings'; +import { getGqlForSol } from './utils/type-mappings'; import { Param } from './utils/types'; import { getBaseType, isArrayType } from './utils/helpers'; @@ -251,7 +251,6 @@ export class Schema { assert(baseTypeName); const gqlReturnType = getGqlForSol(baseTypeName); - assert(gqlReturnType, `gql type for sol type ${baseTypeName} for ${functionName} not found`); return { type: gqlReturnType, @@ -510,7 +509,7 @@ export class Schema { const newFields: any = {}; params.forEach((param: Param) => { if (!commonFields.includes(param.name)) { - newFields[param.name] = `${getGqlForTs(param.type)}`; + newFields[param.name] = `${getGqlForSol(param.type)}`; } }); diff --git a/packages/codegen/src/templates/indexer-template.handlebars b/packages/codegen/src/templates/indexer-template.handlebars index 55410971..2865fc4a 100644 --- a/packages/codegen/src/templates/indexer-template.handlebars +++ b/packages/codegen/src/templates/indexer-template.handlebars @@ -15,7 +15,6 @@ import { SelectionNode } from 'graphql'; 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, @@ -42,7 +41,8 @@ import { ResultEvent, getResultEvent, DatabaseInterface, - Clients + Clients, + EthClient } from '@cerc-io/util'; {{#if (subgraphPath)}} import { GraphWatcher } from '@cerc-io/graph-node'; diff --git a/packages/codegen/src/utils/type-mappings.ts b/packages/codegen/src/utils/type-mappings.ts index a4d0c316..770fbfb2 100644 --- a/packages/codegen/src/utils/type-mappings.ts +++ b/packages/codegen/src/utils/type-mappings.ts @@ -2,6 +2,8 @@ // Copyright 2021 Vulcanize, Inc. // +import assert from 'assert'; + import { solToGql } from './solToGql'; const _tsToGql: Map = new Map(); @@ -29,8 +31,11 @@ _gqlToTs.set('Boolean', 'boolean'); _gqlToTs.set('BigDecimal', 'Decimal'); _gqlToTs.set('Bytes', 'string'); -function getGqlForSol (solType: string): string | undefined { - return solToGql.get(solType); +function getGqlForSol (solType: string): string { + const gqlType = solToGql.get(solType); + assert(gqlType, `GQL type for SOL type ${solType} not found`); + + return gqlType; } function getGqlForTs (tsType: string): string | undefined { diff --git a/packages/graph-node/src/watcher.ts b/packages/graph-node/src/watcher.ts index f7cb9b42..2dc6f9ed 100644 --- a/packages/graph-node/src/watcher.ts +++ b/packages/graph-node/src/watcher.ts @@ -11,7 +11,6 @@ import { ContractInterface, utils, providers } from 'ethers'; import { SelectionNode } from 'graphql'; import { ResultObject } from '@cerc-io/assemblyscript/lib/loader'; -import { EthClient } from '@cerc-io/ipld-eth-client'; import { getFullBlock, BlockHeight, @@ -27,6 +26,7 @@ import { createEvent, getSubgraphConfig, Transaction, + EthClient, DEFAULT_LIMIT } from '@cerc-io/util'; diff --git a/packages/graph-node/test/utils/indexer.ts b/packages/graph-node/test/utils/indexer.ts index 6c6c8b07..4e09392e 100644 --- a/packages/graph-node/test/utils/indexer.ts +++ b/packages/graph-node/test/utils/indexer.ts @@ -16,9 +16,9 @@ import { StateInterface, getResultEvent, ResultEvent, - StateKind + StateKind, + EthClient } from '@cerc-io/util'; -import { EthClient } from '@cerc-io/ipld-eth-client'; import { GetStorageAt, getStorageValue, MappingKey, StorageLayout } from '@cerc-io/solidity-mapper'; export class Indexer implements IndexerInterface { diff --git a/packages/ipld-eth-client/package.json b/packages/ipld-eth-client/package.json index f984568c..b2c767f4 100644 --- a/packages/ipld-eth-client/package.json +++ b/packages/ipld-eth-client/package.json @@ -21,6 +21,7 @@ "dependencies": { "@apollo/client": "^3.7.1", "@cerc-io/cache": "^0.2.66", + "@cerc-io/util": "^0.2.66", "cross-fetch": "^3.1.4", "debug": "^4.3.1", "ethers": "^5.4.4", diff --git a/packages/ipld-eth-client/src/eth-client.ts b/packages/ipld-eth-client/src/eth-client.ts index 9f000a5d..a981de3e 100644 --- a/packages/ipld-eth-client/src/eth-client.ts +++ b/packages/ipld-eth-client/src/eth-client.ts @@ -5,6 +5,7 @@ import assert from 'assert'; import { Cache } from '@cerc-io/cache'; +import { EthClient as EthClientInterface } from '@cerc-io/util'; import ethQueries from './eth-queries'; import { padKey } from './utils'; @@ -22,7 +23,7 @@ interface Vars { addresses?: string[]; } -export class EthClient { +export class EthClient implements EthClientInterface { _graphqlClient: GraphQLClient; _cache: Cache | undefined; diff --git a/packages/rpc-eth-client/src/eth-client.ts b/packages/rpc-eth-client/src/eth-client.ts index de48f940..2aa3f1ae 100644 --- a/packages/rpc-eth-client/src/eth-client.ts +++ b/packages/rpc-eth-client/src/eth-client.ts @@ -7,7 +7,7 @@ import { errors, providers, utils } from 'ethers'; import { TransactionReceipt } from '@ethersproject/abstract-provider'; import { Cache } from '@cerc-io/cache'; -import { encodeHeader, escapeHexString, getRawTransaction } from '@cerc-io/util'; +import { encodeHeader, escapeHexString, getRawTransaction, EthClient as EthClientInterface } from '@cerc-io/util'; import { padKey } from '@cerc-io/ipld-eth-client'; export interface Config { @@ -23,7 +23,7 @@ interface Vars { addresses?: string[]; } -export class EthClient { +export class EthClient implements EthClientInterface { _provider: providers.JsonRpcProvider; _cache: Cache | undefined; diff --git a/packages/solidity-mapper/package.json b/packages/solidity-mapper/package.json index 70d96963..392cd73c 100644 --- a/packages/solidity-mapper/package.json +++ b/packages/solidity-mapper/package.json @@ -4,7 +4,6 @@ "main": "dist/index.js", "license": "AGPL-3.0", "devDependencies": { - "@cerc-io/ipld-eth-client": "^0.2.66", "@ethersproject/abi": "^5.3.0", "@nomiclabs/hardhat-ethers": "^2.0.2", "@nomiclabs/hardhat-waffle": "^2.0.1",