Web3 & DApp Development Fundamentals · 课时

自动做市商

恒定乘积公式

第 2 / 4 课13 个步骤

自动做市商 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What Is an AMM?

An Automated Market Maker (AMM) is a smart contract that prices and executes trades using a mathematical formula instead of an order book.

Instead of matching a buyer with a seller, traders swap against a shared pool of tokens.

Order Books vs AMMs

Traditional exchanges match individual buy and sell orders. AMMs replace that with a liquidity pool and a pricing curve.

  • Order book: needs active market makers
  • AMM: always available, prices set by formula

The Liquidity Pool

An AMM pool holds a reserve of two tokens, for example ETH and USDC. Traders add one token and remove the other.

The pool's reserves determine the price — the contract never needs a counterparty.

Pool reserves:
    x = 100 ETH
    y = 200000 USDC

The Constant Product Formula

The most famous AMM (Uniswap v2) uses the constant product formula:

x * y = k

The product of the two reserves must stay equal to a constant k after every trade (ignoring fees).

x * y = k
100 ETH * 200000 USDC = 20,000,000 = k

How a Swap Moves Reserves

To buy ETH, a trader adds USDC. The USDC reserve grows and the ETH reserve shrinks, but the product must remain k.

Before: 100 ETH * 200000 USDC = 20,000,000
Trader adds 10000 USDC -> y = 210000
New x = k / y = 20,000,000 / 210000
      = 95.238 ETH
Trader receives 100 - 95.238 = 4.76 ETH

Price From the Curve

The instantaneous price is the ratio of reserves: price = y / x.

As one reserve shrinks, that token becomes more expensive. The curve makes large trades cost progressively more per unit.

price of ETH in USDC = y / x
= 200000 / 100 = 2000 USDC per ETH

Slippage

Slippage is the difference between the expected price and the price you actually get. Big trades move along the curve and suffer more slippage.

Deeper pools (larger reserves) reduce slippage because each trade shifts the ratio less.

Trading Fees

Each swap charges a fee (often 0.30%) that is added to the pool. This grows k slightly over time.

Fees are the reward that compensates liquidity providers for supplying capital.

amountIn after fee = amountIn * 0.997
// 0.30% fee stays in the pool

Arbitrage Keeps Prices Honest

If the AMM price drifts from the wider market, arbitrageurs trade against the pool until it matches.

This self-correcting pressure keeps AMM prices aligned with external markets without any oracle.

Other AMM Curves

Constant product is not the only design:

  • Curve uses a stableswap formula for assets that should trade near 1:1
  • Uniswap v3 adds concentrated liquidity within chosen price ranges

Each curve optimizes for a different asset type.

Putting It Together

AMMs price trades with a formula like x * y = k against a shared pool. Reserves set the price, large trades cause slippage, fees reward providers, and arbitrage keeps prices accurate.

Next we look at the liquidity pools themselves.

Quick Check

Test your AMM math intuition.

Recap: Automated Market Makers

You learned that:

  • AMMs replace order books with a formula + pool
  • The constant product rule is x * y = k
  • Price is the reserve ratio; big trades cause slippage
  • Fees reward providers; arbitrage keeps prices honest

Next: providing liquidity to these pools.

免费开始

用 AI 导师学习 Web3 & DApp Development Fundamentals — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
29
课程
105

常见问题解答

「自动做市商」课时是免费的吗?

是的 — 「自动做市商」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。

「自动做市商」这节课中我会学到什么?

恒定乘积公式 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Web3 & DApp Development Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「自动做市商」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?

能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. DeFi 概览
  2. 自动做市商
  3. 流动性池
  4. 构建简单交换
← 返回 Web3 & DApp Development Fundamentals