Free lesson · DeFi models
Constant-product swaps with an input fee
Open interactive lessonPractice calculationsExplore labs
Start with the idea
A pool exchanges against its reserves. Buying a large fraction of the available asset moves the average price against the buyer.
Symbols, units & horizon
- x: base-token reserve
- y: quote-token reserve
- Δx: base tokens sent to pool
- f: input fee fraction
- Δy: quote tokens received
- one swap with retained input fees and positive reserves
When and why to use this
Compare AMM execution prices and fees with an order-book route at the same trade size.
A pool exchanges against its reserves. Buying a large fraction of the available asset moves the average price against the buyer.
Let x be base reserves and y quote reserves. In a no-fee constant-product pool, the product is unchanged by a swap. With an input fee, only the fee-adjusted input is used in the output formula, while the full input enters reserves under the assumed retained-fee convention.
Use this as a V2-style teaching model, not a universal protocol quote. Token transfer fees, hooks, concentrated ranges and integer rounding require additional rules. An external price comparison is needed to judge whether a swap is economically favorable.
Constant-product swaps with an input fee
- Apply the pricing input Δx_eff=(1−f)Δx.
- Set virtual post-trade quote reserve to xy/(x+Δx_eff).
- Subtract that reserve from y and simplify to the displayed output.
x=100 base, y=10,000 quote, input 10 base, f=.003. Effective input=9.97; output=10000×9.97/109.97≈906.610894 quote.
Apply it in a strategy
- Compare AMM execution prices and fees with an order-book route at the same trade size.
- Record the input timestamp, executable quantity, currency and horizon. Reconcile the result with a cash-flow or state table.
- Stress this failure condition: Reserve snapshots can change before execution; minimum-output protection limits price but cannot ensure inclusion.
Research deliverable
Build and explain a constant-product swaps with an input fee worksheet. Compare AMM execution prices and fees with an order-book route at the same trade size.
Evidence boundary: Synthetic arithmetic and scenarios illustrate mechanics. They are not historical returns, a paper replication, or evidence of an executable edge. Research sources and their access limitations are recorded at the end of this module.
Research sources, review dates and limitations
Python implementation
Self-contained teaching example. Python 3.10+; dependencies and input conventions are shown in the code and notation. Run in your own Python environment.
# Python 3.10+; standard library unless NumPy is imported below.
# Inputs and outputs use the units defined in this lesson. Synthetic teaching example.
def cp_swap(base_reserve,quote_reserve,base_in,fee):
if min(base_reserve,quote_reserve)<=0 or base_in<0 or not 0<=fee<1: raise ValueError("Invalid pool inputs")
effective=base_in*(1-fee)
output=quote_reserve*effective/(base_reserve+effective)
return output,base_reserve+base_in,quote_reserve-output
print(cp_swap(100,10000,10,.003))Continue learning
DeFi: AMMs, Liquidity Provision and Lending — all lessons- Constant-product swaps with an input fee
- LP inventory after price changes
- LP value versus holding the original tokens
- Concentrated liquidity and range boundaries
- LVR and the price of stale inventory
- Lending utilization and rate response
- Collateral health factor and correlated shocks
- Liquidation incentives after execution costs
Quantitative finance and development glossary · Python resources and libraries · Research sources and limitations