UNPKG

@patchworkdev/pdk

Version:

Patchwork Development Kit

54 lines (53 loc) 3.11 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generatePonderEnv = generatePonderEnv; const path_1 = __importDefault(require("path")); const config_1 = require("../../common/helpers/config"); const env_1 = require("../../common/helpers/env"); const error_1 = require("../../common/helpers/error"); const logger_1 = require("../../common/helpers/logger"); const text_1 = require("../../common/helpers/text"); const lockFile_1 = __importDefault(require("../../services/lockFile")); const contract_processor_1 = require("../dev/services/contract-processor"); async function generatePonderEnv(configPath) { // Resolve the full path of the config file const fullConfigPath = path_1.default.isAbsolute(configPath) ? configPath : path_1.default.resolve(process.cwd(), configPath); const configDir = path_1.default.dirname(fullConfigPath); // Define paths relative to the config file const ponderEnvPath = path_1.default.join(configDir, 'ponder', '.env.local'); const projectConfig = await (0, config_1.importPatchworkConfig)(fullConfigPath); if (!projectConfig.networks) { logger_1.logger.error(`No networks found in the project config. Cannot build network configuration.`); throw new error_1.PDKError(error_1.ErrorCode.PROJECT_CONFIG_MISSING_NETWORKS, `No networks found in the project config at ${fullConfigPath}`); } const env = await (0, env_1.getEnvFile)(ponderEnvPath); Object.entries(projectConfig.networks).map(([networkName, network]) => { env[`${(0, text_1.envVarCase)(networkName)}_RPC`] = network.rpc; }); const lockFileManager = new lockFile_1.default(configPath); const selectedNetwork = lockFileManager.getCurrentNetwork(); const contractProcessor = new contract_processor_1.ContractProcessor(); const bytecodeInfo = await contractProcessor.processContracts(configPath, {}, false); for (const contractName in projectConfig.contracts) { const deploymentInfo = lockFileManager.getLatestDeploymentForContract(contractName, selectedNetwork); if (!deploymentInfo) { if (bytecodeInfo[contractName]) { env[`${(0, text_1.envVarCase)(contractName)}_BLOCK`] = '1'; env[`${(0, text_1.envVarCase)(contractName)}_ADDRESS`] = bytecodeInfo[contractName].deployedAddress; } else { logger_1.logger.error(`No deployment found for ${contractName}`); throw new error_1.PDKError(error_1.ErrorCode.DEPLOYMENT_NOT_FOUND, `No deployment found for ${contractName}`); } } else { env[`${(0, text_1.envVarCase)(contractName)}_BLOCK`] = deploymentInfo.block.toString(); env[`${(0, text_1.envVarCase)(contractName)}_ADDRESS`] = deploymentInfo.address; } } (0, env_1.writeEnvFile)(env, ponderEnvPath); logger_1.logger.info(`Ponder env generated successfully: ${ponderEnvPath}`); }