UNPKG

@lifi/composer-sdk

Version:

Public Composer SDK for building and submitting flows

105 lines (104 loc) • 3.24 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_authoring_handles = require("./handles.cjs"); const require_authoring_raw = require("./raw.cjs"); let _lifi_compose_spec = require("@lifi/compose-spec"); // src/authoring/FlowBuilderCore.ts const isResource = (decl) => typeof decl === "object" && decl !== null && "kind" in decl; /** * Creates a new flow builder targeting the given chain. * * @param chainId - The EVM chain ID (e.g. `1` for Ethereum mainnet). * @param options - Flow configuration including input declarations. * @returns A {@link FlowBuilderInternal} instance. * @internal */ const createFlowBuilderCore = (chainId, options) => { const id = options.name ?? crypto.randomUUID(); const flowInputs = []; const nodes = []; const nodeIds = /* @__PURE__ */ new Set(); const inputHandles = {}; for (const [name, decl] of Object.entries(options.inputs)) if (isResource(decl)) { if (decl.chainId !== chainId) throw new Error(`Input "${name}" has chainId ${decl.chainId} but flow targets chain ${chainId}`); flowInputs.push({ name, resource: decl }); inputHandles[name] = { _tag: "input", inputName: name, resource: decl }; } else { flowInputs.push({ name, type: decl }); inputHandles[name] = { _tag: "input", inputName: name }; } const call = (nodeId, op, args, outputs, families) => { if (nodeIds.has(nodeId)) throw new Error(`Duplicate node id: "${nodeId}"`); nodeIds.add(nodeId); const bind = {}; for (const [key, val] of Object.entries(args.bind)) bind[key] = require_authoring_handles.toBindValue(val); const config = args.config ?? {}; const node = { id: nodeId, op, bind, config, ...args.guards && args.guards.length > 0 && { guards: args.guards } }; nodes.push(node); const outputHandle = (portName) => ({ _tag: "output", nodeId, portName }); return new Proxy({}, { get: (_target, prop) => { if (typeof prop !== "string" || prop === "then") return void 0; if (!Object.hasOwn(outputs, prop)) throw new Error(`Op "${op}" has no output port "${prop}". Valid ports: ${Object.keys(outputs).join(", ")}`); const countFrom = families?.[prop]; if (countFrom === void 0) return outputHandle(prop); const size = (0, _lifi_compose_spec.familySize)({ name: prop, countFrom }, config); return Array.from({ length: size }, (_, index) => outputHandle((0, _lifi_compose_spec.instancePortName)(prop, index))); } }); }; const untypedOp = (nodeId, op, args) => { if (nodeIds.has(nodeId)) throw new Error(`Duplicate node id: "${nodeId}"`); nodeIds.add(nodeId); const node = { id: nodeId, op, bind: args.bind, config: args.config, ...args.guards && args.guards.length > 0 && { guards: args.guards } }; nodes.push(node); }; const build = () => ({ version: 1, id, chainId, inputs: [...flowInputs], nodes: [...nodes] }); return { context: { sender: require_authoring_raw.ref("context.sender"), executionAddress: require_authoring_raw.ref("context.executionAddress") }, inputs: inputHandles, call, untypedOp, build }; }; exports.createFlowBuilderCore = createFlowBuilderCore; //# sourceMappingURL=FlowBuilderCore.cjs.map