Skip to content

Commit 5ab5aa2

Browse files
authored
feat(gre): desambiguate chainIds using secondary network name (#714)
Signed-off-by: Tomás Migone <[email protected]>
1 parent af17480 commit 5ab5aa2

File tree

5 files changed

+141
-9
lines changed

5 files changed

+141
-9
lines changed

gre/config.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { HttpNetworkConfig } from 'hardhat/types/config'
77

88
import { GraphRuntimeEnvironmentOptions } from './type-extensions'
99
import { GREPluginError } from './helpers/error'
10-
import GraphNetwork from './helpers/network'
10+
import GraphNetwork, { counterpartName } from './helpers/network'
1111

1212
import { createProvider } from 'hardhat/internal/core/providers/construction'
1313
import { EthersProviderWrapper } from '@nomiclabs/hardhat-ethers/internal/ethers-provider-wrapper'
@@ -217,7 +217,7 @@ function getNetworkConfig(
217217
chainId: number,
218218
mainNetworkName: string,
219219
): (NetworkConfig & { name: string }) | undefined {
220-
let candidateNetworks = Object.keys(networks)
220+
const candidateNetworks = Object.keys(networks)
221221
.map((n) => ({ ...networks[n], name: n }))
222222
.filter((n) => n.chainId === chainId)
223223

@@ -226,14 +226,26 @@ function getNetworkConfig(
226226
`Found multiple networks with chainId ${chainId}, trying to use main network name to desambiguate`,
227227
)
228228

229-
candidateNetworks = candidateNetworks.filter((n) => n.name === mainNetworkName)
229+
const filteredByMainNetworkName = candidateNetworks.filter((n) => n.name === mainNetworkName)
230230

231-
if (candidateNetworks.length === 1) {
232-
return candidateNetworks[0]
231+
if (filteredByMainNetworkName.length === 1) {
232+
logDebug(`Found network with chainId ${chainId} and name ${mainNetworkName}`)
233+
return filteredByMainNetworkName[0]
233234
} else {
234-
throw new GREPluginError(
235-
`Found multiple networks with chainID ${chainId}. This is not supported!`,
235+
logWarn(`Could not desambiguate with main network name, trying secondary network name`)
236+
const secondaryNetworkName = counterpartName(mainNetworkName)
237+
const filteredBySecondaryNetworkName = candidateNetworks.filter(
238+
(n) => n.name === secondaryNetworkName,
236239
)
240+
241+
if (filteredBySecondaryNetworkName.length === 1) {
242+
logDebug(`Found network with chainId ${chainId} and name ${mainNetworkName}`)
243+
return filteredBySecondaryNetworkName[0]
244+
} else {
245+
throw new GREPluginError(
246+
`Could not desambiguate network with chainID ${chainId}. Use case not supported!`,
247+
)
248+
}
237249
}
238250
} else if (candidateNetworks.length === 1) {
239251
return candidateNetworks[0]
@@ -242,7 +254,7 @@ function getNetworkConfig(
242254
}
243255
}
244256

245-
function getNetworkName(
257+
export function getNetworkName(
246258
networks: NetworksConfig,
247259
chainId: number,
248260
mainNetworkName: string,

gre/helpers/network.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,46 @@ const chainMap = new MapWithGetKey<number>([
1616
[1337, 412346], // Localhost - Arbitrum Localhost
1717
])
1818

19+
// Hardhat network names as per our convention
20+
const nameMap = new MapWithGetKey<string>([
21+
['mainnet', 'arbitrum-one'], // Ethereum Mainnet - Arbitrum One
22+
['rinkeby', 'arbitrum-rinkeby'], // Ethereum Rinkeby - Arbitrum Rinkeby
23+
['goerli', 'arbitrum-goerli'], // Ethereum Goerli - Arbitrum Goerli
24+
['localnitrol1', 'localnitrol2'], // Arbitrum testnode L1 - Arbitrum testnode L2
25+
])
26+
1927
export const l1Chains = Array.from(chainMap.keys())
2028
export const l2Chains = Array.from(chainMap.values())
2129
export const chains = [...l1Chains, ...l2Chains]
2230

31+
export const l1ChainNames = Array.from(nameMap.keys())
32+
export const l2ChainNames = Array.from(nameMap.values())
33+
export const chainNames = [...l1ChainNames, ...l2ChainNames]
34+
2335
export const isL1 = (chainId: number): boolean => l1Chains.includes(chainId)
2436
export const isL2 = (chainId: number): boolean => l2Chains.includes(chainId)
2537
export const isSupported = (chainId: number | undefined): boolean =>
2638
chainId !== undefined && chains.includes(chainId)
2739

40+
export const isL1Name = (name: string): boolean => l1ChainNames.includes(name)
41+
export const isL2Name = (name: string): boolean => l2ChainNames.includes(name)
42+
export const isSupportedName = (name: string | undefined): boolean =>
43+
name !== undefined && chainNames.includes(name)
44+
2845
export const l1ToL2 = (chainId: number): number | undefined => chainMap.get(chainId)
2946
export const l2ToL1 = (chainId: number): number | undefined => chainMap.getKey(chainId)
3047
export const counterpart = (chainId: number): number | undefined => {
3148
if (!isSupported(chainId)) return
3249
return isL1(chainId) ? l1ToL2(chainId) : l2ToL1(chainId)
3350
}
3451

52+
export const l1ToL2Name = (name: string): string | undefined => nameMap.get(name)
53+
export const l2ToL1Name = (name: string): string | undefined => nameMap.getKey(name)
54+
export const counterpartName = (name: string): string | undefined => {
55+
if (!isSupportedName(name)) return
56+
return isL1Name(name) ? l1ToL2Name(name) : l2ToL1Name(name)
57+
}
58+
3559
export default {
3660
l1Chains,
3761
l2Chains,

gre/test/config.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { expect } from 'chai'
22
import { useEnvironment } from './helpers'
33

4-
import { getAddressBookPath, getChains, getGraphConfigPaths, getProviders } from '../config'
4+
import {
5+
getAddressBookPath,
6+
getChains,
7+
getGraphConfigPaths,
8+
getNetworkName,
9+
getProviders,
10+
} from '../config'
511
import path from 'path'
612

713
describe('GRE init functions', function () {
@@ -104,6 +110,36 @@ describe('GRE init functions', function () {
104110
})
105111
})
106112

113+
describe('getProviders with graph-config-desambiguate project', function () {
114+
useEnvironment('graph-config-desambiguate', 'localnitrol1')
115+
116+
it('should use main network name to desambiguate if multiple chains are defined with same chainId', async function () {
117+
const { l1Provider, l2Provider } = getProviders(this.hre, 1337, 412346, true)
118+
expect(l1Provider).to.be.an('object')
119+
expect(l2Provider).to.be.an('object')
120+
121+
const l1NetworkName = getNetworkName(this.hre.config.networks, 1337, 'localnitrol1')
122+
const l2NetworkName = getNetworkName(this.hre.config.networks, 412346, 'localnitrol1')
123+
expect(l1NetworkName).to.equal('localnitrol1')
124+
expect(l2NetworkName).to.equal('localnitrol2')
125+
})
126+
})
127+
128+
describe('getProviders with graph-config-desambiguate project', function () {
129+
useEnvironment('graph-config-desambiguate', 'localnitrol2')
130+
131+
it('should use secondary network name to desambiguate if multiple chains are defined with same chainId', async function () {
132+
const { l1Provider, l2Provider } = getProviders(this.hre, 1337, 412346, true)
133+
expect(l1Provider).to.be.an('object')
134+
expect(l2Provider).to.be.an('object')
135+
136+
const l1NetworkName = getNetworkName(this.hre.config.networks, 1337, 'localnitrol2')
137+
const l2NetworkName = getNetworkName(this.hre.config.networks, 412346, 'localnitrol2')
138+
expect(l1NetworkName).to.equal('localnitrol1')
139+
expect(l2NetworkName).to.equal('localnitrol2')
140+
})
141+
})
142+
107143
describe('getGraphConfigPaths with graph-config-full project', function () {
108144
useEnvironment('graph-config-full')
109145

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import '../../../gre'
2+
3+
module.exports = {
4+
paths: {
5+
graph: '../../files',
6+
},
7+
solidity: '0.8.9',
8+
defaultNetwork: 'hardhat',
9+
networks: {
10+
hardhat: {
11+
chainId: 1337,
12+
},
13+
localhost: {
14+
chainId: 1337,
15+
url: `http://localhost:8545`,
16+
},
17+
localnitrol1: {
18+
chainId: 1337,
19+
url: `http://localhost:8545`,
20+
},
21+
localnitrol2: {
22+
chainId: 412346,
23+
url: `http://localhost:8547`,
24+
},
25+
mainnet: {
26+
chainId: 1,
27+
graphConfig: 'config/graph.mainnet.yml',
28+
url: `https://mainnet.infura.io/v3/123456`,
29+
},
30+
'arbitrum-one': {
31+
chainId: 42161,
32+
url: 'https://arb1.arbitrum.io/rpc',
33+
graphConfig: 'config/graph.arbitrum-goerli.yml',
34+
},
35+
goerli: {
36+
chainId: 5,
37+
url: `https://goerli.infura.io/v3/123456`,
38+
graphConfig: 'config/graph.goerli.yml',
39+
},
40+
'arbitrum-goerli': {
41+
chainId: 421613,
42+
url: 'https://goerli-rollup.arbitrum.io/rpc',
43+
graphConfig: 'config/graph.arbitrum-goerli.yml',
44+
},
45+
rinkeby: {
46+
chainId: 4,
47+
url: `https://goerli.infura.io/v3/123456`,
48+
},
49+
'arbitrum-rinkeby': {
50+
chainId: 421611,
51+
url: `https://goerli.infura.io/v3/123456`,
52+
},
53+
},
54+
graph: {
55+
addressBook: 'addresses-hre.json',
56+
l1GraphConfig: 'config/graph.hre.yml',
57+
l2GraphConfig: 'config/graph.arbitrum-hre.yml',
58+
},
59+
}

gre/test/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ export function useEnvironment(fixtureProjectName: string, network?: string): vo
2020

2121
afterEach('Resetting hardhat', function () {
2222
resetHardhatContext()
23+
delete process.env.HARDHAT_NETWORK
2324
})
2425
}

0 commit comments

Comments
 (0)