@biconomy/abstractjs
Version:
SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.
67 lines • 2.63 kB
JavaScript
import { isAddress } from "viem";
import { prepareRawComposableParams } from "../../../modules/utils/composabilityCalls.js";
/**
* Builds an instruction for raw composable transaction. This is a generic function which creates the raw composable instructions
* to execute against composability stack
*
* @param baseParams - Base configuration for the instruction
* @param baseParams.account - The account that will execute the composable transaction
* @param baseParams.currentInstructions - Optional array of existing instructions to append to
* @param parameters - Parameters for generate composable instruction
* @param parameters.to - Address of the target contract address
* @param parameters.calldata - Raw calldata of the solidity function call
* @param parameters.chainId - Chain where the composable transaction will be executed
* @param [parameters.gasLimit] - Optional gas limit
* @param [parameters.value] - Optional value
*
* @returns Promise resolving to array of instructions
*
* @example
* ```typescript
* const instructions = buildRawComposable(
* { account: myMultichainAccount },
* {
* to: targetContractAddress,
* calldata: '0x000000',
* chainId: baseSepolia.id
* }
* )
* ```
*/
export const buildRawComposable = async (baseParams, parameters) => {
const { currentInstructions = [] } = baseParams;
const { account } = baseParams;
const { to, calldata, gasLimit, value, chainId } = parameters;
if (!isAddress(to)) {
throw new Error("Invalid target contract address");
}
const smartAccountAddress = account.addressOn(chainId, true);
if (!isAddress(smartAccountAddress)) {
throw new Error("Invalid smart account address");
}
if (calldata.length < 10 || !calldata.startsWith("0x")) {
throw new Error("Invalid calldata");
}
const functionSig = calldata.slice(0, 10);
const composableParams = prepareRawComposableParams(`0x${calldata.slice(10)}`);
const composableCalls = [];
const composableCall = {
to,
value: value ?? BigInt(0),
functionSig,
inputParams: composableParams,
outputParams: [], // In the current scope, output params are not handled. When more composability functions are added, this will change
...(gasLimit ? { gasLimit } : {})
};
composableCalls.push(composableCall);
return [
...currentInstructions,
{
calls: composableCalls,
chainId,
isComposable: true
}
];
};
export default buildRawComposable;
//# sourceMappingURL=buildRawComposable.js.map