Uniswap v2 and the Magic of Automated Market Makers
In the fast-moving world of decentralized finance (DeFi), one innovation stands out as a true game changer: the Automated Market Maker (AMM). Instead of relying on centralized exchanges or human market makers, AMMs use algorithms and smart contracts to set prices, hold liquidity, and execute trades — all without a middleman.
Uniswap v2 is one of the most popular examples, and in this post, we’ll break down exactly how it works: from the constant product formula to real trade simulations, and finally, the risks you should know.
What is an Automated Market Maker (AMM)?
At its core, an AMM is a smart contract that:
- Holds reserves of two (or more) tokens.
- Calculates prices using a mathematical formula.
- Executes trades automatically based on that formula.
Anyone can trade with the pool, and anyone can supply liquidity — making markets open, transparent, and always available.
In Uniswap v2, the formula that powers everything is:
Where:
- \(x\) = amount of token A in the pool
- \(y\) = amount of token B in the pool
- \(k\) = constant value that stays the same during trades
Smart Contracts and Liquidity Pools
An AMM runs entirely on a smart contract — a self-executing program on the blockchain that can’t be altered or stopped once deployed.
A liquidity pool is simply the collection of tokens locked inside that smart contract. Liquidity providers (LPs) deposit equal value of each token — for example, if ETH is worth 2,000 USDC, an LP could deposit:
- 1 ETH + 2,000 USDC
- 5 ETH + 10,000 USDC
From there, the pool handles trades between those tokens automatically. LPs earn a portion of the 0.3% trading fee for providing liquidity.
The Constant Product Formula
The constant product formula ensures that the product of the two reserves always stays the same:
Example:
- \(x = 10\) ETH
- \(y = 20{,}000\) USDC
- \(k = 10 \times 20{,}000 = 200{,}000\)
The price of ETH in USDC is:
\(P_{\text{ETH}} = \frac{y}{x} = \frac{20{,}000}{10} = 2{,}000 \ \text{USDC}\)Trade Simulation
Let’s simulate what happens when someone buys 1 ETH from the pool:
Initial reserves:
- \(x = 10\) ETH
- \(y = 20{,}000\) USDC
- \(k = 200{,}000\)
Step 1 — New ETH reserve
The trader removes 1 ETH from the pool, so the new ETH balance becomes:
This decrease in ETH will cause the price to increase automatically because of the constant product formula.
Step 2 — New USDC reserve (keeping \(k\) constant)
Since \(k\) must remain constant, we solve for the new USDC reserve \(y’\):
Substitute known values:
Therefore:
Step 3 — Amount paid by trader
Before the trade, the pool had 20,000 USDC. Afterward, it needs 22,222.22 USDC.
The trader must pay the difference:
So the trader spends ≈ 2,222.22 USDC to buy 1 ETH.
Step 4 — New price
The updated price of ETH in USDC is given by:
The price jumped from 2,000 USDC to ≈2,469 USDC after just one purchase!
This illustrates the price impact caused by the constant product formula.
Step 5 — Accounting for Fees
Uniswap v2 charges a 0.3% fee on every trade.
The effective input ETH is reduced by this factor:
These fees remain inside the pool, rewarding liquidity providers.
Risks of Using AMMs
Automated Market Makers (AMMs) like Uniswap v2 are powerful, but they come with significant risks that traders and liquidity providers should understand before participating.
Here are the main ones:
1. Impermanent Loss
Impermanent loss happens when the price of tokens in the pool diverges compared to simply holding them in your wallet.
Let’s assume you provided 1 ETH and 2,000 USDC into a pool where the initial price of ETH is:
If ETH’s market price doubles to 4,000 USDC, the AMM automatically rebalances reserves to maintain:
This rebalancing reduces your ETH holdings compared to holding ETH directly.
Impermanent loss becomes permanent if you withdraw liquidity while prices are diverged.
2. Slippage
Slippage occurs when your trade size significantly impacts the price due to limited liquidity.
In small pools, buying or selling even a few ETH can cause the price to move dramatically.
The price is calculated as:
If \(x\) or \(y\) is small, each trade creates a bigger price impact, leading to higher slippage.
That’s why larger pools are more stable.
3. Front-Running
AMMs operate on public blockchains, meaning your trade is visible in the mempool before it’s confirmed.
This allows MEV bots (Maximal Extractable Value bots) to front-run your trade by:
- Detecting your transaction,
- Placing a buy order before yours,
- Selling right after,
- Forcing you to buy at a worse price.
This can increase costs significantly, especially during high network congestion.
4. Flash Loan Exploits
Flash loans allow attackers to borrow huge amounts of liquidity without collateral, manipulate the price inside an AMM, and repay instantly in one transaction.
For example, an attacker could:
- Borrow 1M USDC via a flash loan.
- Swap it for ETH in Uniswap v2, temporarily pushing ETH’s price up.
- Exploit other DeFi protocols that depend on Uniswap’s price.
- Swap ETH back, repay the flash loan, and pocket the difference.
This is why Uniswap v3 introduced better price oracles and concentrated liquidity to reduce attack vectors.
5. Smart Contract Bugs
AMMs like Uniswap rely on smart contracts. If there’s a bug or vulnerability in the code, funds in the pool can be stolen instantly.
Even audited protocols have been hacked before, so it’s important to:
Diversify your liquidity instead of locking everything in one pool
Use trusted protocols
Avoid depositing into forks or unaudited contracts
6. Network & Regulatory Risks
If governments impose regulations on DeFi, your ability to access liquidity could be affected.
Network congestion (especially on Ethereum) can make trades extremely expensive during high gas spikes.
Sudden forks or upgrades may cause temporary mismatches in pricing.
Final Thoughts
Uniswap v2’s AMM model is elegant: simple math and transparent code powering billions in trades without centralized control. But like any powerful tool, it comes with risks that demand respect.
If you understand the constant product formula, how trades impact price, and the ways bad actors might exploit the system, you’ll be far better prepared to use AMMs wisely — whether as a trader or a liquidity provider.