-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathgre.ts
100 lines (89 loc) · 3.94 KB
/
gre.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* eslint-disable no-case-declarations */
import path from 'path'
import {
getAccounts,
getArbitrator,
getDeployer,
getGateway,
getGovernor,
getPauseGuardian,
getSubgraphAvailabilityOracle,
getTestAccounts,
} from '@graphprotocol/toolshed'
import { loadGraphHorizon, loadSubgraphService } from '@graphprotocol/toolshed/deployments'
import { getAddressBookPath } from './config'
import { GraphPluginError } from './error'
import { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider'
import { isGraphDeployment } from './types'
import { lazyFunction } from 'hardhat/plugins'
import { logDebug } from './logger'
import type { HardhatConfig, HardhatRuntimeEnvironment, HardhatUserConfig } from 'hardhat/types'
import type { GraphDeployments } from '@graphprotocol/toolshed/deployments'
import type { GraphRuntimeEnvironmentOptions } from './types'
export const greExtendConfig = (config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) => {
const userPath = userConfig.paths?.graph
let newPath: string
if (userPath === undefined) {
newPath = config.paths.root
} else {
if (path.isAbsolute(userPath)) {
newPath = userPath
} else {
newPath = path.normalize(path.join(config.paths.root, userPath))
}
}
config.paths.graph = newPath
}
export const greExtendEnvironment = (hre: HardhatRuntimeEnvironment) => {
hre.graph = lazyFunction(() => (opts: GraphRuntimeEnvironmentOptions = { deployments: {} }) => {
logDebug('*** Initializing Graph Runtime Environment (GRE) ***')
logDebug(`Main network: ${hre.network.name}`)
const chainId = hre.network.config.chainId
if (chainId === undefined) {
throw new GraphPluginError('Please define chainId in your Hardhat network configuration')
}
logDebug(`Chain Id: ${chainId}`)
const deployments = [...new Set([
...Object.keys(opts.deployments ?? {}),
...Object.keys(hre.network.config.deployments ?? {}),
...Object.keys(hre.config.graph?.deployments ?? {}),
].filter(v => isGraphDeployment(v)))]
logDebug(`Detected deployments: ${deployments.join(', ')}`)
// Build the Graph Runtime Environment (GRE) for each deployment
const provider = new HardhatEthersProvider(hre.network.provider, hre.network.name)
const greDeployments: GraphDeployments = {} as GraphDeployments
for (const deployment of deployments) {
logDebug(`== Initializing deployment: ${deployment} ==`)
const addressBookPath = getAddressBookPath(deployment, hre, opts)
switch (deployment) {
case 'horizon':
greDeployments.horizon = loadGraphHorizon(addressBookPath, chainId, provider)
break
case 'subgraphService':
greDeployments.subgraphService = loadSubgraphService(addressBookPath, chainId, provider)
break
default:
break
}
}
// Accounts
const grtTokenAddress = greDeployments?.horizon?.contracts?.GraphToken?.target
const accounts = {
getAccounts: async () => getAccounts(provider, grtTokenAddress),
getDeployer: async (accountIndex?: number) => getDeployer(provider, accountIndex, grtTokenAddress),
getGovernor: async (accountIndex?: number) => getGovernor(provider, accountIndex, grtTokenAddress),
getArbitrator: async (accountIndex?: number) => getArbitrator(provider, accountIndex, grtTokenAddress),
getPauseGuardian: async (accountIndex?: number) => getPauseGuardian(provider, accountIndex, grtTokenAddress),
getSubgraphAvailabilityOracle: async (accountIndex?: number) => getSubgraphAvailabilityOracle(provider, accountIndex, grtTokenAddress),
getGateway: async (accountIndex?: number) => getGateway(provider, accountIndex, grtTokenAddress),
getTestAccounts: async () => getTestAccounts(provider, grtTokenAddress),
}
logDebug('GRE initialized successfully!')
return {
...greDeployments,
provider,
chainId,
accounts: accounts,
}
})
}