@biconomy/abstractjs
Version:
SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.
48 lines • 1.95 kB
JavaScript
import { buildBatch } from "../decorators/instructions/buildBatch.js";
/**
* Groups consecutive instructions with the same chainId into batches.
* Instructions can only be batched if they are consecutive and share the same chainId.
*
* @param parameters - The parameters for the batching.
* @param parameters.account - The account to execute the instructions on.
* @param parameters.triggerCall - The first instruction to be executed.
* @param parameters.instructions - The remaining instructions to be executed.
*
* @returns An array of instructions, where consecutive same-chain instructions are batched together.
*/
export const batchInstructions = async (parameters) => {
const { account, instructions } = parameters;
const result = [];
let currentBatch = [];
let currentChainId = null;
for (const instruction of instructions) {
const chainId = String(instruction.chainId);
if (currentChainId === null || chainId === currentChainId) {
currentBatch.push(instruction);
currentChainId = chainId;
}
else {
// Chain ID changed, process current batch
if (currentBatch.length > 1) {
const [batchedOp] = await buildBatch({ account }, { instructions: currentBatch });
result.push(batchedOp);
}
else if (currentBatch.length === 1) {
result.push(currentBatch[0]);
}
// Start new batch with current instruction
currentBatch = [instruction];
currentChainId = chainId;
}
}
// Process the final batch
if (currentBatch.length > 1) {
const [batchedOp] = await buildBatch({ account }, { instructions: currentBatch });
result.push(batchedOp);
}
else if (currentBatch.length === 1) {
result.push(currentBatch[0]);
}
return result;
};
//# sourceMappingURL=batchInstructions.js.map