@lifi/composer-sdk
Version:
Public Composer SDK for building and submitting flows
114 lines • 3.99 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var signatureArgs_exports = {};
__export(signatureArgs_exports, {
buildCallWireFormat: () => buildCallWireFormat,
buildStaticCallWireFormat: () => buildStaticCallWireFormat,
parseFunctionParams: () => parseFunctionParams,
toBindRef: () => toBindRef
});
module.exports = __toCommonJS(signatureArgs_exports);
var import_abitype = require("abitype");
var import_handles = require("./handles.js");
const parseFunctionParams = (functionSignature) => {
const parsed = (0, import_abitype.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 (0, import_handles.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 }
};
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
buildCallWireFormat,
buildStaticCallWireFormat,
parseFunctionParams,
toBindRef
});
//# sourceMappingURL=signatureArgs.cjs.map