Your team deployed a constant product pool with 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 the real price, exit price 11% below. No slippage control at the contract level, only frontend validation. This is a typical story: developers copy AMM mechanics without understanding how Ethereum's MEV infrastructure operates. Contact us to avoid such losses — we specialize in pool protection.
Problems with AMM Contracts at Deployment
Impermanent Loss as an Architectural Decision, Not a Bug
IL is not a bug but a mathematical consequence of rebalancing. Protocols that fail to explain the real math to LP providers lose liquidity: people see 40% APY, withdraw after a month, and don't understand why they are in the red in USD terms. The contract's job is to provide accurate data on the position: current value, accumulated fees, estimated IL.
In Uniswap v3, this is complicated by concentrated liquidity: a position is active only within the range [tickLower, tickUpper]. When the price exits the range, the position stops earning fees and fully converts to one token. An LP provider without monitoring can hold a dead position for weeks.
Price Manipulation via Flash Loans in Single-Source Oracles
An AMM pool that itself serves as a price oracle is an attack vector. A flash loan, a single swap transaction — spot price in the pool shifts by 10x. If your lending protocol reads the price from this pool, it issues loans against manipulated collateral. This happened to Mango Markets (loss of $114M). Solution: TWAP via Uniswap v3 oracle — IUniswapV3Pool.observe() with a minimum observation window of 30 minutes. Or Chainlink as an external oracle with a circuit breaker: if spot and Chainlink diverge by more than 5%, the transaction reverts.
Reentrancy in Callback Mechanics
Uniswap v2/v3 uses a callback pattern: uniswapV2Call and uniswapV3SwapCallback. If your pool implements similar mechanics and does not put nonReentrant on the function that invokes the callback — classic vector. In practice, we encountered a pool contract with a swap function that updated reserve0 and reserve1 after calling _callback. Reentrancy allowed getting tokens twice on one input. ReentrancyGuard is needed on swap, addLiquidity, removeLiquidity — all three.
Why a TWAP Oracle Is Mandatory for a Pool
Without TWAP, the pool is vulnerable to flash loan attacks. By using a short observation window, we smooth out manipulations. In high-TVL projects, we add a fallback to Chainlink to increase reliability.
How to Reduce Impermanent Loss Risk for LPs
We embed real-time impermanent loss calculation in the contract and provide data via view functions. In concentrated pools, we add automatic position rebalancing through keeper bots to avoid prolonged stays outside the range. This reduces losses for LP providers.
How We Build Liquidity Pools
Architectural Decisions
For most tasks, there's no need to write an AMM from scratch. Uniswap v2/v3, Balancer, Curve protocols are battle-tested code with billions in TVL. The task is to choose the right mechanics for the tokenomics:
| Mechanics | 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, we base it on Uniswap v4 Hooks (if the chain supports it) or Balancer v2 Vault architecture, where a shared vault holds tokens and pools only contain logic.
LP Tokens and Position Accounting
The ERC-20 standard for LP tokens in Uniswap v2 is the simplest case. Share in pool = lpBalance / lpTotalSupply. Problem: with many LP providers, each transfer of LP tokens changes relative shares without notifying other participants. For Uniswap v3-style positions, we use NFT (ERC-721) — each position is unique by ticks and size. This complicates integration but provides accurate accounting. Fees accumulate via feeGrowthInside0LastX128 — requires unchecked arithmetic with explicit comments.
MEV Protection at the Contract Level
- Use commit-reveal for large swaps: user publishes a hash, then after N blocks the actual transaction. Bots don't know parameters in advance.
- Use dynamic fees: fee increases during high volatility (detected via deviation from TWAP). Implemented via Uniswap v4 Hook or custom fee tier.
- Recommend private mempool (Flashbots Protect, MEV Blocker) — infrastructural solution, we warn the client before deployment.
What's Included in Liquidity Pool Development
| Stage | Duration | Result |
|---|---|---|
| Specification | 2-3 days | Document with pool mechanics, tokenomics, fee structure, roles |
| Development | 1-6 weeks | Contracts in Solidity 0.8.x with Foundry, fuzz tests |
| Fork testing | 3-5 days | Tests on mainnet fork with real prices |
| Audit | 1-2 weeks | Internal Slither + optional external |
| Deployment | 1-2 days | Forge script, verification, multisig, timelock |
Additional: frontend integration (ethers.js, wagmi), position monitoring, client team training.
Our Experience Metrics
Total TVL of developed pools exceeds $100M. 5+ years in DeFi, 30+ smart contract projects. We work with certified auditors from Code4rena and Sherlock. In one project, contract optimization reduced gas costs by 40%, saving the client $50k per year.
Development Process
Specification (2-3 days). Define pool mechanics, LP token tokenomics, fee structure, roles (owner, fee collector, pause guardian). Draw state diagram: all pool state transitions, including emergency pause.
Development (1-2 weeks). Contracts in Solidity 0.8.x with Foundry. Fuzz tests on invariants: reserve0 * reserve1 >= k never violated, sum of all LP shares equals totalSupply, fees do not 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 x*y=k pool with ERC-20 LP tokens — from 2 weeks with tests. Concentrated liquidity pool (Uniswap v3 style) — from 6 weeks. Custom mechanics with multiple tokens, external oracle integration, and fee-sharing — 2-3 months. Audit timeline not included.
Get a consultation on pool architecture selection — contact us for a free assessment of your project.







