Swap Lifecycle Walkthrough
Step-by-step, layman-friendly walkthrough of a hook-enabled swap from request to final settlement.
Big Picture
This system keeps the normal Uniswap v4 pool path, then adds a hook-managed safety and settlement layer around it.
Key Idea
The pool still has LP liquidity. The hook does not replace LPs. It adds risk checks and optional collateral-backed settlement during the same swap transaction.
Sequence Diagram
Decision Tree
Step-by-Step (16 Steps)
1) User sends a swap request
A trader asks to swap token A for token B using the router.
2) Router forwards the request to the target pool
The pool key includes fee, tick spacing, and the configured hook address.
3) Pool calls beforeSwap on the hook
This is the hook's first chance to allow, deny, or shape the trade.
4) Hook checks for signed quote metadata
If no signed quote is present, the hook returns ZERO_DELTA immediately. Empty and other non-signed hookData never reach vault sizing or settlement.
5) Hook checks mode and size guardrails
It checks safe mode and the full requested amount against the per-transaction limit. If a limit is broken, it reverts.
6) Hook checks oracle conditions for signed swaps
It checks oracle mode/price only after signed quote metadata is present. Signed-quote payloads are validated in CollateralManager against the same policy envelope.
If a signed quote is supplied and fails validation, the swap reverts and does not fall back to unsigned settlement in the same transaction.
7) Hook asks how much the vault rail may handle
It calls vaultSettlementSpecifiedAbs(...) on the configured liquidity source (here, CollateralManager).
The manager delegates registered-issuer sizing to the settlement controller and returns either the exact safe vault leg or zero.
In ExternalSettlementVolume mode, the hook checks that leg against the remaining per-block hook allowance. A zero vault leg uses no allowance.
8) Hook previews the nonzero vault leg
If the vault leg is nonzero, the hook calls previewSwapExternal(...) to compute the unspecified delta returned to PoolManager.
If the vault leg is zero, no external preview is needed and the pool handles the whole swap.
Settlement resolves to one of two paths:
- Pure AMM: no signed quote present, so the hook does not call
CollateralManagerfor vault settlement. - Signed quote: signed payload present and valid, with
SwapParamsaction/amount plus quote validation.
In current SDK usage, signed payloads are provided as a typed signedSwapQuote in the call input; the SDK serializes it into hookData.
Decision precedence is strict:
- No signed payload supplied => pure AMM pass-through.
- Signed payload supplied and valid => signed quote path.
- Signed payload supplied and invalid => revert (no same-transaction fallback to unsigned settlement).
For signed settlement:
amount = abs(params.amountSpecified)amountSpecified > 0=> redeem pathamountSpecified < 0=> mint pathhookDatadoes not set amount or action by itselfint256.minis treated as absolute value2^255and then narrowed toint128for hook deltas (which reverts if out of range).
9) Hook stores preview in transient swap context
The preview, or zero delta for a zero-vault signed quote, is cached in temporary transaction storage and returned to the pool as beforeSwap delta.
10) Pool executes normal AMM swap math
Uniswap uses LP-provided liquidity for any pool remainder. A fully external issuer settlement has no pool remainder.
11) Pool calls afterSwap on the hook
The hook now sees post-swap deltas from the pool.
12) Hook enforces settlement-size bounds
It checks each token's combined pool-plus-hook movement against that token's PoolAssetRiskConfig.maxSettlementAbsDelta when configured.
It also verifies that executed pool plus hook volume does not exceed the request. The block cap records the executed hook leg in ExternalSettlementVolume mode or both tokens' executed pool-plus-hook movement in AllVolume mode before external settlement. AllVolume mode also counts ordinary pool swaps.
13) Hook triggers external settlement or quote consumption
For signed swaps with nonzero vault sizing, it calls settleSwapExternal(...) on the liquidity source with exact input/output token amounts.
Pure AMM paths skip manager calls. Signed zero-vault paths call consumeSignedQuoteForSwap(...) so a valid signed quote cannot be replayed after a pool-only signed success.
14) Manager validates caller and executes signed swap-param-based settlement
CollateralManager only accepts calls from the authorized hook. External settlement uses the same SwapParams rule as preview: amount and sign determine mint/redeem.
It also runs the signature/trader/pool/amount policy checks before mutating state.
Any signed-quote validation failure reverts the swap and does not downgrade to unsigned settlement in that transaction.
15) For resolved action, manager executes mint or redeem
Mint/redeem paths apply caps, debt ceilings, oracle checks, and solvency checks before updating balances. Registered-issuer broker payloads are exact-or-zero: the controller executes the broker leg only when the requested issuer leg exactly matches policy, capacity, quote, and token expectations.
16) Hook finalizes and returns
Hook emits settlement telemetry, clears transient swap state, and returns the cached unspecified delta to the pool.
Which Liquidity Is Used When
LP liquidity is still the in-pool AMM execution path for the swap.
Signed quotes add a permissioned settlement envelope; they do not replace LP math.
Model behavior:
- No signed quote: pure AMM pass-through with no hook-managed vault settlement.
- Signed quote: same AMM trade path, plus quote-based validation in
CollateralManager:- exact trader binding
- expiry check
- derived replay-key check from the signed envelope
- signer validation
- max amount cap check
- policy alignment against oracle price envelope
Observed effects:
- The pool always executes swap math against LP liquidity.
- Only signed quote swaps can settle via mint/redeem in
CollateralManagerafter the settlement hook runs. - Signed settlement still starts from
abs(amountSpecified), then registered-issuer and risk checks may reduce the vault leg to zero before AMM execution. - If validation fails, swap reverts and does not settle.
Outcome rules:
- No signed quote provided => pure AMM pass-through.
- Signed quote provided and valid => continue with signed quote settlement.
- Signed quote provided but invalid => revert; no settlement and no same-transaction fallback.
Model Summary
| Model | Input Surface | Settlement Amount | Settlement Direction | Extra Requirements |
|---|---|---|---|---|
| Pure AMM | non-signed hookData | none | none | no vault sizing or settlement |
| Signed Quote | SwapParams + SignedSwapQuote payload | exact safe vault leg or zero, bounded by abs(amountSpecified) | sign(amountSpecified) | signature, expiry, trader, pool, signer, derived replay key, oracle deviation policy |
Notes for New Developers
- LPs still provide in-pool liquidity in the normal v4 way.
- The hook adds policy-gated external settlement around that normal path.
- If the hook address is zero in scripts, the swap behaves like a no-hook pool path.