# `UniswapImplementation::beforeSwap()` might revert when swapping native tokens to collection tokens

Flayer's Uniswap hook compared the wrong variable (tokenOut vs ethIn) when deciding whether to convert accumulated collection-token fees, causing native-to-collection swaps to revert.

- Payout: Audit contest
- Severity: medium
- Program: Flayer
- Found by: 0xAlix2
- Disclosed: 2024-08-06
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-08-flayer-judging/issues/517
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-flayer%3AM-19
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:dos

**Target:** UniswapImplementation.beforeSwap hook in Flayer AMM
**Root cause:** When amountSpecified < 0 (exact input native), the code checks `tokenOut <= uint(-params.amountSpecified)` instead of `ethIn <= uint(-params.amountSpecified)`, so it decides to apply a beforeSwapDelta when the internal fee-conversion swap actually consumed more ETH than the user is paying, causing the outer swap to revert.
**Steps:**
1. Initialize a collection with native + ctoken liquidity at SQRT_PRICE_1_1.
2. Deposit an NFT and call `uniswapImplementation.depositFees(collection, 0, 1e18)` to accumulate ctoken fees in the hook.
3. Call `poolSwap.swap` with `zeroForOne: false`, `amountSpecified: -1e18` (exact native input) to buy ctokens.
4. Observe revert because `tokenOut <= 1e18` while `ethIn > 1e18`, so the hook applies a delta larger than the user's input.
**How found:** Code review of the beforeSwap fee-conversion branch: noticed the comparison used tokenOut against the user's specified native input rather than ethIn, which is the actual native amount consumed by the internal swap.
**Insight:** In Uniswap v4 hook delta logic, when amountSpecified is negative (exact input), the guard for applying a beforeSwapDelta must compare the consumed input (ethIn) against the user's specified input, not the output token amount; mismatched variables silently produce deltas larger than the user's payment and revert the swap.

