UNPKG

@biconomy/abstractjs

Version:

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

107 lines 4.09 kB
import { encodeFunctionData } from "viem"; import { TokenWithPermitAbi } from "../../../constants/abi/TokenWithPermitAbi.js"; import { isComposableCallRequired, isRuntimeComposableValue } from "../../../modules/utils/composabilityCalls.js"; import { getFunctionContextFromAbi } from "../../../modules/utils/runtimeAbiEncoding.js"; import { addressEquals } from "../../utils/index.js"; import { buildComposableCall } from "./buildComposable.js"; /** * Builds an instruction for transferring tokens. This function creates the necessary * instruction for a standard ERC20 transfer. * * @param baseParams - Base configuration for the instruction * @param baseParams.account - The account that will execute the transfer * @param baseParams.currentInstructions - Optional array of existing instructions to append to * @param parameters - Parameters for the transfer * @param parameters.chainId - Chain ID where the transfer will be executed * @param parameters.tokenAddress - Address of the token to transfer * @param parameters.amount - Amount to transfer * @param [parameters.gasLimit] - Optional gas limit for the transfer * @param [parameters.recipient] - Optional recipient address * * @returns Promise resolving to array of instructions * * @example * ```typescript * const instructions = await buildWithdrawal( * { account: myMultichainAccount }, * { * chainId: 1, * tokenAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC * amount: 1000000n, // 1 USDC * gasLimit: 65000n, * recipient: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e" * } * ); * ``` */ export const buildWithdrawal = async (baseParams, parameters, forceComposableEncoding = false, efficientMode = true) => { const { currentInstructions = [], account } = baseParams; const { chainId, tokenAddress, amount, gasLimit, recipient = account.signer.address } = parameters; const isNativeToken = addressEquals(tokenAddress, "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"); let triggerCalls; if (isNativeToken) { if (isRuntimeComposableValue(amount)) { throw new Error("Runtime balance is not supported for Native tokens"); } triggerCalls = [ { to: recipient, value: amount, ...(gasLimit ? { gasLimit } : {}) } ]; } else { const abi = TokenWithPermitAbi; const functionSig = "transfer"; const args = [ recipient, amount ]; const functionContext = getFunctionContextFromAbi(functionSig, abi); // Check for the runtime arguments and detect the need for composable call const isComposableCall = forceComposableEncoding ? true : isComposableCallRequired(functionContext, args); // If the composable call is detected ? The call needs to composed with runtime encoding if (isComposableCall) { const composableCallParams = { to: tokenAddress, functionName: functionSig, args: args, abi, chainId, ...(gasLimit ? { gasLimit } : {}) }; triggerCalls = await buildComposableCall(baseParams, composableCallParams, efficientMode); return [ ...currentInstructions, { calls: triggerCalls, chainId, isComposable: true } ]; } triggerCalls = [ { to: tokenAddress, data: encodeFunctionData({ abi, functionName: functionSig, args: args }), ...(gasLimit ? { gasLimit } : {}) } ]; } return [ ...currentInstructions, { calls: triggerCalls, chainId } ]; }; export default buildWithdrawal; //# sourceMappingURL=buildWithdrawal.js.map