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

# Swap step

> Fee, boundary, output, and remainder behavior in the Q128.128 swap engine.

A swap step starts with square-root price, liquidity, remaining raw input, fee tier, direction, and a target boundary.

## Fee-adjusted input

```text theme={"languages":{"custom":["/languages/leo.tmLanguage.json"]}}
input_less_fee = floor(remaining * (1_000_000 - fee_pips) / 1_000_000)
```

If this value is zero, the bounded partial path returns the input. It does not charge the whole input as a zero-output fee.

## Boundary selection

The engine chooses the nearer value in the swap direction:

* next initialized-tick price; or
* user price limit.

The fixed crossing loop can process five initialized ticks for a single-hop swap and three per multi-hop leg. A bounded partial fill then moves only up to the selected boundary. It never jumps past an uncrossed initialized tick.

## Amount relationships

For token0 input, price moves down. Input follows the amount0 delta and output follows the amount1 delta. For token1 input, price moves up and the roles reverse. All price formulas use Q128.128 and full-width division.

## Fees

The contract calculates gross input consumed, swap fee, protocol share, LP fee growth, output, and remainder with one consistent capped amount. Protocol fee is `fee_charged * fee_protocol / 16`. The LP share increases global Q128.128 fee growth for the input token.

## Stored tick

An upward fill that would stop exactly on an initialized tick is held one Q128.128 unit below that boundary when the tick cannot be crossed. This preserves the half-open liquidity convention. `resolve_stored_tick` then makes stored tick agree with stored price and crossing state.

Quotes must reproduce these branch and rounding rules. A continuous constant-product formula is not sufficient.
