Swap output calculation for a constant-product pool starts by removing the fee from the input: only the remainder is priced along xy=k, so output = ydx(1-f) / (x + dx(1-f)). The pool receives the full input, and the retained fee helps raise its reserve product, k.
Price the input after the fee
A constant-product pool uses its token reserves to set the trade: x is the reserve of the token going in, y is the reserve of the token coming out, and k = xy. The spot price is y/x, so a tiny trade with no fee or price impact would return dxy/x.
- Input: dx
- Fee rate: f
- Priced input: dx' = dx*(1-f)
- Output: y*dx'/(x+dx')
The fee-adjusted input, dx', moves along the curve. The full dx enters the pool’s reserves, while the output is calculated using only dx'. Constant-product pools such as Uniswap V2 use this approach. SunSwap V2 is a concrete example: its constant-product reserve formula makes this calculation applicable to a TRON token swap.
At sunswap.cc, you can swap TRC-20 tokens through liquidity pools on TRON.
The distinction to keep clear is between the input used to price the trade and the input added to the pool. Treating the full input as priced gives too much output; subtracting the fee from the reserve update would also miss the fee that stays in the pool.
The example separates fee from price impact
Take a pool with 1,000,000 TRX and 100,000 USDT. Its spot price is 0.1 USDT per TRX. For a 10,000 TRX trade at a 0.3% fee, the pool prices 9,970 TRX, since 10,000*(1-0.003) = 9,970.
Putting those values into the formula gives 100,000*9,970/(1,000,000+9,970), or about 987.16 USDT. At the starting spot price, 10,000 TRX would correspond to 1,000 USDT. The actual output is about 12.84 USDT below that reference, a shortfall of about 1.28%.
Compare the same pool and trade with the fee set to zero. The priced input would be 10,000 TRX and the curve would return about 990.10 USDT. With the 0.3% fee, output falls further to about 987.16 USDT. The shared reserve sizes and trade amount make the fee the decisive change: the lower priced input accounts for about 3 USDT of the shortfall, while curve movement accounts for about 9.84 USDT. Relative to the 1,000 USDT spot reference, that is roughly 0.3% fee and 0.99% price impact.
Price impact comes from moving along the curve: as more TRX is added, each further unit buys less USDT. The fee is a separate charge on the input. Mixing them up makes the pool’s curve look more costly than it is, or makes the fee disappear from the accounting.
After the trade, the pool holds 1,010,000 TRX and about 99,012.84 USDT. Their product is above the original k of 100,000,000,000. That increase comes from adding the full 10,000 TRX while paying output as though only 9,970 TRX entered the pricing calculation. The retained fee contributes to k growth over trades, which is how liquidity providers earn; the trader’s cost relative to spot combines fee and price impact.
SunSwap V3 applies the same idea within the active price range. Its concentrated liquidity means the depth available in that range sets how quickly price moves, so the V2 reserve calculation above is not a complete output calculation for V3.
Related questions
How is price impact calculated on a DEX?
Price impact is the difference between the output at the starting spot price and the output after the trade moves along the pool curve, measured separately from the fee.
Where does the swap fee go after a trade?
The fee remains in the pool’s reserves, increasing k and contributing to liquidity providers’ earnings.
How does V3 concentrated liquidity change the x*y=k math?
V3 concentrates liquidity within active price ranges, so available depth in the current range determines impact; the simple whole-pool reserve formula is not enough to calculate its output.
To estimate a constant-product swap by hand, use the pool’s reserves and fee rate, price only dx*(1-f), and then compare the result with the spot-price output to see the fee and curve impact separately.