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

# Guards

> How Composer simulates a flow before returning calldata, and how guards turn simulated values into checks enforced onchain.

A **guard** is an invariant attached to an op output. Composer measures the output's value in a simulation before it returns calldata, then writes a check into the transaction so the invariant holds onchain when the transaction runs.

## When the simulation runs

Composer simulates the flow against the latest state of its chain whenever the response needs a measured value: a guard to evaluate, a terminal output to report, an epilogue forward to measure, or a handle an op exposes. Measured amounts appear as `outputs[key].amount.estimate` with `basis: "simulated"`. A flow that needs none of these skips the simulation, and its estimates come from quotes (`basis: "quoted"`).

The simulation assumes the signer holds the amounts the flow pulls and has granted its approvals. It is not byte-for-byte the transaction the user signs. To also simulate that exact transaction, set `run.simulateUserProgram: true`; a success is echoed as `userProgramSimulation`.

## What a guard is

A guard's check is part of the final calldata, so the transaction itself enforces the invariant; nothing relaxes it after the simulation.

Production serves three guards:

| Guard | Attaches to | What it asserts onchain |
| - | - | - |
| `slippage` | A resource output or handle | The value is at least `simulated × (1 − bps / 10000)`. |
| `band` | A resource output or handle | The value stays within `bps` of the simulated value, in both directions. |
| `core.numericInvariant` | A `uint256` handle output | The value compares to a constant `threshold` with `gte`, `gt`, `lte`, `lt`, `eq` or `neq`. |

See the [Guards catalog](https://docs.li.fi/composer/composer-api/guards.md) for each guard's config.

## When guards run

The simulation reads the value of every port a guard observes. Composer then turns each guard into a check with a bound derived from that value, and compiles the checks into the calldata it returns. The checks run onchain, inside the user's transaction.

A guard can fail at three points:

* **At compile time, `guard_error` (HTTP 422).** The guard cannot be attached: it targets a port that does not exist, has the wrong kind, or cannot be guarded.
* **During simulation, `simulation_revert` (HTTP 422).** The simulated flow reverted. Under `simulationPolicy: "allow-revert"` the calldata is returned as HTTP 206 with the revert diagnostics instead.
* **Onchain.** A `slippage` or `band` check cannot fail in the simulation that set its bound. It fails if the chain state moves past the bound before the transaction lands, and the transaction then reverts.

## Where guards can attach

A guard's `compatibility.selectors` list says which ports it can attach to, and the compile rejects any other attachment. Two kinds of port are **unguardable** for every guard:

* **Ports that declare `providesMinimum: true`.** The op already enforces its own minimum. `lifi.swap`'s `amountOut` is the canonical example: its `slippage` config sets `minOut`. See [`providesMinimum`](https://docs.li.fi/composer/composer-api/concepts/resources-and-ports#providesminimum) for the other ports.
* **Ports with `availability: "future"`.** A bridge, LI.FI Intents order or `continuation.settle` output lands after the transaction. Its simulated value is an estimate made in the initiating transaction, so a guard on it would enforce nothing.

`lifi.zap`'s `amountOut` does **not** provide its own minimum, which is why the [Swap and deposit recipe](https://docs.li.fi/composer/composer-api/recipes/swap-and-deposit.md) attaches a `slippage` guard there. In a cross-chain flow, the minimum received comes from the settle op, not from a guard on it; to protect the destination flow itself, attach a guard to an op inside it. See [Cross-chain flows](https://docs.li.fi/composer/composer-api/concepts/cross-chain-flows.md).

The manifest (`GET /compose/manifest`) shows which output ports carry `providesMinimum` or `future` availability.

## Guaranteed minimums

A `slippage` guard also produces a **guaranteed minimum**: the worst-case value of the port given the guard's tolerance. The response reports it as `outputs[key].amount.minimum = { value, basis: "guard" }` for the resource output the guard protects. Your UI can show "you will receive at least X" from that field.

Ops produce floors too (`basis: "op"`, for example a swap's `minOut`). When both exist, the larger floor wins; it is tagged `guard` only when the guard floor is strictly larger. With several guards on one port, the largest minimum wins. A `band` guard reports no minimum: it bounds the value in both directions but promises no amount.

A guard on a **handle** port produces no reported minimum, because `outputs` lists only resource outputs. To surface one, turn the handle into a resource with `core.asResource` and guard that resource.

## Common guard failures

| Failure | Cause |
| - | - |
| `Guard targets port "<p>" but it provides its own minimum -- attach the guard to a port that does not declare providesMinimum` | The port's op already declares `providesMinimum`. Remove the guard. |
| `Guard targets port "<p>" but its availability is 'future' -- …` | The port lands after the transaction (bridge, Intents order, continuation). Remove the guard; rely on the op's floor. |
| `Guard targets outputs port "<p>" which does not exist. Available: [...]` | The port name in the guard config doesn't match any port on the op. Check the op's manifest. |
| `Guard targets port "<p>" but it has kind "<k>", expected "<k2>"` | The guard does not accept that kind of port, for example `core.numericInvariant` on a resource output. |
| `Guard requires config field "<key>" to select a target port` | The guard selects its port from config and the config is missing the key (usually `port`). |
| `Guard has no valid targets -- every matching output port is unguardable: …` | A guard that applies to all matching ports found only unguardable ones. |

## What guards do **not** do

* **Guards don't change routing.** If the market moves past a guard's bound before the transaction lands, the transaction reverts; Composer never reroutes to honour the bound. To make the aggregator honour a tolerance, configure it on the op, for example `lifi.swap`'s `slippage`.
* **Guards don't describe state before the transaction.** [Preconditions](https://docs.li.fi/composer/composer-api/guides/build-a-flow.md#preconditions-state-before-execution) do: they tell the simulation what state to assume, such as a wallet balance. Guards act on values computed during the transaction.

## See also

* [Guards catalog](https://docs.li.fi/composer/composer-api/guards.md): every guard registered in the manifest.
* [Execution Model](https://docs.li.fi/composer/composer-api/concepts/execution-model.md): the full compile sequence.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.