Skip to content

Commit

Permalink
fix: couple fixes and tweaks
Browse files Browse the repository at this point in the history
Signed-off-by: Tomás Migone <[email protected]>
  • Loading branch information
tmigone committed Nov 22, 2024
1 parent 42b43b6 commit a99e216
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 101 deletions.
3 changes: 2 additions & 1 deletion packages/hardhat-graph-protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
3 changes: 1 addition & 2 deletions packages/hardhat-graph-protocol/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
)
}
4 changes: 1 addition & 3 deletions packages/hardhat-graph-protocol/src/gre.ts
Original file line number Diff line number Diff line change
@@ -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<HardhatUserConfig>) => {
const userPath = userConfig.paths?.graph
Expand Down
12 changes: 7 additions & 5 deletions packages/hardhat-graph-protocol/src/sdk/address-book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContractName, AddressBookEntry>
}
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<ContractName, AddressBookEntry>
}
}

/**
Expand Down Expand Up @@ -165,6 +164,9 @@ export abstract class AddressBook<
signerOrProvider?: Signer | Provider,
): ContractList<ContractName> {
const contracts = {} as ContractList<ContractName>
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]
Expand Down
86 changes: 14 additions & 72 deletions packages/hardhat-graph-protocol/src/type-extensions.ts
Original file line number Diff line number Diff line change
@@ -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<GraphDeploymentRuntimeEnvironmentMap>

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
}
}
57 changes: 57 additions & 0 deletions packages/hardhat-graph-protocol/src/types.ts
Original file line number Diff line number Diff line change
@@ -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<GraphDeploymentRuntimeEnvironmentMap>

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
}
1 change: 1 addition & 0 deletions packages/hardhat-graph-protocol/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"module": "commonjs",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
Expand Down
2 changes: 0 additions & 2 deletions packages/horizon/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion packages/horizon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
4 changes: 3 additions & 1 deletion packages/horizon/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
45 changes: 45 additions & 0 deletions packages/horizon/types/hardhat-graph-protocol.d.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a99e216

Please sign in to comment.