UNPKG

@lifi/composer-sdk

Version:

Public Composer SDK for building and submitting flows

87 lines 2.75 kB
import { parseAbiItem } from "abitype"; import { handleToRef } from "./handles.js"; 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; }); }; const toBindRef = (bindable) => { if ("_tag" in bindable) return handleToRef(bindable); return bindable; }; 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 toBindRef(bindable); }); }; 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 } }; }; 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 } }; }; export { buildCallWireFormat, buildStaticCallWireFormat, parseFunctionParams, toBindRef }; //# sourceMappingURL=signatureArgs.js.map