UNPKG

@lifi/composer-sdk

Version:

Public Composer SDK for building and submitting flows

95 lines (94 loc) • 4.2 kB
import { handleToRef } from "./handles.js"; import { parseAbiItem } from "abitype"; //#region src/authoring/signatureArgs.ts /** * Parses a Solidity function signature and returns parameter names in order. * Delegates to abitype's runtime `parseAbiItem` — the same parser that powers * the compile-time `ParseAbiItem<TSig>` type — so runtime and type-level * parsing can never diverge. * * @throws if the signature is not a valid function signature or a parameter is unnamed */ const parseFunctionParams = (functionSignature) => { const parsed = parseAbiItem(functionSignature); if (parsed.type !== "function") throw new Error(`core.call: expected a function signature, got "${parsed.type}": "${functionSignature}"`); return parsed.inputs.map((input, index) => { if (!input.name) throw new Error(`core.call: parameter at index ${index} in signature has no name. All parameters must be named for named binds to work.`); return input.name; }); }; /** * Converts a ref-resolving bindable to a `Ref` (`{ $ref: string }`). * Handles carry a `_tag` property and are converted via `handleToRef`; * `Ref` objects pass through as-is. */ const toBindRef = (bindable) => { if ("_tag" in bindable) return handleToRef(bindable); return bindable; }; /** * Converts one signature-derived bind to its positional `config.args` entry: a * ref-resolving bindable becomes `{ $ref }`; a literal becomes its `value` * verbatim, which the backend encodes against the parameter's ABI type (a * decimal or hex integer, a hex address or byte string, `'true'`/`'false'`). */ const toPositionalArg = (bindable) => "kind" in bindable ? bindable.value : toBindRef(bindable); /** * Parses a function signature from `config`, validates bind keys against it, * and converts named binds to positional args. */ const resolvePositionalArgs = (label, bind, config) => { const functionSignature = config.functionSignature; if (typeof functionSignature !== "string") throw new Error(`${label}: config.functionSignature is required and must be a string`); const paramNames = parseFunctionParams(functionSignature); const paramSet = new Set(paramNames); const bindKeys = Object.keys(bind); const bindSet = new Set(bindKeys); const missing = paramNames.filter((n) => !bindSet.has(n)); const extra = bindKeys.filter((k) => !paramSet.has(k)); if (missing.length > 0 || extra.length > 0) throw new Error(`${label}: bind keys [${bindKeys.join(", ")}] do not match signature parameters [${paramNames.join(", ")}]. Missing: ${missing.join(", ") || "(none)"}. Extra: ${extra.join(", ") || "(none)"}.`); return paramNames.map((name) => { const bindable = bind[name]; if (bindable === void 0) throw new Error(`${label}: bind value for parameter "${name}" is undefined`); return toPositionalArg(bindable); }); }; /** * Transforms the user-facing `resource` + named `bind` API into the wire * format that the backend expects: * - With resource: `core.call` with `bind: { input: <resourceRef> }` * - Without resource: `core.invoke` with `bind: {}` * - `config: { ...userConfig, args: [positional refs and literal values...] }` */ const buildCallWireFormat = (args) => { const positionalArgs = resolvePositionalArgs("core.call", args.bind, args.config); const hasResource = args.resource !== void 0; return { op: hasResource ? "core.call" : "core.invoke", bind: hasResource ? { input: toBindRef(args.resource) } : {}, config: { ...args.config, args: positionalArgs }, ...args.guards && args.guards.length > 0 && { guards: args.guards } }; }; /** * Transforms the user-facing named `bind` API into the wire format that the * backend expects for `core.staticCall`. Same as `buildCallWireFormat` but * with no `resource` field — staticCall has no resource inputs. */ const buildStaticCallWireFormat = (args) => { const positionalArgs = resolvePositionalArgs("core.staticCall", args.bind, args.config); return { bind: {}, config: { ...args.config, args: positionalArgs }, ...args.guards && args.guards.length > 0 && { guards: args.guards } }; }; //#endregion export { buildCallWireFormat, buildStaticCallWireFormat, parseFunctionParams, toBindRef }; //# sourceMappingURL=signatureArgs.js.map