Skip to content

Commit eaff905

Browse files
committed
fix(toolshed): address book path resolution
Signed-off-by: Tomás Migone <tomas@edgeandnode.com>
1 parent d939135 commit eaff905

File tree

4 files changed

+54
-21
lines changed

4 files changed

+54
-21
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ export function loadGraphHorizon(addressBookPath: string, chainId: number, provi
1919
}
2020

2121
export function connectGraphHorizon(chainId: number, signerOrProvider: Signer | Provider, addressBookPath?: string) {
22-
addressBookPath =
23-
addressBookPath ?? resolveAddressBook(require, '@graphprotocol/address-book', 'horizon/addresses.json')
22+
addressBookPath = addressBookPath ?? resolveAddressBook(require, '@graphprotocol/address-book/horizon/addresses.json')
2423
if (!addressBookPath) {
2524
throw new Error('Address book path not found')
2625
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function loadSubgraphService(addressBookPath: string, chainId: number, pr
2020

2121
export function connectSubgraphService(chainId: number, signerOrProvider: Signer | Provider, addressBookPath?: string) {
2222
addressBookPath =
23-
addressBookPath ?? resolveAddressBook(require, '@graphprotocol/address-book', 'subgraph-service/addresses.json')
23+
addressBookPath ?? resolveAddressBook(require, '@graphprotocol/address-book/subgraph-service/addresses.json')
2424
if (!addressBookPath) {
2525
throw new Error('Address book path not found')
2626
}

packages/toolshed/src/hardhat/hardhat.base.config.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,26 @@ export const networksUserConfig = function (callerRequire: typeof require): Base
8787
mnemonic: 'myth like bonus scare over problem client lizard pioneer submit female collect',
8888
},
8989
deployments: {
90-
horizon: resolveAddressBook(callerRequire, '@graphprotocol/horizon', 'addresses-hardhat.json'),
91-
subgraphService: resolveAddressBook(callerRequire, '@graphprotocol/subgraph-service', 'addresses-hardhat.json'),
90+
horizon: resolveAddressBook(callerRequire, '@graphprotocol/horizon/addresses.json', 'addresses-hardhat.json'),
91+
subgraphService: resolveAddressBook(
92+
callerRequire,
93+
'@graphprotocol/subgraph-service/addresses.json',
94+
'addresses-hardhat.json',
95+
),
9296
},
9397
},
9498
localNetwork: {
9599
chainId: 1337,
96100
url: LOCAL_NETWORK_RPC,
97101
deployments: {
98-
horizon: resolveAddressBook(callerRequire, '@graphprotocol/horizon', 'addresses-local-network.json'),
102+
horizon: resolveAddressBook(
103+
callerRequire,
104+
'@graphprotocol/horizon/addresses.json',
105+
'addresses-local-network.json',
106+
),
99107
subgraphService: resolveAddressBook(
100108
callerRequire,
101-
'@graphprotocol/subgraph-service',
109+
'@graphprotocol/subgraph-service/addresses.json',
102110
'addresses-local-network.json',
103111
),
104112
},
@@ -110,10 +118,10 @@ export const networksUserConfig = function (callerRequire: typeof require): Base
110118
enabled: true,
111119
},
112120
deployments: {
113-
horizon: resolveAddressBook(callerRequire, '@graphprotocol/horizon', 'addresses-localhost.json'),
121+
horizon: resolveAddressBook(callerRequire, '@graphprotocol/horizon/addresses.json', 'addresses-localhost.json'),
114122
subgraphService: resolveAddressBook(
115123
callerRequire,
116-
'@graphprotocol/subgraph-service',
124+
'@graphprotocol/subgraph-service/addresses.json',
117125
'addresses-localhost.json',
118126
),
119127
},
@@ -148,8 +156,8 @@ export const hardhatBaseConfig = function (callerRequire: typeof require): BaseH
148156
networks: networksUserConfig(callerRequire),
149157
graph: {
150158
deployments: {
151-
horizon: resolveAddressBook(callerRequire, '@graphprotocol/horizon'),
152-
subgraphService: resolveAddressBook(callerRequire, '@graphprotocol/subgraph-service'),
159+
horizon: resolveAddressBook(callerRequire, '@graphprotocol/horizon/addresses.json'),
160+
subgraphService: resolveAddressBook(callerRequire, '@graphprotocol/subgraph-service/addresses.json'),
153161
},
154162
},
155163
etherscan: etherscanUserConfig,

packages/toolshed/src/lib/resolve.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
11
import path from 'path'
22

33
/**
4-
* Use to get a resolved path to an address book file wether it exists or not
5-
* If addresses.json does not exist in the package root, returns undefined
6-
* @param callerRequire - The require function to use
7-
* @param packageName - The name of the package to resolve the address book for
8-
* @param addressBook - The name of the address book file to resolve
9-
* @returns The resolved path to the address book file
4+
* Resolves the absolute path to an address book file relative to an existing file.
5+
*
6+
* This function uses `require.resolve` (from the caller's context) on the provided
7+
* `existingAddressBookPath` to locate a known file (e.g., an existing address book JSON file).
8+
* Once located, it returns the absolute path to the desired `addressBookPath`, which is resolved
9+
* relative to the directory of the existing file.
10+
*
11+
* If the existing file cannot be resolved, the function returns `undefined`.
12+
*
13+
* This is useful when:
14+
* - You know the location of one file in a package or module.
15+
* - You need the path to another file in the same directory (or nearby), whether or not it exists.
16+
*
17+
* ## Examples
18+
*
19+
* ```ts
20+
* // Example 1: Resolve a different file in the same folder
21+
* // Locates: <node_modules>/@graphprotocol/horizon/addresses.json
22+
* // Returns: <node_modules>/@graphprotocol/horizon/addresses-hardhat.json
23+
* resolveAddressBook(require, 'addresses.json', 'addresses-hardhat.json')
24+
* ```
25+
*
26+
* ```ts
27+
* // Example 2: Resolve the same file you use for lookup
28+
* // Locates and returns: <node_modules>/@graphprotocol/address-book/horizon/addresses.json
29+
* resolveAddressBook(require, '@graphprotocol/address-book/horizon/addresses.json')
30+
* ```
31+
*
32+
* @param callerRequire - The `require` function from the calling module, used for resolution relative to the caller.
33+
* @param existingAddressBookPath - A resolvable path to an existing file (relative to the caller), used as an anchor. Defaults to `"addresses.json"`.
34+
* @param addressBookPath - The path (relative to the anchor's directory) to the file you want returned. Defaults to `"addresses.json"`.
35+
* @returns The absolute path to the requested file, or `undefined` if the existing file cannot be resolved.
1036
*/
1137
export function resolveAddressBook(
1238
callerRequire: typeof require,
13-
packageName: string,
14-
addressBook?: string,
39+
existingAddressBookPath: string = 'addresses.json',
40+
addressBookPath: string = 'addresses.json',
1541
): string | undefined {
1642
try {
17-
const packageRoot = path.dirname(callerRequire.resolve(`${packageName}/addresses.json`))
18-
return path.join(packageRoot, addressBook ?? 'addresses.json')
43+
const packageRoot = path.dirname(callerRequire.resolve(`${existingAddressBookPath}`))
44+
return path.join(packageRoot, addressBookPath)
1945
} catch (_) {
2046
return undefined
2147
}

0 commit comments

Comments
 (0)