UNPKG

synkrokonn-dev

Version:

Plugin-based cross-chain orchestration middleware for Web3 enterprise automation.

96 lines (95 loc) 3.91 kB
import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { FabricAdapter } from '../adapters/fabric/FabricAdapter.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); export class OrchestratorEngine { constructor(workflowFile, adapters, pluginManager) { let resolvedPath = ''; // First try relative to user's current working directory const cwdPath = path.resolve(process.cwd(), workflowFile); if (fs.existsSync(cwdPath)) { resolvedPath = cwdPath; } else { // Fallback: try relative to __dirname (inside compiled npm package dist folder) const fallbackPath = path.resolve(__dirname, '..', '..', workflowFile); if (fs.existsSync(fallbackPath)) { resolvedPath = fallbackPath; } else { console.error(`[ERROR] Workflow file not found at: ${cwdPath}`); console.error(`[ERROR] Workflow file not found at: ${fallbackPath}`); throw new Error(`Workflow file not found in both locations.`); } } console.log(`[DEBUG] Final resolved workflow path: ${resolvedPath}`); this.workflow = this.loadWorkflow(resolvedPath); this.chainAdapters = adapters; this.pluginManager = pluginManager; } loadWorkflow(resolvedPath) { try { const content = fs.readFileSync(resolvedPath, 'utf-8'); return JSON.parse(content); } catch (err) { console.error(`[ERROR] Failed to parse workflow JSON: ${err.message}`); throw err; } } async start() { const context = { id: Date.now().toString(), timestamp: new Date().toISOString(), metadata: {}, }; await this.pluginManager.onStart(context); for (const step of this.workflow.steps) { try { // Fabric chain if (step.chain === 'fabric_chain') { const fabric = new FabricAdapter(); await fabric.connect(); const data = await fabric.query(step.functionName, step.args); console.log('Fabric Output:', data); await fabric.disconnect(); await this.pluginManager.onEvent('StepSuccess', { step, context, result: data }); continue; } // EVM chain const adapter = this.chainAdapters[step.chain]; if (!adapter) throw new Error(`No adapter found for chain: ${step.chain}`); if (step.htlc) { await this.pluginManager.onEvent('HTLC_CREATE', { step, context, htlc: step.htlc }); } if (step.reversible) { await this.pluginManager.onEvent('REGISTER_ROLLBACK', { step, context }); } if (step.domainObject) { await this.pluginManager.onEvent('DOMAIN_OBJECT', { object: step.domainObject, context, }); } const types = step.args.map((arg) => arg.type); const values = step.args.map((arg) => arg.value); await adapter.sendTransaction(step.contractAddress, step.functionName, { types, values, }); await this.pluginManager.onEvent('StepSuccess', { step, context, result: 'success', }); } catch (err) { await this.pluginManager.onError(err, { step, context }); break; } } } }