From a99e216fd292a0ac357a5ace6d1c656acbbf3c18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Migone?= Date: Fri, 22 Nov 2024 15:30:56 -0300 Subject: [PATCH] fix: couple fixes and tweaks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomás Migone --- packages/hardhat-graph-protocol/package.json | 3 +- packages/hardhat-graph-protocol/src/config.ts | 3 +- .../{deployments.ts => deployment-list.ts} | 11 +-- packages/hardhat-graph-protocol/src/gre.ts | 4 +- .../src/sdk/address-book.ts | 12 +-- .../src/type-extensions.ts | 86 +++---------------- packages/hardhat-graph-protocol/src/types.ts | 57 ++++++++++++ packages/hardhat-graph-protocol/tsconfig.json | 1 + packages/horizon/hardhat.config.ts | 2 - packages/horizon/package.json | 2 +- packages/horizon/tsconfig.json | 4 +- .../horizon/types/hardhat-graph-protocol.d.ts | 45 ++++++++++ yarn.lock | 8 +- 13 files changed, 137 insertions(+), 101 deletions(-) rename packages/hardhat-graph-protocol/src/{deployments.ts => deployment-list.ts} (50%) create mode 100644 packages/hardhat-graph-protocol/src/types.ts create mode 100644 packages/horizon/types/hardhat-graph-protocol.d.ts diff --git a/packages/hardhat-graph-protocol/package.json b/packages/hardhat-graph-protocol/package.json index 0774a1f55..5d81d0dd5 100644 --- a/packages/hardhat-graph-protocol/package.json +++ b/packages/hardhat-graph-protocol/package.json @@ -18,7 +18,8 @@ "scripts": { "build": "tsc", "lint": "eslint '**/*.{js,ts}' --fix", - "test": "mocha --exit --recursive 'test/**/*.test.ts'" + "test": "mocha --exit --recursive 'test/**/*.test.ts'", + "prepublishOnly": "npm run build" }, "files": [ "dist/", diff --git a/packages/hardhat-graph-protocol/src/config.ts b/packages/hardhat-graph-protocol/src/config.ts index b73dd80a5..acd966db0 100644 --- a/packages/hardhat-graph-protocol/src/config.ts +++ b/packages/hardhat-graph-protocol/src/config.ts @@ -4,8 +4,7 @@ import { GraphPluginError } from './sdk/utils/error' import { logDebug } from './logger' import { normalizePath } from './sdk/utils/path' -import type { GraphDeployment } from './deployments' -import type { GraphRuntimeEnvironmentOptions } from './type-extensions' +import type { GraphDeployment, GraphRuntimeEnvironmentOptions } from './types' import type { HardhatRuntimeEnvironment } from 'hardhat/types' export function getAddressBookPath( diff --git a/packages/hardhat-graph-protocol/src/deployments.ts b/packages/hardhat-graph-protocol/src/deployment-list.ts similarity index 50% rename from packages/hardhat-graph-protocol/src/deployments.ts rename to packages/hardhat-graph-protocol/src/deployment-list.ts index 8074977b1..a37b85680 100644 --- a/packages/hardhat-graph-protocol/src/deployments.ts +++ b/packages/hardhat-graph-protocol/src/deployment-list.ts @@ -1,22 +1,13 @@ import type { GraphHorizonAddressBook, GraphHorizonContracts } from './sdk/deployments/horizon' // List of supported Graph deployments -const GraphDeploymentsList = [ +export const GraphDeploymentsList = [ 'horizon', ] as const -export type GraphDeployment = (typeof GraphDeploymentsList)[number] - export type GraphDeploymentRuntimeEnvironmentMap = { horizon: { contracts: GraphHorizonContracts addressBook: GraphHorizonAddressBook } } - -export function isGraphDeployment(deployment: unknown): deployment is GraphDeployment { - return ( - typeof deployment === 'string' - && GraphDeploymentsList.includes(deployment as GraphDeployment) - ) -} diff --git a/packages/hardhat-graph-protocol/src/gre.ts b/packages/hardhat-graph-protocol/src/gre.ts index 3424a4c91..d8f4c0825 100644 --- a/packages/hardhat-graph-protocol/src/gre.ts +++ b/packages/hardhat-graph-protocol/src/gre.ts @@ -1,14 +1,12 @@ import path from 'path' -import { assertGraphRuntimeEnvironment } from './type-extensions' import { getAddressBookPath } from './config' import { GraphHorizonAddressBook } from './sdk/deployments/horizon' import { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider' -import { isGraphDeployment } from './deployments' import { logDebug } from './logger' +import { assertGraphRuntimeEnvironment, type GraphRuntimeEnvironmentOptions, isGraphDeployment } from './types' import type { HardhatConfig, HardhatRuntimeEnvironment, HardhatUserConfig } from 'hardhat/types' -import type { GraphRuntimeEnvironmentOptions } from './type-extensions' export const greExtendConfig = (config: HardhatConfig, userConfig: Readonly) => { const userPath = userConfig.paths?.graph diff --git a/packages/hardhat-graph-protocol/src/sdk/address-book.ts b/packages/hardhat-graph-protocol/src/sdk/address-book.ts index 2e8b89ad9..aa18cccb3 100644 --- a/packages/hardhat-graph-protocol/src/sdk/address-book.ts +++ b/packages/hardhat-graph-protocol/src/sdk/address-book.ts @@ -78,15 +78,14 @@ export abstract class AddressBook< if (!fs.existsSync(this.file)) throw new Error(`Address book path provided does not exist!`) // Load address book and validate its shape + // If it's empty, initialize it with an empty object const fileContents = JSON.parse(fs.readFileSync(this.file, 'utf8') || '{}') + if (!fileContents[this.chainId]) { + fileContents[this.chainId] = {} as Record + } this.assertAddressBookJson(fileContents) this.addressBook = fileContents this._parseAddressBook() - - // If the address book is empty for this chain id, initialize it with an empty object - if (!this.addressBook[this.chainId]) { - this.addressBook[this.chainId] = {} as Record - } } /** @@ -165,6 +164,9 @@ export abstract class AddressBook< signerOrProvider?: Signer | Provider, ): ContractList { const contracts = {} as ContractList + if (this.listEntries().length == 0) { + throw Error('No valid contracts found in address book') + } for (const contractName of this.listEntries()) { const artifactPath = typeof artifactsPath === 'object' && !Array.isArray(artifactsPath) ? artifactsPath[contractName] diff --git a/packages/hardhat-graph-protocol/src/type-extensions.ts b/packages/hardhat-graph-protocol/src/type-extensions.ts index 833722b63..f42df8eaa 100644 --- a/packages/hardhat-graph-protocol/src/type-extensions.ts +++ b/packages/hardhat-graph-protocol/src/type-extensions.ts @@ -1,102 +1,44 @@ // To extend one of Hardhat's types, you need to import the module where it has been defined, and redeclare it. import 'hardhat/types/config' import 'hardhat/types/runtime' - -import type { GraphDeployment, GraphDeploymentRuntimeEnvironmentMap } from './deployments' -import type { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider' - -export type GraphRuntimeEnvironmentOptions = { - deployments?: { - [deployment in GraphDeployment]?: string | { - addressBook: string - } - } -} - -export type GraphRuntimeEnvironment = { - [deployment in keyof GraphDeploymentRuntimeEnvironmentMap]?: GraphDeploymentRuntimeEnvironmentMap[deployment] -} & { - provider: HardhatEthersProvider - chainId: number -} - -export function assertGraphRuntimeEnvironment( - obj: unknown, -): obj is GraphRuntimeEnvironment { - if (typeof obj !== 'object' || obj === null) return false - - const deployments = obj as Partial - - for (const deployment in deployments) { - const environment = deployments[deployment as keyof GraphDeploymentRuntimeEnvironmentMap] - if (!environment || typeof environment !== 'object') { - return false - } - } - - if (typeof (obj as GraphRuntimeEnvironment).provider !== 'object') { - return false - } - - if (typeof (obj as GraphRuntimeEnvironment).chainId !== 'function') { - return false - } - - return true -} +import type { GraphDeployments, GraphRuntimeEnvironment, GraphRuntimeEnvironmentOptions } from './types' declare module 'hardhat/types/runtime' { - export interface HardhatRuntimeEnvironment { + interface HardhatRuntimeEnvironment { graph: (opts?: GraphRuntimeEnvironmentOptions) => GraphRuntimeEnvironment } } declare module 'hardhat/types/config' { - export interface HardhatConfig { + interface HardhatConfig { graph: GraphRuntimeEnvironmentOptions } - export interface HardhatUserConfig { + interface HardhatUserConfig { graph: GraphRuntimeEnvironmentOptions } - export interface HardhatNetworkConfig { - deployments?: { - [deployment in GraphDeployment]?: string | { - addressBook: string - } - } + interface HardhatNetworkConfig { + deployments?: GraphDeployments } - export interface HardhatNetworkUserConfig { - deployments?: { - [deployment in GraphDeployment]?: string | { - addressBook: string - } - } + interface HardhatNetworkUserConfig { + deployments?: GraphDeployments } - export interface HttpNetworkConfig { - deployments?: { - [deployment in GraphDeployment]?: string | { - addressBook: string - } - } + interface HttpNetworkConfig { + deployments?: GraphDeployments } - export interface HttpNetworkUserConfig { - deployments?: { - [deployment in GraphDeployment]?: string | { - addressBook: string - } - } + interface HttpNetworkUserConfig { + deployments?: GraphDeployments } - export interface ProjectPathsConfig { + interface ProjectPathsConfig { graph?: string } - export interface ProjectPathsUserConfig { + interface ProjectPathsUserConfig { graph?: string } } diff --git a/packages/hardhat-graph-protocol/src/types.ts b/packages/hardhat-graph-protocol/src/types.ts new file mode 100644 index 000000000..b15045849 --- /dev/null +++ b/packages/hardhat-graph-protocol/src/types.ts @@ -0,0 +1,57 @@ +import { type GraphDeploymentRuntimeEnvironmentMap, GraphDeploymentsList } from './deployment-list' +import type { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider' + +export type GraphDeployment = (typeof GraphDeploymentsList)[number] + +export type GraphDeployments = { + [deployment in GraphDeployment]?: string | { + addressBook: string + } +} + +export type GraphRuntimeEnvironmentOptions = { + deployments?: { + [deployment in GraphDeployment]?: string | { + addressBook: string + } + } +} + +export type GraphRuntimeEnvironment = { + [deployment in keyof GraphDeploymentRuntimeEnvironmentMap]?: GraphDeploymentRuntimeEnvironmentMap[deployment] +} & { + provider: HardhatEthersProvider + chainId: number +} + +export function isGraphDeployment(deployment: unknown): deployment is GraphDeployment { + return ( + typeof deployment === 'string' + && GraphDeploymentsList.includes(deployment as GraphDeployment) + ) +} + +export function assertGraphRuntimeEnvironment( + obj: unknown, +): obj is GraphRuntimeEnvironment { + if (typeof obj !== 'object' || obj === null) return false + + const deployments = obj as Partial + + for (const deployment in deployments) { + const environment = deployments[deployment as keyof GraphDeploymentRuntimeEnvironmentMap] + if (!environment || typeof environment !== 'object') { + return false + } + } + + if (typeof (obj as GraphRuntimeEnvironment).provider !== 'object') { + return false + } + + if (typeof (obj as GraphRuntimeEnvironment).chainId !== 'function') { + return false + } + + return true +} diff --git a/packages/hardhat-graph-protocol/tsconfig.json b/packages/hardhat-graph-protocol/tsconfig.json index 18d89b091..41ba7e8bc 100644 --- a/packages/hardhat-graph-protocol/tsconfig.json +++ b/packages/hardhat-graph-protocol/tsconfig.json @@ -4,6 +4,7 @@ "module": "commonjs", "declaration": true, "declarationMap": true, + "sourceMap": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, diff --git a/packages/horizon/hardhat.config.ts b/packages/horizon/hardhat.config.ts index 9d9f4c57c..dca9b466a 100644 --- a/packages/horizon/hardhat.config.ts +++ b/packages/horizon/hardhat.config.ts @@ -8,8 +8,6 @@ import 'hardhat-graph-protocol' import type { HardhatUserConfig } from 'hardhat/config' -import 'tasks/test' - const config: HardhatUserConfig = { solidity: { version: '0.8.27', diff --git a/packages/horizon/package.json b/packages/horizon/package.json index 79f9c6b91..e2363022a 100644 --- a/packages/horizon/package.json +++ b/packages/horizon/package.json @@ -55,7 +55,7 @@ "solidity-coverage": "^0.8.0", "ts-node": ">=8.0.0", "typechain": "^8.3.0", - "typescript": "^5.3.3" + "typescript": "^5.6.3" }, "lint-staged": { "contracts/**/*.sol": [ diff --git a/packages/horizon/tsconfig.json b/packages/horizon/tsconfig.json index 806f1c23e..202a9923e 100644 --- a/packages/horizon/tsconfig.json +++ b/packages/horizon/tsconfig.json @@ -6,10 +6,12 @@ "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true, - "resolveJsonModule": true + "resolveJsonModule": true, + "outDir": "dist" }, "include": [ "hardhat.config.ts", + "types/**/*.ts", "scripts/**/*.ts", "tasks/**/*.ts", "test/**/*.ts", diff --git a/packages/horizon/types/hardhat-graph-protocol.d.ts b/packages/horizon/types/hardhat-graph-protocol.d.ts new file mode 100644 index 000000000..ab3c30b4b --- /dev/null +++ b/packages/horizon/types/hardhat-graph-protocol.d.ts @@ -0,0 +1,45 @@ +// TypeScript does not resolve correctly the type extensions when they are symlinked from the same monorepo. +// So we need to re-type it... this file should be a copy of hardhat-graph-protocol/src/type-extensions.ts +import 'hardhat/types/config' +import 'hardhat/types/runtime' +import type { GraphDeployments, GraphRuntimeEnvironment, GraphRuntimeEnvironmentOptions } from 'hardhat-graph-protocol/src/types' + +declare module 'hardhat/types/runtime' { + interface HardhatRuntimeEnvironment { + graph: (opts?: GraphRuntimeEnvironmentOptions) => GraphRuntimeEnvironment + } +} + +declare module 'hardhat/types/config' { + interface HardhatConfig { + graph: GraphRuntimeEnvironmentOptions + } + + interface HardhatUserConfig { + graph: GraphRuntimeEnvironmentOptions + } + + interface HardhatNetworkConfig { + deployments?: GraphDeployments + } + + interface HardhatNetworkUserConfig { + deployments?: GraphDeployments + } + + interface HttpNetworkConfig { + deployments?: GraphDeployments + } + + interface HttpNetworkUserConfig { + deployments?: GraphDeployments + } + + interface ProjectPathsConfig { + graph?: string + } + + interface ProjectPathsUserConfig { + graph?: string + } +} diff --git a/yarn.lock b/yarn.lock index 3b8fcd674..ce5384ed2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2965,7 +2965,7 @@ __metadata: solidity-coverage: "npm:^0.8.0" ts-node: "npm:>=8.0.0" typechain: "npm:^8.3.0" - typescript: "npm:^5.3.3" + typescript: "npm:^5.6.3" languageName: unknown linkType: soft @@ -6153,9 +6153,9 @@ __metadata: linkType: hard "@types/mocha@npm:^10.0.9": - version: 10.0.9 - resolution: "@types/mocha@npm:10.0.9" - checksum: 76dd782ac7e971ea159d4a7fd40c929afa051e040be3f41187ff03a2d7b3279e19828ddaa498ba1757b3e6b91316263bb7640db0e906938275b97a06e087b989 + version: 10.0.10 + resolution: "@types/mocha@npm:10.0.10" + checksum: d2b8c48138cde6923493e42b38e839695eb42edd04629abe480a8f34c0e3f50dd82a55832c2e8d2b6e6f9e4deb492d7d733e600fbbdd5a0ceccbcfc6844ff9d5 languageName: node linkType: hard