Skip to content

fix: correct fee calculation for exactOut swaps in afterSwap example#1109

Open
Kropiunig wants to merge 1 commit intoUniswap:mainfrom
Kropiunig:fix/afterswap-fee-calculation
Open

fix: correct fee calculation for exactOut swaps in afterSwap example#1109
Kropiunig wants to merge 1 commit intoUniswap:mainfrom
Kropiunig:fix/afterswap-fee-calculation

Conversation

@Kropiunig
Copy link

Summary

This PR fixes a bug in the afterSwap hook fee example where the fee amount was incorrectly calculated for exact output swaps.

The issue: The documentation correctly states that the returned int128 is for the unspecified currency (output for exactIn, input for exactOut), but the code example always calculated the fee from outputAmount, which is wrong for exactOut swaps.

The fix:

  • Determine the unspecified token based on swap type (exactIn vs exactOut)
  • Calculate the fee from the correct amount (output for exactIn, input for exactOut)
  • Handle the sign difference (output is positive, input is negative)

Changes

  • Updated the code snippet in Step 2
  • Updated the complete contract example

Fixes #1101

The afterSwap hook fee example incorrectly calculated the fee amount
using outputAmount for both exactIn and exactOut swaps. However, as
noted in the documentation tip, the returned int128 is for the
unspecified currency:
- exactIn: unspecified = output token
- exactOut: unspecified = input token

The previous code always used outputAmount to compute feeAmount,
which is incorrect for exactOut swaps where the fee should be
calculated from the input amount.

This fix:
- Determines the unspecified token based on swap type
- Uses the correct amount (output for exactIn, input for exactOut)
- Simplifies feeCurrency assignment since it always matches unspecifiedIsToken0

Fixes Uniswap#1101
@vercel
Copy link

vercel bot commented Feb 5, 2026

@Kropiunig is attempting to deploy a commit to the Uniswap Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug in custom accounting docs

1 participant