UNPKG

dop-stick

Version:

Source control tooling for versionable-upgradeable smart contracts

202 lines 8.68 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.initializeEnvironment = exports.ensureSuffix = exports.safeStringify = exports.isDefined = exports.formatDuration = exports.sleep = exports.configLoader = exports.InitializationHelper = exports.diamondHelper = exports.DiamondCutAction = exports.DiamondInfo = exports.mine = exports.deployModule = exports.upgrade = exports._initializeSDK = void 0; const dotenv_1 = __importDefault(require("dotenv")); const logger_1 = require("./utils/logsAndMetrics/core/logger"); const introAdapter_1 = require("./utils/logsAndMetrics/adapters/introAdapter"); const compilerService_1 = require("./utils/compiler/compilerService"); // Private initialization tracking let _isInitialized = false; // Separate intro function that can be async async function _playIntro() { const intro = new introAdapter_1.IntroAdapter(); await intro.playIntro(""); } // Keep initialization sync for proxy usage function _initializeSDK() { if (_isInitialized) return; dotenv_1.default.config(); logger_1.Logger.debug('DopStick SDK initialized with environment:', { NETWORK: process.env.NETWORK, RPC_URL: process.env.RPC_URL, DIAMOND_ADDRESS: process.env.DIAMOND_ADDRESS }); _isInitialized = true; } exports._initializeSDK = _initializeSDK; // Modified proxy creator to handle async intro and compilation function createFunctionProxy(fn) { return (async (...args) => { var _a; _initializeSDK(); await _playIntro(); // Always call ensureCompiled, let it handle the case when compiler is undefined await compilerService_1.CompilerService.ensureCompiled((_a = args[0]) === null || _a === void 0 ? void 0 : _a.compiler); return fn(...args); }); } // Import your core functionality const upgrade_1 = require("./upgrade"); const mine_1 = require("./mine"); const info_1 = require("./info"); // Export proxied core functionality /** * Upgrades an existing diamond contract with new or modified facets. * Handles the entire upgrade process including compilation, deployment, and diamond cuts. * * @param config - Configuration object for the upgrade process * @param config.paths - Contract and artifact paths * @param config.paths.contracts - Path to contract source files * @param config.paths.artifacts - Path to compiled contract artifacts * @param config.compiler - Compiler settings * @param config.compiler.name - Compiler name (e.g., 'hardhat', 'foundry') * @param config.modules - Array of module names to upgrade * @param config.parallelization - Parallel processing settings * @param config.parallelization.enabled - Enable parallel processing * @param config.parallelization.maxConcurrent - Maximum concurrent operations * @param config.fail_all_if_one_fails - Stop all upgrades if any module fails * * @returns Promise<UpgradeContext> - Detailed results of the upgrade process * @throws {Error} If compilation fails, deployment fails, or diamond cut fails * * Example usage: * ```ts * const config = { * paths: { * contracts: './contracts', * artifacts: './artifacts' * }, * compiler: { * name: 'hardhat' * }, * modules: ['TokenModule', 'StakingModule'], * parallelization: { * enabled: true, * maxConcurrent: 3 * }, * fail_all_if_one_fails: true * }; * * const result = await upgrade(config); * ``` * * The upgrade process includes: * 1. Compilation check of contracts * 2. Parallel or sequential module deployment * 3. Pre-deployment validation * 4. Batched diamond cuts * 5. Automatic retry with different cut actions on failure * 6. Detailed reporting and gas usage tracking */ exports.upgrade = createFunctionProxy(upgrade_1.upgrade); /** * Deploys a new module to be used with a diamond contract. * * @param config - Configuration for module deployment * @returns Promise<string> - Deployed module address * * Example: * ```ts * const moduleAddress = await deployModule({ * paths: { contracts: './contracts' }, * module: 'MyNewModule' * }); * ``` */ exports.deployModule = createFunctionProxy(upgrade_1.deployModule); /** * Deploys a new diamond contract with specified facets. * * @param config - Configuration for diamond deployment * @returns Promise<string> - Deployed diamond address * * Example: * ```ts * const diamondAddress = await mine({ * paths: { contracts: './contracts' }, * modules: ['BaseModule', 'ExtendedModule'], * compiler: { name: 'hardhat' } * }); * ``` */ exports.mine = createFunctionProxy(mine_1.mine); // Create class proxy creator function createClassProxy(OriginalClass) { return class extends OriginalClass { constructor(...args) { super(...args); // Initialize the promise right away this.__initialized = new Promise((resolve, reject) => { var _a; const isCliCall = args[2] === true; // Check if it's a CLI call // If CLI call, skip intro and compilation (already done) if (isCliCall) { _initializeSDK(); resolve(); return; } // For programmatic usage, do full initialization _initializeSDK(); compilerService_1.CompilerService.ensureCompiled((_a = args[1]) === null || _a === void 0 ? void 0 : _a.compiler) .then(resolve) .catch(error => { logger_1.Logger.error('Initialization failed:', error instanceof Error ? error.message : String(error)); reject(error); }); }); } // Helper method to ensure initialization is complete async ensureInitialized() { await this.__initialized; } // Override async methods to wait for initialization async getBasicInfo(...args) { await this.ensureInitialized(); return super.getBasicInfo(...args); } async getFacets(...args) { await this.ensureInitialized(); return super.getFacets(...args); } async getFacetInfo(...args) { await this.ensureInitialized(); return super.getFacetInfo(...args); } async getDetailedFacetsInfo(...args) { await this.ensureInitialized(); return super.getDetailedFacetsInfo(...args); } async generateDocumentation(...args) { await this.ensureInitialized(); return super.generateDocumentation(...args); } }; } // Create the proxied version const DiamondInfo = createClassProxy(info_1.BaseDiamondInfo); exports.DiamondInfo = DiamondInfo; // Export other utilities (no need to proxy these as they'll be called through the main functions) var diamond_1 = require("./types/diamond"); Object.defineProperty(exports, "DiamondCutAction", { enumerable: true, get: function () { return diamond_1.DiamondCutAction; } }); var diamondHelpers_1 = require("./utils/diamondHelpers"); Object.defineProperty(exports, "diamondHelper", { enumerable: true, get: function () { return diamondHelpers_1.diamondHelper; } }); var initialization_1 = require("./utils/initialization"); Object.defineProperty(exports, "InitializationHelper", { enumerable: true, get: function () { return initialization_1.InitializationHelper; } }); var configLoader_1 = require("./utils/configLoader"); Object.defineProperty(exports, "configLoader", { enumerable: true, get: function () { return configLoader_1.configLoader; } }); var common_1 = require("./utils/common"); Object.defineProperty(exports, "sleep", { enumerable: true, get: function () { return common_1.sleep; } }); Object.defineProperty(exports, "formatDuration", { enumerable: true, get: function () { return common_1.formatDuration; } }); Object.defineProperty(exports, "isDefined", { enumerable: true, get: function () { return common_1.isDefined; } }); Object.defineProperty(exports, "safeStringify", { enumerable: true, get: function () { return common_1.safeStringify; } }); Object.defineProperty(exports, "ensureSuffix", { enumerable: true, get: function () { return common_1.ensureSuffix; } }); // Export the initialization function for manual use if needed function initializeEnvironment() { _initializeSDK(); } exports.initializeEnvironment = initializeEnvironment; //# sourceMappingURL=index.js.map