Skip to content

Commit a99e216

Browse files
committed
fix: couple fixes and tweaks
Signed-off-by: Tomás Migone <[email protected]>
1 parent 42b43b6 commit a99e216

File tree

13 files changed

+137
-101
lines changed

13 files changed

+137
-101
lines changed

packages/hardhat-graph-protocol/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"scripts": {
1919
"build": "tsc",
2020
"lint": "eslint '**/*.{js,ts}' --fix",
21-
"test": "mocha --exit --recursive 'test/**/*.test.ts'"
21+
"test": "mocha --exit --recursive 'test/**/*.test.ts'",
22+
"prepublishOnly": "npm run build"
2223
},
2324
"files": [
2425
"dist/",

packages/hardhat-graph-protocol/src/config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { GraphPluginError } from './sdk/utils/error'
44
import { logDebug } from './logger'
55
import { normalizePath } from './sdk/utils/path'
66

7-
import type { GraphDeployment } from './deployments'
8-
import type { GraphRuntimeEnvironmentOptions } from './type-extensions'
7+
import type { GraphDeployment, GraphRuntimeEnvironmentOptions } from './types'
98
import type { HardhatRuntimeEnvironment } from 'hardhat/types'
109

1110
export function getAddressBookPath(
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
import type { GraphHorizonAddressBook, GraphHorizonContracts } from './sdk/deployments/horizon'
22

33
// List of supported Graph deployments
4-
const GraphDeploymentsList = [
4+
export const GraphDeploymentsList = [
55
'horizon',
66
] as const
77

8-
export type GraphDeployment = (typeof GraphDeploymentsList)[number]
9-
108
export type GraphDeploymentRuntimeEnvironmentMap = {
119
horizon: {
1210
contracts: GraphHorizonContracts
1311
addressBook: GraphHorizonAddressBook
1412
}
1513
}
16-
17-
export function isGraphDeployment(deployment: unknown): deployment is GraphDeployment {
18-
return (
19-
typeof deployment === 'string'
20-
&& GraphDeploymentsList.includes(deployment as GraphDeployment)
21-
)
22-
}

packages/hardhat-graph-protocol/src/gre.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import path from 'path'
22

3-
import { assertGraphRuntimeEnvironment } from './type-extensions'
43
import { getAddressBookPath } from './config'
54
import { GraphHorizonAddressBook } from './sdk/deployments/horizon'
65
import { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider'
7-
import { isGraphDeployment } from './deployments'
86
import { logDebug } from './logger'
97

8+
import { assertGraphRuntimeEnvironment, type GraphRuntimeEnvironmentOptions, isGraphDeployment } from './types'
109
import type { HardhatConfig, HardhatRuntimeEnvironment, HardhatUserConfig } from 'hardhat/types'
11-
import type { GraphRuntimeEnvironmentOptions } from './type-extensions'
1210

1311
export const greExtendConfig = (config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) => {
1412
const userPath = userConfig.paths?.graph

packages/hardhat-graph-protocol/src/sdk/address-book.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,14 @@ export abstract class AddressBook<
7878
if (!fs.existsSync(this.file)) throw new Error(`Address book path provided does not exist!`)
7979

8080
// Load address book and validate its shape
81+
// If it's empty, initialize it with an empty object
8182
const fileContents = JSON.parse(fs.readFileSync(this.file, 'utf8') || '{}')
83+
if (!fileContents[this.chainId]) {
84+
fileContents[this.chainId] = {} as Record<ContractName, AddressBookEntry>
85+
}
8286
this.assertAddressBookJson(fileContents)
8387
this.addressBook = fileContents
8488
this._parseAddressBook()
85-
86-
// If the address book is empty for this chain id, initialize it with an empty object
87-
if (!this.addressBook[this.chainId]) {
88-
this.addressBook[this.chainId] = {} as Record<ContractName, AddressBookEntry>
89-
}
9089
}
9190

9291
/**
@@ -165,6 +164,9 @@ export abstract class AddressBook<
165164
signerOrProvider?: Signer | Provider,
166165
): ContractList<ContractName> {
167166
const contracts = {} as ContractList<ContractName>
167+
if (this.listEntries().length == 0) {
168+
throw Error('No valid contracts found in address book')
169+
}
168170
for (const contractName of this.listEntries()) {
169171
const artifactPath = typeof artifactsPath === 'object' && !Array.isArray(artifactsPath)
170172
? artifactsPath[contractName]
Lines changed: 14 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,44 @@
11
// To extend one of Hardhat's types, you need to import the module where it has been defined, and redeclare it.
22
import 'hardhat/types/config'
33
import 'hardhat/types/runtime'
4-
5-
import type { GraphDeployment, GraphDeploymentRuntimeEnvironmentMap } from './deployments'
6-
import type { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider'
7-
8-
export type GraphRuntimeEnvironmentOptions = {
9-
deployments?: {
10-
[deployment in GraphDeployment]?: string | {
11-
addressBook: string
12-
}
13-
}
14-
}
15-
16-
export type GraphRuntimeEnvironment = {
17-
[deployment in keyof GraphDeploymentRuntimeEnvironmentMap]?: GraphDeploymentRuntimeEnvironmentMap[deployment]
18-
} & {
19-
provider: HardhatEthersProvider
20-
chainId: number
21-
}
22-
23-
export function assertGraphRuntimeEnvironment(
24-
obj: unknown,
25-
): obj is GraphRuntimeEnvironment {
26-
if (typeof obj !== 'object' || obj === null) return false
27-
28-
const deployments = obj as Partial<GraphDeploymentRuntimeEnvironmentMap>
29-
30-
for (const deployment in deployments) {
31-
const environment = deployments[deployment as keyof GraphDeploymentRuntimeEnvironmentMap]
32-
if (!environment || typeof environment !== 'object') {
33-
return false
34-
}
35-
}
36-
37-
if (typeof (obj as GraphRuntimeEnvironment).provider !== 'object') {
38-
return false
39-
}
40-
41-
if (typeof (obj as GraphRuntimeEnvironment).chainId !== 'function') {
42-
return false
43-
}
44-
45-
return true
46-
}
4+
import type { GraphDeployments, GraphRuntimeEnvironment, GraphRuntimeEnvironmentOptions } from './types'
475

486
declare module 'hardhat/types/runtime' {
49-
export interface HardhatRuntimeEnvironment {
7+
interface HardhatRuntimeEnvironment {
508
graph: (opts?: GraphRuntimeEnvironmentOptions) => GraphRuntimeEnvironment
519
}
5210
}
5311

5412
declare module 'hardhat/types/config' {
55-
export interface HardhatConfig {
13+
interface HardhatConfig {
5614
graph: GraphRuntimeEnvironmentOptions
5715
}
5816

59-
export interface HardhatUserConfig {
17+
interface HardhatUserConfig {
6018
graph: GraphRuntimeEnvironmentOptions
6119
}
6220

63-
export interface HardhatNetworkConfig {
64-
deployments?: {
65-
[deployment in GraphDeployment]?: string | {
66-
addressBook: string
67-
}
68-
}
21+
interface HardhatNetworkConfig {
22+
deployments?: GraphDeployments
6923
}
7024

71-
export interface HardhatNetworkUserConfig {
72-
deployments?: {
73-
[deployment in GraphDeployment]?: string | {
74-
addressBook: string
75-
}
76-
}
25+
interface HardhatNetworkUserConfig {
26+
deployments?: GraphDeployments
7727
}
7828

79-
export interface HttpNetworkConfig {
80-
deployments?: {
81-
[deployment in GraphDeployment]?: string | {
82-
addressBook: string
83-
}
84-
}
29+
interface HttpNetworkConfig {
30+
deployments?: GraphDeployments
8531
}
8632

87-
export interface HttpNetworkUserConfig {
88-
deployments?: {
89-
[deployment in GraphDeployment]?: string | {
90-
addressBook: string
91-
}
92-
}
33+
interface HttpNetworkUserConfig {
34+
deployments?: GraphDeployments
9335
}
9436

95-
export interface ProjectPathsConfig {
37+
interface ProjectPathsConfig {
9638
graph?: string
9739
}
9840

99-
export interface ProjectPathsUserConfig {
41+
interface ProjectPathsUserConfig {
10042
graph?: string
10143
}
10244
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { type GraphDeploymentRuntimeEnvironmentMap, GraphDeploymentsList } from './deployment-list'
2+
import type { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider'
3+
4+
export type GraphDeployment = (typeof GraphDeploymentsList)[number]
5+
6+
export type GraphDeployments = {
7+
[deployment in GraphDeployment]?: string | {
8+
addressBook: string
9+
}
10+
}
11+
12+
export type GraphRuntimeEnvironmentOptions = {
13+
deployments?: {
14+
[deployment in GraphDeployment]?: string | {
15+
addressBook: string
16+
}
17+
}
18+
}
19+
20+
export type GraphRuntimeEnvironment = {
21+
[deployment in keyof GraphDeploymentRuntimeEnvironmentMap]?: GraphDeploymentRuntimeEnvironmentMap[deployment]
22+
} & {
23+
provider: HardhatEthersProvider
24+
chainId: number
25+
}
26+
27+
export function isGraphDeployment(deployment: unknown): deployment is GraphDeployment {
28+
return (
29+
typeof deployment === 'string'
30+
&& GraphDeploymentsList.includes(deployment as GraphDeployment)
31+
)
32+
}
33+
34+
export function assertGraphRuntimeEnvironment(
35+
obj: unknown,
36+
): obj is GraphRuntimeEnvironment {
37+
if (typeof obj !== 'object' || obj === null) return false
38+
39+
const deployments = obj as Partial<GraphDeploymentRuntimeEnvironmentMap>
40+
41+
for (const deployment in deployments) {
42+
const environment = deployments[deployment as keyof GraphDeploymentRuntimeEnvironmentMap]
43+
if (!environment || typeof environment !== 'object') {
44+
return false
45+
}
46+
}
47+
48+
if (typeof (obj as GraphRuntimeEnvironment).provider !== 'object') {
49+
return false
50+
}
51+
52+
if (typeof (obj as GraphRuntimeEnvironment).chainId !== 'function') {
53+
return false
54+
}
55+
56+
return true
57+
}

packages/hardhat-graph-protocol/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"module": "commonjs",
55
"declaration": true,
66
"declarationMap": true,
7+
"sourceMap": true,
78
"esModuleInterop": true,
89
"forceConsistentCasingInFileNames": true,
910
"strict": true,

packages/horizon/hardhat.config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import 'hardhat-graph-protocol'
88

99
import type { HardhatUserConfig } from 'hardhat/config'
1010

11-
import 'tasks/test'
12-
1311
const config: HardhatUserConfig = {
1412
solidity: {
1513
version: '0.8.27',

packages/horizon/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"solidity-coverage": "^0.8.0",
5656
"ts-node": ">=8.0.0",
5757
"typechain": "^8.3.0",
58-
"typescript": "^5.3.3"
58+
"typescript": "^5.6.3"
5959
},
6060
"lint-staged": {
6161
"contracts/**/*.sol": [

0 commit comments

Comments
 (0)