惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
S
Securelist
V
Vulnerabilities – Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
Simon Willison's Weblog
Simon Willison's Weblog
Hacker News - Newest:
Hacker News - Newest: "LLM"
P
Palo Alto Networks Blog
T
Troy Hunt's Blog
SecWiki News
SecWiki News
Security Archives - TechRepublic
Security Archives - TechRepublic
T
The Blog of Author Tim Ferriss
Project Zero
Project Zero
Microsoft Security Blog
Microsoft Security Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
F
Full Disclosure
阮一峰的网络日志
阮一峰的网络日志
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Attack and Defense Labs
Attack and Defense Labs
Know Your Adversary
Know Your Adversary
WordPress大学
WordPress大学
PCI Perspectives
PCI Perspectives
N
News | PayPal Newsroom
The Last Watchdog
The Last Watchdog
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
CERT Recently Published Vulnerability Notes
H
Help Net Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
云风的 BLOG
云风的 BLOG
月光博客
月光博客
T
The Exploit Database - CXSecurity.com
I
InfoQ
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
腾讯CDC
小众软件
小众软件
V2EX - 技术
V2EX - 技术
罗磊的独立博客
Cloudbric
Cloudbric
Recorded Future
Recorded Future
IT之家
IT之家
Google DeepMind News
Google DeepMind News
C
CXSECURITY Database RSS Feed - CXSecurity.com

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
AMM Mathematics: Constant Product and Beyond — How Uniswap's x·y=k Formula Powers DeFi
Khusniddin Q · 2026-05-14 · via DEV Community

Introduction

Decentralized Finance (DeFi) has transformed how we think about financial primitives. At the heart of this transformation lies a deceptively simple equation: x · y = k. This invariant, introduced by Uniswap V2, underpins billions of dollars in daily trading volume and gave birth to the Automated Market Maker (AMM) paradigm. Yet beneath its simplicity lies a rich mathematical structure — one that governs pricing, liquidity, slippage, and the often-misunderstood phenomenon of impermanent loss.

In this article, we derive the constant-product formula from first principles, analyze its mathematical properties, explore how impermanent loss emerges naturally from the model, and then examine how Uniswap V3's concentrated liquidity model extends and generalizes the original invariant.


1. What Is an Automated Market Maker?

Traditional exchanges use an order book: buyers post bids and sellers post asks, and trades execute when the two sides match. This model requires active market participants and suffers from low liquidity in thin markets.

An AMM replaces the order book with a mathematical pricing function. A smart contract holds reserves of two tokens — say, token X (e.g., ETH) and token Y (e.g., USDC) — and uses a formula to deterministically quote prices based solely on the current reserve balances. No counterparty is needed; the contract itself is always willing to trade at the formula-determined price.

The key insight is that the AMM does not need to know the "true" price. Arbitrageurs will continuously trade against it until the AMM price aligns with the broader market, making the system self-correcting.


2. Deriving the Constant-Product Formula

2.1 The Invariant

Let x denote the reserve of token X and y the reserve of token Y. Uniswap V2 enforces the following invariant after every trade (ignoring fees for now):

x * y = k

Enter fullscreen mode Exit fullscreen mode

where k is a constant. This means the product of the two reserves must remain unchanged after any swap.

2.2 Price Derivation

The marginal price of token X in terms of token Y is the instantaneous rate at which the curve trades one for the other. Taking the total differential of the invariant:

d(x * y) = 0
y dx + x dy = 0
dy/dx = -y/x

Enter fullscreen mode Exit fullscreen mode

The negative sign indicates that as we buy X (x decreases), y must increase. The spot price of X in terms of Y at any point on the curve is:

P = y / x

Enter fullscreen mode Exit fullscreen mode

This is the marginal price. Notice it is not constant — it changes with every trade. This is precisely what creates slippage.

2.3 Executing a Swap

Suppose a trader wants to buy Δx tokens of X by depositing Δy tokens of Y. The new reserves must satisfy:

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

Enter fullscreen mode Exit fullscreen mode

Expanding and solving for Δx:

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

Enter fullscreen mode Exit fullscreen mode

Alternatively, solving for the input needed to obtain a given output:

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

Enter fullscreen mode Exit fullscreen mode

This closed-form solution means swaps can be computed in O(1) with no order book traversal, which is why AMMs are extremely gas-efficient on-chain.

2.4 Effective Price vs. Spot Price

The effective price paid per unit of X is:

P_eff = Δy / Δx = y / (x - Δx)

Enter fullscreen mode Exit fullscreen mode

Compared to the spot price P_spot = y/x, we see:

P_eff = P_spot * (x / (x - Δx)) > P_spot

Enter fullscreen mode Exit fullscreen mode

The ratio x / (x - Δx) captures price impact — the larger the trade relative to reserves, the worse the execution price.


3. Impermanent Loss: A Mathematical Analysis

Impermanent loss (IL) is the difference in value between holding tokens in an AMM pool versus simply holding them in a wallet. It is one of the most important risks for liquidity providers.

3.1 Setup

Assume a liquidity provider (LP) deposits into a pool when the price of X in terms of Y is P_0 = y_0 / x_0. The invariant gives:

x_0 * y_0 = k

Enter fullscreen mode Exit fullscreen mode

At this point, the LP's portfolio value in terms of Y is:

V_hold = x_0 * P_0 + y_0 = 2 * y_0  (since x_0 * P_0 = y_0)

Enter fullscreen mode Exit fullscreen mode

3.2 After a Price Change

Let the external market price move to P_1. Arbitrageurs will rebalance the pool to match. The new reserves x_1, y_1 satisfy:

x_1 * y_1 = k           ... (invariant)
y_1 / x_1 = P_1         ... (new price)

Enter fullscreen mode Exit fullscreen mode

Solving:

x_1 = sqrt(k / P_1)
y_1 = sqrt(k * P_1)

Enter fullscreen mode Exit fullscreen mode

The LP's pool value in terms of Y is now:

V_pool = x_1 * P_1 + y_1
       = sqrt(k / P_1) * P_1 + sqrt(k * P_1)
       = sqrt(k * P_1) + sqrt(k * P_1)
       = 2 * sqrt(k * P_1)

Enter fullscreen mode Exit fullscreen mode

3.3 The IL Formula

Let r = P_1 / P_0 be the price ratio. Then:

k = x_0 * y_0 = x_0 * (x_0 * P_0) = x_0^2 * P_0

Enter fullscreen mode Exit fullscreen mode

So sqrt(k * P_1) = x_0 * sqrt(P_0 * P_1) = x_0 * P_0 * sqrt(r) = y_0 * sqrt(r)

Therefore:

V_pool = 2 * y_0 * sqrt(r)
V_hold = 2 * y_0

Enter fullscreen mode Exit fullscreen mode

Impermanent loss is defined as:

IL = (V_pool - V_hold) / V_hold
   = (2 * y_0 * sqrt(r) - 2 * y_0) / (2 * y_0)
   = sqrt(r) - 1

Enter fullscreen mode Exit fullscreen mode

More precisely, since the LP must compare pool value against holding the original token amounts at the new price:

V_hold_new = x_0 * P_1 + y_0 = x_0 * r * P_0 + x_0 * P_0 = x_0 * P_0 * (1 + r) = y_0 * (1 + r)

IL = V_pool / V_hold_new - 1
   = (2 * sqrt(r)) / (1 + r) - 1

Enter fullscreen mode Exit fullscreen mode

This is the canonical impermanent loss formula. Let's check key values:

Price Ratio r IL (%)
1.0 (no change) 0%
1.25 (+25%) -0.6%
1.5 (+50%) -2.0%
2.0 (+100%) -5.7%
4.0 (+300%) -20.0%
0.5 (-50%) -5.7%

Notice that IL is symmetric in log-price space: a halving and a doubling produce the same IL. Also, IL is always non-positive — the pool underperforms the holding strategy whenever prices change. However, trading fees can offset this loss.


4. Fees and the Real Invariant

In practice, Uniswap V2 charges a 0.3% fee on every swap. This means only (1 - 0.003) = 0.997 of the input is effectively traded. If a user sends Δy tokens of Y, the effective amount that shifts the invariant is 0.997 * Δy.

The actual swap formula becomes:

Δx = (x * 0.997 * Δy) / (y + 0.997 * Δy)

Enter fullscreen mode Exit fullscreen mode

Fees are collected in the pool, which increases k over time. This is the mechanism by which LPs earn yield. After many trades, the pool's k grows as:

k_new = k_old * (1 + fee_income / pool_value)

Enter fullscreen mode Exit fullscreen mode

LP shares (represented by ERC-20 LP tokens) represent a proportional claim on the pool's growing reserves.


5. Python Implementation

Here is a minimal Python simulation of a Uniswap V2 pool:

class UniswapV2Pool:
    def __init__(self, x: float, y: float, fee: float = 0.003):
        self.x = x
        self.y = y
        self.fee = fee
        self.k = x * y

    def spot_price(self) -> float:
        """Price of X in terms of Y"""
        return self.y / self.x

    def swap_x_for_y(self, dx: float) -> float:
        """Buy Y by selling dx of X. Returns amount of Y received."""
        dx_eff = dx * (1 - self.fee)
        dy = (self.y * dx_eff) / (self.x + dx_eff)
        self.x += dx
        self.y -= dy
        return dy

    def swap_y_for_x(self, dy: float) -> float:
        """Buy X by selling dy of Y. Returns amount of X received."""
        dy_eff = dy * (1 - self.fee)
        dx = (self.x * dy_eff) / (self.y + dy_eff)
        self.y += dy
        self.x -= dx
        return dx

    def impermanent_loss(self, initial_x: float, initial_y: float) -> float:
        """Compute IL relative to holding initial balances at current price."""
        P = self.spot_price()
        v_pool = self.x * P + self.y
        v_hold = initial_x * P + initial_y
        return (v_pool / v_hold) - 1

# Example
pool = UniswapV2Pool(x=100, y=200000)  # 100 ETH, 200k USDC
print(f"Initial price: {pool.spot_price()} USDC/ETH")
print(f"K = {pool.k}")

initial_x, initial_y = pool.x, pool.y

# Simulate a large buy of Y (selling ETH)
dy_received = pool.swap_x_for_y(dx=10)
print(f"Received: {dy_received:.2f} USDC for 10 ETH")
print(f"New price: {pool.spot_price():.2f} USDC/ETH")
print(f"IL: {pool.impermanent_loss(initial_x, initial_y)*100:.2f}%")

Enter fullscreen mode Exit fullscreen mode


6. Uniswap V3: Concentrated Liquidity

Uniswap V3 (released May 2021) introduced a fundamental generalization: concentrated liquidity. Instead of spreading liquidity uniformly across all possible prices from 0 to infinity, LPs can choose a specific price range [P_a, P_b] within which their capital is active.

6.1 The Generalized Invariant

Uniswap V3 transforms the V2 invariant by introducing virtual reserves. If an LP provides liquidity in range [P_a, P_b], the pool behaves as if it has larger reserves x_virtual and y_virtual that include an offset:

(x + x_offset) * (y + y_offset) = L^2

Enter fullscreen mode Exit fullscreen mode

where L is called liquidity and the offsets are:

x_offset = L / sqrt(P_b)
y_offset = L * sqrt(P_a)

Enter fullscreen mode Exit fullscreen mode

This ensures the curve "runs out" of one token at each end of the price range.

6.2 Liquidity as a Geometric Quantity

The parameter L measures the depth of the market at the current price. It is defined as:

L = sqrt(x * y)   (for the full-range V2 case)

Enter fullscreen mode Exit fullscreen mode

In V3, a position with liquidity L in range [P_a, P_b] requires the following capital:

Δx = L * (1/sqrt(P_current) - 1/sqrt(P_b))   if P_current < P_b
Δy = L * (sqrt(P_current) - sqrt(P_a))        if P_current > P_a

Enter fullscreen mode Exit fullscreen mode

This means the capital required for a given liquidity depth scales with the square root of the price, not linearly.

6.3 Capital Efficiency Comparison

The key innovation of V3 is capital efficiency. Consider a pool with price at P = 2000 USDC/ETH and an LP who wants to provide liquidity only in the narrow range [1900, 2100] (a ~10% band).

In V2, the LP's capital is spread across [0, infinity), so only a tiny fraction is ever used for trades near the current price.

In V3, all of the LP's capital is concentrated in that narrow range, providing much deeper liquidity per dollar deployed. The capital efficiency gain is approximately:

Efficiency Gain = sqrt(P_b / P_a) / (sqrt(P_b / P_a) - 1)

For [1900, 2100]: sqrt(2100/1900) / (sqrt(2100/1900) - 1)
                = sqrt(1.105) / (sqrt(1.105) - 1)
                = 1.0513 / 0.0513
                ~ 20.5x

Enter fullscreen mode Exit fullscreen mode

A V3 LP in this narrow range achieves roughly 20x better capital efficiency than a V2 LP with the same amount of capital.

6.4 Tick Architecture

V3 discretizes the price range into ticks. Each tick corresponds to a price:

P(i) = 1.0001^i

Enter fullscreen mode Exit fullscreen mode

This gives approximately 1 basis point (0.01%) spacing between adjacent ticks. Liquidity positions are defined between integer tick boundaries, and the contract tracks aggregate liquidity per tick using a tick bitmap for efficient O(1) updates.

When a swap moves the price across a tick boundary, the contract initializes or exits LP positions at that boundary, updating the active liquidity accordingly.

6.5 V2 vs V3: Key Differences

Feature Uniswap V2 Uniswap V3
Price Range [0, infinity) [P_a, P_b] custom
Capital Efficiency Low Up to 4000x higher
LP Complexity Simple Requires active management
Fee Tiers Fixed 0.3% 0.05%, 0.3%, 1%
LP Token Fungible ERC-20 Non-fungible ERC-721 (NFT)
IL Exposure Moderate Higher within range

7. Slippage Tolerance in Practice

Real-world AMM integrations must handle slippage — the difference between the expected price and the execution price. Most DEX aggregators let users set a slippage tolerance (e.g., 0.5%).

The maximum output a user can receive is bounded by the constant-product formula. For a trade of Δy input:

Minimum output Δx_min = Δx_expected * (1 - slippage_tolerance)

Enter fullscreen mode Exit fullscreen mode

If the pool state changes between transaction submission and execution (due to front-running or sandwich attacks), and the output falls below Δx_min, the transaction reverts. This is enforced by Uniswap's smart contract via:

require(amountOut >= amountOutMinimum, 'Too little received');

Enter fullscreen mode Exit fullscreen mode


8. Conclusion

The constant-product formula x * y = k is one of the most elegant and impactful mathematical primitives in modern finance. From it, we can derive:

  • A closed-form pricing function that requires no order book
  • An impermanent loss formula IL = 2*sqrt(r)/(1+r) - 1 that precisely quantifies LP risk
  • A generalization in Uniswap V3 that concentrates liquidity to dramatically improve capital efficiency

The brilliance of the design is that a single mathematical invariant — just three tokens and a multiplication — creates a self-contained, trustless exchange that operates entirely on-chain. Understanding the mathematics behind AMMs is essential for anyone building in DeFi, whether as a developer, LP, or protocol designer.


References

  1. Adams, H., Zinsmeister, N., Salem, M., Keefer, R., & Robinson, D. (2021). Uniswap v3 Core. Uniswap Labs. https://uniswap.org/whitepaper-v3.pdf

  2. Adams, H., Zinsmeister, N., & Robinson, D. (2020). Uniswap v2 Core. Uniswap Labs. https://uniswap.org/whitepaper.pdf

  3. Angeris, G., Kao, H. T., Chiang, R., Noyes, C., & Chitra, T. (2019). An analysis of Uniswap markets. arXiv:1911.03380.

  4. Pintail. (2019). Understanding Uniswap Returns. Medium. https://medium.com/@pintail/understanding-uniswap-returns-cc593f3499ef

  5. Buterin, V. (2021). Improving front running resistance of x*y=k market makers. Ethereum Research. https://ethresear.ch/t/improving-front-running-resistance-of-x-y-k-market-makers/1281