@biconomy/abstractjs
Version:
SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.
83 lines • 3.2 kB
JavaScript
import { encodeAbiParameters, encodeFunctionData } from "viem";
import { InputParamFetcherType, equalTo, greaterThanOrEqualTo, lessThanOrEqualTo, prepareInputParam, validateAndProcessConstraints } from "./composabilityCalls.js";
/**
* Enum for condition constraint types
*/
export var ConditionType;
(function (ConditionType) {
/** Greater than or equal to */
ConditionType["GTE"] = "gte";
/** Less than or equal to */
ConditionType["LTE"] = "lte";
/** Equal to */
ConditionType["EQ"] = "eq";
})(ConditionType || (ConditionType = {}));
/**
* Creates an InputParam for a condition using STATIC_CALL fetcher type.
* The resulting InputParam will be appended to the function's regular parameters.
*
* @param condition - The condition to convert to an InputParam
* @returns InputParam configured for STATIC_CALL with constraint validation
*
* @internal
*/
export const createConditionInputParam = (condition) => {
// Encode the static call: (address, bytes)
const encodedParam = encodeAbiParameters([{ type: "address" }, { type: "bytes" }], [
condition.targetContract,
encodeFunctionData({
abi: condition.functionAbi,
functionName: condition.functionName,
args: condition.args
})
]);
// Process constraint into smart contract format
const constraintsToAdd = validateAndProcessConstraints([condition.constraint]);
return prepareInputParam(InputParamFetcherType.STATIC_CALL, encodedParam, constraintsToAdd);
};
/**
* Creates a condition with the specified constraint type and value.
* This is the unified helper function used by all condition builders.
*
* @typeParam TAbi - The contract ABI type
* @typeParam TFunctionName - The name of the view/pure function to call
*
* @param params - Condition parameters
* @param params.targetContract - Contract to call
* @param params.functionAbi - ABI containing the function
* @param params.functionName - Function to call (must be view or pure)
* @param params.args - Function arguments (type-safe based on ABI)
* @param params.value - The value to compare against (threshold or expected value)
* @param params.type - The constraint type (GTE, LTE, or EQ)
* @param params.description - Optional description
* @returns Configured condition with type-safe arguments
*
* @example
* ```typescript
* const condition = createCondition({
* targetContract: tokenAddress,
* functionAbi: erc20Abi,
* functionName: "balanceOf",
* args: [userAddress],
* value: 1000n,
* type: ConditionType.GTE
* })
* ```
*/
export const createCondition = (params) => {
// Map ConditionType enum to the appropriate constraint function
const constraintMap = {
[ConditionType.GTE]: greaterThanOrEqualTo,
[ConditionType.LTE]: lessThanOrEqualTo,
[ConditionType.EQ]: equalTo
};
return {
targetContract: params.targetContract,
functionAbi: params.functionAbi,
functionName: params.functionName,
args: params.args,
constraint: constraintMap[params.type](params.value),
description: params.description
};
};
//# sourceMappingURL=conditions.js.map