Skip to content

Commit 09b5cb9

Browse files
committed
feat(gre): desambiguate chainIds using secondary network name
Signed-off-by: Tomás Migone <[email protected]>
1 parent 790b01d commit 09b5cb9

File tree

4 files changed

+117
-6
lines changed

4 files changed

+117
-6
lines changed

gre/config.ts

Lines changed: 18 additions & 6 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) {
231+
if (filteredByMainNetworkName.length === 1) {
232+
logDebug(`Found network with chainId ${chainId} and name ${mainNetworkName}`)
232233
return candidateNetworks[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 candidateNetworks[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]

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ describe('GRE init functions', function () {
104104
})
105105
})
106106

107+
describe('getProviders with graph-config-desambiguate project', function () {
108+
useEnvironment('graph-config-desambiguate')
109+
110+
it('should desambiguate chains correctly if multiple chains are defined with same chainId (HH L1)', function () {
111+
const { l1Provider, l2Provider } = getProviders(this.hre, 1337, 412346, true)
112+
expect(l1Provider).to.be.an('object')
113+
expect(l2Provider).to.be.an('object')
114+
})
115+
116+
it('should desambiguate chain correctly if multiple chains are defined with same chainId (HH L2)', function () {
117+
const { l1Provider, l2Provider } = getProviders(this.hre, 1337, 412346, false)
118+
expect(l1Provider).to.be.an('object')
119+
expect(l2Provider).to.be.an('object')
120+
})
121+
})
122+
107123
describe('getGraphConfigPaths with graph-config-full project', function () {
108124
useEnvironment('graph-config-full')
109125

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+
}

0 commit comments

Comments
 (0)