Liquidity Pools Development
A team brought a pool contract with classical x*y=k mechanics. Everything worked on testnet. On mainnet, three days after launch, a sandwich bot extracted $180k from the pool — entry price 12% above market, exit price 11% below. There was no slippage control at the contract level, only frontend checks. This is a typical story when developers copy AMM mechanics without understanding how MEV infrastructure works on Ethereum.
Where AMM Contract Breaks at Deployment
Impermanent Loss as an Architectural Decision, Not a Bug
IL is not an error, it's a mathematical consequence of continuous rebalancing. But protocols that don't explain the real math to LP providers lose liquidity: people see 40% APY, withdraw their position in a month, and don't understand why they're down in USD. The contract's job is to provide accurate position data: current value, accumulated fees, estimated IL.
In Uniswap v3, this is complicated by concentrated liquidity: a position is active only in the [tickLower, tickUpper] range. When price exits the range, the position stops earning fees and converts entirely to one token. An LP provider without monitoring can hold a "dead" position for weeks.
Price Manipulation via Flash Loan in One-Sided Oracles
An AMM pool that serves as its own price oracle is an attack vector. Flash loan of $50M USDC, one swap transaction — spot price in the pool shifts 10x. If your lending protocol reads price from this pool — it issues loans against manipulated collateral. This is how the Mango Markets attack worked ($114M in 2022).
Solution: TWAP (Time-Weighted Average Price) via Uniswap v3 oracle — IUniswapV3Pool.observe() with observation window at least 30 minutes. Or Chainlink as external oracle with circuit breaker: if spot and Chainlink diverge more than 5% — transaction reverts.
Reentrancy in Callback Mechanics
Uniswap v2/v3 uses callback pattern: uniswapV2Call and uniswapV3SwapCallback. If your pool implements similar mechanics and doesn't put nonReentrant on the function that calls the callback — classic vector. Attacker re-enters the pool via callback before reserves are updated.
In practice, we found pool contracts with a swap function that updated reserve0 and reserve1 after calling _callback. Reentrancy allowed getting tokens twice in one entry. OpenZeppelin ReentrancyGuard is needed on swap, addLiquidity, removeLiquidity — all three.
How We Build Liquidity Pools
Architectural Decisions
For most tasks, you don't need to write an AMM from scratch. Uniswap v2/v3, Balancer, Curve protocols — battle-tested code with billions in TVL. The task is to choose the right mechanics for your tokenomics:
| Mechanic | Suitable For | Example |
|---|---|---|
| x*y=k (Uniswap v2) | General case, two tokens | Any ERC-20 pair |
| Concentrated liquidity (Uniswap v3) | Stablecoins, correlated assets | USDC/USDT, ETH/stETH |
| StableSwap (Curve) | Stablecoins with minimal slippage | 3pool, FRAX |
| Weighted pools (Balancer) | 2-8 tokens with custom weights | 80/20 WETH/TOKEN |
| CPMM with custom fees | Protocol pools with fee-sharing | Custom DEX |
If the task is a custom pool for a specific protocol, use Uniswap v4 Hooks as a base (if the chain supports it) or Balancer v2 Vault architecture, where a common vault stores tokens and pools only contain logic.
LP Tokens and Position Tracking
ERC-20 standard for LP tokens in Uniswap v2 — the simplest case. Pool share = lpBalance / lpTotalSupply. Problem: with many LP providers, each transfer of LP token changes relative shares without other participants' knowledge.
For Uniswap v3-style positions, use NFT (ERC-721) — each position is unique by ticks and size. This complicates integration with other protocols (can't just stake in farm), but provides precise accounting of concentrated liquidity.
Fee accumulation: in Uniswap v3, fees accumulate via feeGrowthInside0LastX128 and feeGrowthInside1LastX128 — global counter minus counter at position creation. On collect(), recalculation happens. If implementing similar mechanics — overflow in uint128 over long periods without fee collection gives wrong numbers; use unchecked arithmetic with explicit comment.
MEV Protection at Contract Level
Several mechanics that reduce sandwich bot profit:
Commit-reveal for large swaps. User first publishes transaction hash, after N blocks — the actual transaction. Bots don't know parameters beforehand. High friction for user, only suitable for large swaps.
Dynamic fees. Fee increases with high volatility (detected via deviation from TWAP). This makes sandwiching less profitable. Implemented via Uniswap v4 Hook or custom fee tier.
Private mempool. Not solved at contract level — infrastructure question (Flashbots Protect, MEV Blocker). But important to warn the client before deployment.
Development Process
Specification (2-3 days). Define pool mechanics, LP tokenomics, fee structure, roles (owner, fee collector, pause guardian). Draw state diagram: all pool state transitions, including emergency pause.
Development (1-2 weeks). Solidity 0.8.x contracts with Foundry. Fuzz tests on invariants: reserve0 * reserve1 >= k never breaks, sum of all LP shares = totalSupply, fees don't exceed swap volume.
Fork testing. Tests on mainnet/Arbitrum fork — real prices, real tokens, real MEV bots in mempool. Use vm.createFork in Foundry.
Internal audit + Slither. At least a week before deployment. For TVL > $500k, we recommend external audit (Code4rena, Sherlock, Spearbit).
Deployment. forge script with verification, Gnosis Safe multisig on owner functions, timelock on fee parameter changes (minimum 48 hours).
Timeline Estimates
Basic pool with x*y=k mechanics and ERC-20 LP tokens — from 2 weeks with tests. Pool with concentrated liquidity Uniswap v3-style — from 6 weeks. Custom mechanics with multiple tokens, external oracle integration, and fee-sharing — 2-3 months. Audit timelines not included.







