UNPKG

hardhat-graph-protocol

Version:

A hardhat plugin that extends the runtime environment to inject additional functionality related to the usage of the Graph Protocol.

98 lines 4.43 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.greExtendEnvironment = exports.greExtendConfig = void 0; const deployments_1 = require("@graphprotocol/toolshed/deployments"); const hardhat_ethers_provider_1 = require("@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider"); const plugins_1 = require("hardhat/plugins"); const path_1 = __importDefault(require("path")); const accounts_1 = require("./accounts"); const config_1 = require("./config"); const error_1 = require("./error"); const logger_1 = require("./logger"); const types_1 = require("./types"); const greExtendConfig = (config, userConfig) => { const userPath = userConfig.paths?.graph; let newPath; if (userPath === undefined) { newPath = config.paths.root; } else { if (path_1.default.isAbsolute(userPath)) { newPath = userPath; } else { newPath = path_1.default.normalize(path_1.default.join(config.paths.root, userPath)); } } config.paths.graph = newPath; }; exports.greExtendConfig = greExtendConfig; const greExtendEnvironment = (hre) => { hre.graph = (0, plugins_1.lazyFunction)(() => (opts) => { (0, logger_1.logDebug)('*** Initializing Graph Runtime Environment (GRE) ***'); (0, logger_1.logDebug)(`Main network: ${hre.network.name}`); if (opts === undefined) { opts = { deployments: {}, createAddressBook: false, }; } const chainId = hre.network.config.chainId; if (chainId === undefined) { throw new error_1.GraphPluginError('Please define chainId in your Hardhat network configuration'); } (0, logger_1.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) => (0, types_1.isGraphDeployment)(v))), ]; (0, logger_1.logDebug)(`Detected deployments: ${deployments.join(', ')}`); // Build the Graph Runtime Environment (GRE) for each deployment const provider = new hardhat_ethers_provider_1.HardhatEthersProvider(hre.network.provider, hre.network.name); const greDeployments = {}; for (const deployment of deployments) { (0, logger_1.logDebug)(`== Initializing deployment: ${deployment} ==`); const addressBookPath = (0, config_1.getAddressBookPath)(deployment, hre, opts); if (addressBookPath === undefined) { (0, logger_1.logError)(`Skipping deployment ${deployment} - Reason: address book path does not exist`); continue; } try { switch (deployment) { case 'horizon': greDeployments.horizon = (0, deployments_1.loadGraphHorizon)(addressBookPath, chainId, provider); break; case 'subgraphService': greDeployments.subgraphService = (0, deployments_1.loadSubgraphService)(addressBookPath, chainId, provider); break; default: (0, logger_1.logError)(`Skipping deployment ${deployment} - Reason: unknown deployment`); break; } } catch (error) { (0, logger_1.logError)(`Skipping deployment ${deployment} - Reason: runtime error`); (0, logger_1.logError)(error); continue; } } // Accounts // We use ? here because we've previously asserted that the deployment exists which might not be true const accounts = (0, accounts_1.getAccounts)(provider, chainId, greDeployments.horizon?.contracts?.GraphToken?.target); (0, logger_1.logDebug)('GRE initialized successfully!'); return { ...greDeployments, provider, chainId, accounts, }; }); }; exports.greExtendEnvironment = greExtendEnvironment; //# sourceMappingURL=gre.js.map