AMM-Based DEX Development

We design and develop full-cycle blockchain solutions: from smart contract architecture to launching DeFi protocols, NFT marketplaces and crypto exchanges. Security audits, tokenomics, integration with existing infrastructure.
Showing 1 of 1 servicesAll 1306 services
AMM-Based DEX Development
Complex
from 2 weeks to 3 months
FAQ
Blockchain Development Services
Blockchain Development Stages
Latest works
  • image_website-b2b-advance_0.png
    B2B ADVANCE company website development
    1221
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1163
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    857
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1063
  • image_logo-advance_0.png
    B2B Advance company logo design
    561
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    829

DEX Development Based on AMM

AMM (Automated Market Maker) is a protocol that replaces a traditional order book with a mathematical formula. Instead of waiting for a counterparty with an opposite order, a trader trades against a liquidity pool. Price is determined by an algorithm in real time. This changed DeFi — and understanding how it works internally is critical before building your own DEX.

Pricing Mathematics

Constant Product Formula

Uniswap V2 popularized the formula x * y = k, where x and y are reserves of two tokens in the pool, k is an invariant. At any operation, the product of reserves must remain constant (with a fee adjustment).

Current price of token X in units of Y: price = y / x

On a swap: trader deposits Δx of token X, receives Δy of token Y:

(x + Δx) * (y - Δy) = k
Δy = y - k / (x + Δx) = y * Δx / (x + Δx)

With 0.3% fee (fee = 0.003):

Δy = y * Δx * (1 - fee) / (x + Δx * (1 - fee))

Price impact — how much price shifts during a trade — is directly proportional to trade size relative to pool reserves. This is fundamental: large orders get poor pricing in small pools.

Concentrated Liquidity (Uniswap V3)

Uniswap V3 added concentrated liquidity: LP providers specify a price range [Pa, Pb] where their liquidity works. Outside the range, liquidity is "inactive" and earns no fees.

Formulas become more complex. Real reserves X and Y in the pool:

x = L * (1/√P - 1/√Pb)
y = L * (√P - √Pa)

Where L is the amount of liquidity (virtual constant), P is the current price.

This fundamentally changes economics: LP capital efficiency increases 100-4000x compared to V2 for stablecoin pairs, but LP takes on a more complex impermanent loss profile.

Curve StableSwap

For assets with close prices (stablecoins, stETH/ETH), constant product gives poor slippage. Curve uses a hybrid formula combining constant product and constant sum.

Curve balances between them, providing minimal slippage in the range near parity and converging to constant product at the edges.

Smart Contract Architecture

Core Contracts

Typical AMM DEX architecture consists of several levels:

Factory contract — registry and pool creator:

CREATE2 is important: pool address is deterministic and predictable from token addresses, allowing Router to compute pool address without contacting Factory.

Pair (Pool) contract — AMM core. Stores reserves, issues LP tokens, executes swaps.

The "optimistic transfer + invariant check" pattern is also the foundation of flash loans: data.length > 0 allows calling a callback on the recipient before checking — this is flash swap.

Router contract — convenient interface for end users. Handles multi-hop routes (A→B→C), slippage protection, deadline.

LP Tokens and Liquidity Management

When adding liquidity, LP receives ERC-20 tokens representing a pool share:

LP_minted = totalSupply * min(amount0 / reserve0, amount1 / reserve1)

On first deposit: LP_minted = sqrt(amount0 * amount1) - MINIMUM_LIQUIDITY

MINIMUM_LIQUIDITY = 1000 — these LP tokens are burned forever on pool creation, preventing attacks on empty pools.

Price Oracles

TWAP (Time-Weighted Average Price)

Uniswap V2 introduced an on-chain price oracle through cumulative price accumulation.

TWAP over a period = cumulative difference / period time. This is manipulation-resistant: to distort TWAP over 1 hour, you need to hold wrong price for the entire hour, which is economically unfeasible.

Uniswap V3 improved the system: stores a ring buffer of 65535 observations, tick-based accumulator instead of price accumulator — more accurate with concentrated liquidity.

MEV and Attack Protection

Sandwich Attacks

Classic attack: MEV bot sees pending swap, inserts its buy before it (front-run) and sell after (back-run), extracting profit at victim's expense.

Defensive mechanisms:

Slippage tolerance — user sets maximum acceptable price impact (usually 0.5-1%). Transaction reverts if exceeded.

Private mempool / Flashbots Protect — sending transactions to private mempool inaccessible to MEV bots.

Commit-reveal schemes — user first publishes intent hash, then reveals parameters. Complicates but doesn't eliminate MEV.

CoW Protocol / batch auctions — principled solution: all orders in a period execute at uniform clearing price, front-running within batch is impossible.

Reentrancy Protection

Pool uses nonReentrant mutex (lock pattern in Uniswap).

Optimization: using one storage slot instead of OpenZeppelin ReentrancyGuard — saves ~2300 gas per call.

Token Economics and Protocol Governance

Protocol Fee

Uniswap V2 introduced a "protocol fee switch": in addition to 0.3% LP fee, a 0.05% protocol fee can be enabled, going to treasury/governance. Off by default — enabled by governance vote.

For your own AMM: recommend initially embedding two-level fee structure (LP fee + protocol fee) even if protocol fee = 0 at launch. Adding retroactively is harder due to contract upgrade necessity.

Governance Token

Pattern: governance token (GVN) distributed through liquidity mining — LPs stake their LP tokens in Staking contract and receive GVN proportional to share and time.

accRewardPerShare is updated on each interaction — this is the pattern from MasterChef (SushiSwap), de-facto standard for liquidity mining.

Frontend and UX

Critical frontend components for AMM DEX:

  • Swap interface with real-time price impact and minimum received
  • Liquidity management with range visualization (for V3-style)
  • Charts — integration with The Graph for historical pool data
  • Wallet integration — wagmi + viem, WalletConnect v2 support
  • Transaction simulation — tenderly simulate before sending (prevents unexpected reverts)

The Graph indexes pool events (Swap, Mint, Burn) and provides GraphQL API for analytics. Alternative — own indexer on Ponder or Envio for full data control.

Final stack for production AMM DEX: Solidity + Foundry for contracts, Hardhat for deployment and tests, React + wagmi for frontend, The Graph for indexing, Tenderly for monitoring. Audit is mandatory — vulnerability in AMM pool means TVL loss.