UNPKG

@biconomy/abstractjs

Version:

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

82 lines 3.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildBatch = void 0; const utils_1 = require("../../utils/index.js"); 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 (0, utils_1.resolveInstructions)(instructions); let maxTimeWindow = 0; let maxExecutionSimulationRetryDelay = 0; let finalLowerBoundTimestamp = 0; let finalUpperBoundTimestamp = 0; const customOverrides = []; const tokenOverrides = []; for (const { lowerBoundTimestamp, upperBoundTimestamp, executionSimulationRetryDelay, simulationOverrides } of resolvedInstructions) { if (executionSimulationRetryDelay !== undefined) { const isMax = executionSimulationRetryDelay >= maxExecutionSimulationRetryDelay; if (isMax) { maxExecutionSimulationRetryDelay = executionSimulationRetryDelay; } } if (lowerBoundTimestamp !== undefined && upperBoundTimestamp !== undefined) { if (upperBoundTimestamp <= lowerBoundTimestamp) { throw new Error("Invalid lowerbound and upperbound timestamps."); } const timeWindow = upperBoundTimestamp - lowerBoundTimestamp; const isMax = timeWindow >= maxTimeWindow; if (isMax) { maxTimeWindow = timeWindow; finalLowerBoundTimestamp = lowerBoundTimestamp; finalUpperBoundTimestamp = upperBoundTimestamp; } } if (simulationOverrides !== undefined) { if (simulationOverrides.customOverrides !== undefined) { customOverrides.push(...simulationOverrides.customOverrides); } if (simulationOverrides.tokenOverrides !== undefined) { tokenOverrides.push(...simulationOverrides.tokenOverrides); } } } 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 (!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); const metadata = resolvedInstructions.flatMap(({ metadata }) => metadata || []); return [ ...currentInstructions, { calls: isComposable ? calls : calls, chainId: resolvedInstructions[0].chainId, isComposable, metadata, ...(finalLowerBoundTimestamp !== 0 ? { lowerBoundTimestamp: finalLowerBoundTimestamp } : {}), ...(finalUpperBoundTimestamp !== 0 ? { upperBoundTimestamp: finalUpperBoundTimestamp } : {}), ...(maxExecutionSimulationRetryDelay !== 0 ? { executionSimulationRetryDelay: maxExecutionSimulationRetryDelay } : {}), ...(tokenOverrides.length > 0 || customOverrides.length > 0 ? { simulationOverrides: { tokenOverrides, customOverrides } } : {}) } ]; }; exports.buildBatch = buildBatch; exports.default = exports.buildBatch; //# sourceMappingURL=buildBatch.js.map