> ## Documentation Index
> Fetch the complete documentation index at: https://docs.biconomy.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Conditional Execution

> Attach runtime conditions to transactions

Conditions allow transactions to wait until specific requirements are met before executing.

## createCondition

Create a condition that must be satisfied for execution.

```typescript theme={null}
import { createCondition, ConditionType } from "@biconomy/abstractjs";

const condition = createCondition(options);
```

### Parameters

| Parameter        | Type                | Required | Description                                                       |
| ---------------- | ------------------- | -------- | ----------------------------------------------------------------- |
| `targetContract` | `Address`           | Yes      | Contract to call                                                  |
| `functionAbi`    | `Abi`               | Yes      | ABI containing the function                                       |
| `functionName`   | `string`            | Yes      | View/pure function name                                           |
| `args`           | `any[]`             | Yes      | Function arguments                                                |
| `value`          | `bigint \| boolean` | Yes      | Threshold or expected value; booleans are encoded via `toBytes32` |
| `type`           | `ConditionType`     | Yes      | Comparison type                                                   |
| `description`    | `string`            | No       | Human-readable description                                        |

### Returns

`Condition` - Can be passed to `buildComposable`

### Example

```typescript theme={null}
import { createCondition, ConditionType } from "@biconomy/abstractjs";
import { erc20Abi, parseUnits } from "viem";

const minBalance = createCondition({
  targetContract: USDC,
  functionAbi: erc20Abi,
  functionName: "balanceOf",
  args: [userAddress],
  value: parseUnits("100", 6),
  type: ConditionType.GTE,
  description: "User must have at least 100 USDC"
});
```

***

## ConditionType

```typescript theme={null}
enum ConditionType {
  GTE = "gte",  // Greater than or equal (≥)
  LTE = "lte",  // Less than or equal (≤)
  EQ = "eq"     // Equal (=)
}
```

### Usage Examples

```typescript theme={null}
// Balance must be at least 1000 tokens
createCondition({
  ...
  value: parseUnits("1000", 18),
  type: ConditionType.GTE
});

// Price must not exceed maximum
createCondition({
  ...
  value: maxPrice,
  type: ConditionType.LTE
});

// Contract must not be paused (paused = false = 0)
createCondition({
  ...
  value: 0n,
  type: ConditionType.EQ
});
```

***

## Adding Conditions to Instructions

```typescript theme={null}
const instruction = await account.buildComposable({
  type: "transfer",
  data: {
    chainId: 8453,
    tokenAddress: USDC,
    recipient: "0x...",
    amount: parseUnits("50", 6),
    conditions: [minBalanceCondition]  // Add conditions here
  }
});
```

***

## Multiple Conditions (AND Logic)

All conditions must pass for execution:

```typescript theme={null}
const instruction = await account.buildComposable({
  type: "default",
  data: {
    chainId: 8453,
    to: lendingProtocol,
    abi: lendingAbi,
    functionName: "borrow",
    args: [amount],
    conditions: [
      // Condition 1: Sufficient collateral
      createCondition({
        targetContract: collateralToken,
        functionAbi: erc20Abi,
        functionName: "balanceOf",
        args: [userAddress],
        value: minCollateral,
        type: ConditionType.GTE
      }),
      
      // Condition 2: Healthy position
      createCondition({
        targetContract: lendingProtocol,
        functionAbi: lendingAbi,
        functionName: "getHealthFactor",
        args: [userAddress],
        value: parseUnits("1.5", 18),
        type: ConditionType.GTE
      }),
      
      // Condition 3: Protocol not paused
      createCondition({
        targetContract: lendingProtocol,
        functionAbi: lendingAbi,
        functionName: "paused",
        args: [],
        value: 0n,
        type: ConditionType.EQ
      })
    ]
  }
});
```

***

## Waiting for Conditions

Set a timeout for how long to wait:

```typescript theme={null}
const quote = await meeClient.getFusionQuote({
  trigger,
  instructions: [instruction],
  feeToken,
  upperBoundTimestamp: Math.floor(Date.now() / 1000) + 300  // Wait up to 5 min
});
```

### Execution Flow

1. **Submit** - Transaction submitted with conditions
2. **PENDING** - MEE periodically checks conditions
3. **Satisfied** - All conditions pass → execute
4. **Timeout** - Conditions not met in time → fail

***

## Common Patterns

### Contract Not Paused

```typescript theme={null}
const pausableAbi = [{
  inputs: [],
  name: "paused",
  outputs: [{ name: "", type: "bool" }],
  stateMutability: "view",
  type: "function"
}] as const;

createCondition({
  targetContract: protocol,
  functionAbi: pausableAbi,
  functionName: "paused",
  args: [],
  value: 0n,  // false
  type: ConditionType.EQ
});
```

### Minimum Balance

```typescript theme={null}
createCondition({
  targetContract: USDC,
  functionAbi: erc20Abi,
  functionName: "balanceOf",
  args: [userAddress],
  value: parseUnits("50", 6),
  type: ConditionType.GTE
});
```

### Price Threshold

```typescript theme={null}
createCondition({
  targetContract: priceOracle,
  functionAbi: oracleAbi,
  functionName: "getPrice",
  args: [tokenAddress],
  value: targetPrice,
  type: ConditionType.GTE  // Wait for price to reach target
});
```

***

## Best Practices

| Practice                | Reason                     |
| ----------------------- | -------------------------- |
| Match decimal precision | USDC = 6, WETH = 18        |
| Limit to 1-3 conditions | Each adds \~5-10k gas      |
| Use `as const` for ABIs | Enables type inference     |
| Set reasonable timeouts | Prevent indefinite waiting |
| Add descriptions        | Improves debugging         |
