@lifi/composer-sdk
Version:
Public Composer SDK for building and submitting flows
104 lines (103 loc) • 3 kB
JavaScript
import { toBindValue } from "./handles.js";
import { ref } from "./raw.js";
import { familySize, instancePortName } from "@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] = 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 = familySize({
name: prop,
countFrom
}, config);
return Array.from({ length: size }, (_, index) => outputHandle(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: ref("context.sender"),
executionAddress: ref("context.executionAddress")
},
inputs: inputHandles,
call,
untypedOp,
build
};
};
export { createFlowBuilderCore };
//# sourceMappingURL=FlowBuilderCore.js.map