|
| 1 | +import path from 'path' |
| 2 | + |
| 3 | +import { getAddressBookPath } from './config' |
| 4 | +import { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider' |
| 5 | +import { logDebug } from './logger' |
| 6 | + |
| 7 | +import { GraphHorizonAddressBook } from './sdk/deployments/horizon' |
| 8 | +import { SubgraphServiceAddressBook } from './sdk/deployments/subgraph-service' |
| 9 | + |
| 10 | +import { assertGraphRuntimeEnvironment, type GraphRuntimeEnvironmentOptions, isGraphDeployment } from './types' |
| 11 | +import type { HardhatConfig, HardhatRuntimeEnvironment, HardhatUserConfig } from 'hardhat/types' |
| 12 | + |
| 13 | +export const greExtendConfig = (config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) => { |
| 14 | + const userPath = userConfig.paths?.graph |
| 15 | + |
| 16 | + let newPath: string |
| 17 | + if (userPath === undefined) { |
| 18 | + newPath = config.paths.root |
| 19 | + } else { |
| 20 | + if (path.isAbsolute(userPath)) { |
| 21 | + newPath = userPath |
| 22 | + } else { |
| 23 | + newPath = path.normalize(path.join(config.paths.root, userPath)) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + config.paths.graph = newPath |
| 28 | +} |
| 29 | + |
| 30 | +export const greExtendEnvironment = (hre: HardhatRuntimeEnvironment) => { |
| 31 | + hre.graph = (opts: GraphRuntimeEnvironmentOptions = { deployments: {} }) => { |
| 32 | + logDebug('*** Initializing Graph Runtime Environment (GRE) ***') |
| 33 | + logDebug(`Main network: ${hre.network.name}`) |
| 34 | + const chainId = hre.network.config.chainId |
| 35 | + if (chainId === undefined) { |
| 36 | + throw new Error('Please define chainId in your Hardhat network configuration') |
| 37 | + } |
| 38 | + logDebug(`Chain Id: ${chainId}`) |
| 39 | + |
| 40 | + const deployments = [ |
| 41 | + ...Object.keys(opts.deployments ?? {}), |
| 42 | + ...Object.keys(hre.network.config.deployments ?? {}), |
| 43 | + ...Object.keys(hre.config.graph?.deployments ?? {}), |
| 44 | + ].filter(v => isGraphDeployment(v)) |
| 45 | + logDebug(`Detected deployments: ${deployments.join(', ')}`) |
| 46 | + |
| 47 | + // Build the Graph Runtime Environment (GRE) for each deployment |
| 48 | + const provider = new HardhatEthersProvider(hre.network.provider, hre.network.name) |
| 49 | + const greDeployments: Record<string, unknown> = {} |
| 50 | + for (const deployment of deployments) { |
| 51 | + logDebug(`== Initializing deployment: ${deployment} ==`) |
| 52 | + const addressBookPath = getAddressBookPath(deployment, hre, opts) |
| 53 | + let addressBook |
| 54 | + |
| 55 | + switch (deployment) { |
| 56 | + case 'horizon': |
| 57 | + addressBook = new GraphHorizonAddressBook(addressBookPath, chainId) |
| 58 | + greDeployments.horizon = { |
| 59 | + addressBook: addressBook, |
| 60 | + contracts: addressBook.loadContracts(provider), |
| 61 | + } |
| 62 | + break |
| 63 | + case 'subgraphService': |
| 64 | + addressBook = new SubgraphServiceAddressBook(addressBookPath, chainId) |
| 65 | + greDeployments.subgraphService = { |
| 66 | + addressBook: addressBook, |
| 67 | + contracts: addressBook.loadContracts(provider), |
| 68 | + } |
| 69 | + break |
| 70 | + default: |
| 71 | + break |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + const gre = { |
| 76 | + ...greDeployments, |
| 77 | + provider, |
| 78 | + chainId, |
| 79 | + } |
| 80 | + assertGraphRuntimeEnvironment(gre) |
| 81 | + logDebug('GRE initialized successfully!') |
| 82 | + return gre |
| 83 | + } |
| 84 | +} |
0 commit comments