Skip to content

Commit c0c256c

Browse files
committed
fix: path resolution for contract artifacts
Signed-off-by: Tomás Migone <[email protected]>
1 parent 4f8cfc3 commit c0c256c

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

packages/toolshed/src/deployments/horizon/contracts.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolveNodeModulesPath } from '../../lib/path'
1+
import { resolvePackagePath } from '../../lib/path'
22

33
import type {
44
Controller,
@@ -35,8 +35,8 @@ export const GraphHorizonContractNameList = [
3535
'GraphTallyCollector',
3636
] as const
3737

38-
export const CONTRACTS_ARTIFACTS_PATH = resolveNodeModulesPath('@graphprotocol/contracts/build/contracts')
39-
export const HORIZON_ARTIFACTS_PATH = resolveNodeModulesPath('@graphprotocol/horizon/build/contracts')
38+
export const CONTRACTS_ARTIFACTS_PATH = resolvePackagePath('@graphprotocol/contracts', 'build/contracts')
39+
export const HORIZON_ARTIFACTS_PATH = resolvePackagePath('@graphprotocol/horizon', 'build/contracts')
4040

4141
export const GraphHorizonArtifactsMap = {
4242
// @graphprotocol/contracts

packages/toolshed/src/deployments/horizon/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { GraphHorizonAddressBook } from './address-book'
22
import { loadActions } from './actions'
3-
import { resolveNodeModulesPath } from '../../lib/path'
43

54
import type { Provider, Signer } from 'ethers'
65
import type { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider'
@@ -37,7 +36,7 @@ export function loadGraphHorizon(addressBookPath: string, chainId: number, provi
3736

3837
export function connectGraphHorizon(chainId: number, signerOrProvider: Signer | Provider, addressBookPath?: string) {
3938
const addressBook = new GraphHorizonAddressBook(
40-
addressBookPath ?? resolveNodeModulesPath('@graphprotocol/horizon/addresses.json'),
39+
addressBookPath ?? require.resolve('@graphprotocol/horizon/addresses.json'),
4140
chainId,
4241
)
4342
return addressBook.loadContracts(signerOrProvider)

packages/toolshed/src/deployments/subgraph-service/contracts.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolveNodeModulesPath } from '../../lib/path'
1+
import { resolvePackagePath } from '../../lib/path'
22

33
import type {
44
DisputeManager,
@@ -11,6 +11,7 @@ import type {
1111
LegacyServiceRegistry,
1212
} from './types'
1313
import type { ContractList } from '../contract'
14+
1415
export const SubgraphServiceContractNameList = [
1516
// @graphprotocol/contracts
1617
'L2Curation',
@@ -26,8 +27,8 @@ export const SubgraphServiceContractNameList = [
2627
'LegacyServiceRegistry',
2728
] as const
2829

29-
export const CONTRACTS_ARTIFACTS_PATH = resolveNodeModulesPath('@graphprotocol/contracts/build/contracts')
30-
export const SUBGRAPH_SERVICE_ARTIFACTS_PATH = resolveNodeModulesPath('@graphprotocol/subgraph-service/build/contracts')
30+
export const CONTRACTS_ARTIFACTS_PATH = resolvePackagePath('@graphprotocol/contracts', 'build/contracts')
31+
export const SUBGRAPH_SERVICE_ARTIFACTS_PATH = resolvePackagePath('@graphprotocol/subgraph-service', 'build/contracts')
3132

3233
export const SubgraphServiceArtifactsMap = {
3334
// @graphprotocol/contracts

packages/toolshed/src/deployments/subgraph-service/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider'
22
import { loadActions } from './actions'
3-
import { resolveNodeModulesPath } from '../../lib/path'
43
import { SubgraphServiceAddressBook } from './address-book'
54

65
import type { Provider, Signer } from 'ethers'
@@ -20,7 +19,7 @@ export function loadSubgraphService(addressBookPath: string, chainId: number, pr
2019

2120
export function connectSubgraphService(chainId: number, signerOrProvider: Signer | Provider, addressBookPath?: string) {
2221
const addressBook = new SubgraphServiceAddressBook(
23-
addressBookPath ?? resolveNodeModulesPath('@graphprotocol/subgraph-service/addresses.json'),
22+
addressBookPath ?? require.resolve('@graphprotocol/subgraph-service/addresses.json'),
2423
chainId,
2524
)
2625
return addressBook.loadContracts(signerOrProvider)

packages/toolshed/src/lib/path.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,18 @@ export function findPathUp(startPath: string, pathToFind: string): string | null
2121
return null
2222
}
2323

24-
export function resolveNodeModulesPath(packageName: string): string {
24+
// Useful if you need to resolve a path to a file that might not exist but you
25+
// know it will eventually exist in the node_modules directory
26+
export function resolveNodeModulesPath(fileName: string): string {
2527
const basePath = findPathUp(__dirname, 'node_modules')
2628
if (!basePath) {
2729
throw new Error('Could not find node_modules directory')
2830
}
29-
return path.resolve(basePath, packageName)
31+
return path.resolve(basePath, fileName)
32+
}
33+
34+
// Useful if you need to resolve a path to a file that exists in a package
35+
export function resolvePackagePath(packageName: string, relativePath: string): string {
36+
const packageRoot = path.dirname(require.resolve(`${packageName}/package.json`))
37+
return path.join(packageRoot, relativePath)
3038
}

0 commit comments

Comments
 (0)