UNPKG

dop-stick

Version:

Source control tooling for versionable-upgradeable smart contracts

150 lines 6.31 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getProvider = exports.getSigner = exports.getContractFactory = void 0; const ethers_1 = require("ethers"); const logger_1 = require("./logsAndMetrics/core/logger"); const path = __importStar(require("path")); const fs = __importStar(require("fs")); const ethers = __importStar(require("ethers")); /** * Dynamically imports Hardhat from the project directory */ async function getHardhatInstance(projectRoot) { try { process.chdir(projectRoot); // Try to import hardhat directly first try { const hardhat = require('hardhat'); return hardhat; } catch (_a) { // If direct import fails, try dynamic import const { ethers, ...rest } = await Promise.resolve().then(() => __importStar(require('hardhat'))); return { ethers, ...rest }; } } catch (error) { throw new Error('Failed to import Hardhat. Please ensure hardhat is installed in your project.\n' + 'Try running: npm install --save-dev hardhat'); } } async function getContractFactory(name, signer) { try { // Find the project root (directory with hardhat.config) const projectRoot = findHardhatConfig(process.cwd()); if (!projectRoot) { throw new Error('Could not find hardhat.config.ts/js in current or parent directories.\n' + 'Please ensure you are running this command from within a Hardhat project structure.'); } // logger.info(`Found Hardhat project root at: ${projectRoot}`); // Store original directory const originalCwd = process.cwd(); try { // Get Hardhat instance const hardhat = await getHardhatInstance(projectRoot); // Try different path combinations const possiblePaths = [ name, `${name}.sol:${name}`, `contracts/${name}.sol:${name}`, `facets/${name}.sol:${name}`, `modules/${name}.sol:${name}` // Check modules directory ]; let lastError = null; for (const contractPath of possiblePaths) { try { // logger.info(`Trying to find contract at: ${contractPath}`); // Try both ways to access ethers try { if (hardhat.ethers) { return await hardhat.ethers.getContractFactory(contractPath, signer); } } catch (_a) { const { ethers } = require('hardhat'); return await ethers.getContractFactory(contractPath, signer); } } catch (error) { lastError = error; continue; } } // If all attempts fail throw new Error(`Failed to find contract "${name}" in project at ${projectRoot}.\n` + "Please ensure:\n" + "1. The contract is compiled (run 'npx hardhat compile')\n" + "2. The contract name matches the file name\n" + "3. The contract is in one of these locations:\n" + " - contracts/\n" + " - contracts/facets/\n" + " - contracts/modules/\n" + `Last error: ${(lastError === null || lastError === void 0 ? void 0 : lastError.message) || 'Unknown error'}`); } finally { // Restore original working directory process.chdir(originalCwd); } } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; logger_1.Logger.error(`Contract factory creation failed: ${errorMessage}`); throw error; } } exports.getContractFactory = getContractFactory; function findHardhatConfig(startPath) { let currentPath = startPath; // Keep going up until we find hardhat.config or hit the root while (currentPath !== path.parse(currentPath).root) { const hardhatConfigTs = path.join(currentPath, 'hardhat.config.ts'); const hardhatConfigJs = path.join(currentPath, 'hardhat.config.js'); if (fs.existsSync(hardhatConfigTs) || fs.existsSync(hardhatConfigJs)) { return currentPath; } currentPath = path.dirname(currentPath); } return null; } // Helper function to get signer (can be imported from existing utils) async function getSigner() { const provider = await getProvider(); if (!process.env.PRIVATE_KEY) { throw new Error('PRIVATE_KEY environment variable is not set'); } return new ethers.Wallet(process.env.PRIVATE_KEY, provider); } exports.getSigner = getSigner; async function getProvider() { // Check if RPC_URL is set if (!process.env.RPC_URL) { throw new Error('RPC_URL environment variable is not set'); } // Create provider from RPC URL return new ethers_1.providers.JsonRpcProvider(process.env.RPC_URL); } exports.getProvider = getProvider; //# sourceMappingURL=hardhatHelpers.js.map