UNPKG

@biconomy/abstractjs

Version:

SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.

46 lines 2.12 kB
import { resolveInstructions } from "../../utils/index.js"; /** * Builds a batch of instructions for a single userOp to be included in a supertransaction * UserOps must be on the same chain for the batch to be valid * @param baseParams - Base parameters for the instruction * @param parameters - Parameters for the batch instruction * @param parameters.instructions - Instructions to be executed in the batch * @returns The built instruction * * @example * ```typescript * const instructions = await buildBatch( * { account: myMultichainAccount }, * { instructions: [buildApprove, buildSwap] } * ) * ``` */ export const buildBatch = async (baseParams, parameters) => { const { currentInstructions = [] } = baseParams; const { instructions } = parameters; if (instructions.length < 2) { throw new Error("A Batch must contain at least 2 instructions"); } const resolvedInstructions = await resolveInstructions(instructions); if (resolvedInstructions.some(({ chainId }) => Number(chainId) !== Number(resolvedInstructions[0].chainId))) { throw new Error("All instructions must be on the same chain"); } const isComposable = resolvedInstructions.some(({ isComposable }) => isComposable === true); // If any one instruction in a batch is composable, every other instructions should follow composable encoding. if (!resolvedInstructions.every((inx) => !!inx.isComposable === !!isComposable)) { throw new Error(`${isComposable ? "All the instructions must be built with buildComposable in order to support the runtime time parameters." : "All the instructions must be non composable when there are no runtime parameters"}`); } const calls = resolvedInstructions.flatMap(({ calls }) => calls); return [ ...currentInstructions, { calls: isComposable ? calls : calls, chainId: resolvedInstructions[0].chainId, // Batch instructions must be on the same chain isComposable } ]; }; export default buildBatch; //# sourceMappingURL=buildBatch.js.map