AMM과 유동성 풀
Uniswap과 같은 자동화된 시장 조성자(AMM)의 작동 방식과 유동성 풀이 탈중앙화 거래를 가능하게 하는 방식을 이해합니다.
AMM과 유동성 풀은(는) CoddyKit의 무료 Blockchain Smart Contracts with Solidity 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Blockchain Smart Contracts with Solidity 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Blockchain Smart Contracts with Solidity 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Welcome to AMMs
Decentralized Finance (DeFi) has revolutionized how we trade assets. At its heart are Automated Market Makers (AMMs).
AMMs power decentralized exchanges (DEXs) like Uniswap, allowing users to swap tokens without traditional buyers and sellers.
Instead of matching orders, AMMs use mathematical formulas and liquidity pools.
Order Books vs. AMMs
Traditional exchanges use order books, where buyers and sellers list their desired prices. Trades only happen when bids and asks match.
- Order Books: Centralized, requires matching orders, can suffer from low liquidity.
- AMMs: Decentralized, uses liquidity pools, always offers a price determined by a formula.
AMMs solve the liquidity problem for many token pairs on a blockchain by always providing a market.
The Power of Liquidity Pools
An AMM's backbone is the liquidity pool. This is a collection of two or more tokens locked in a smart contract.
For example, a common pool might hold ETH and a stablecoin like DAI. Users can swap between these tokens directly from the pool.
These pools are funded by individuals called Liquidity Providers (LPs).
Meet the Constant Product Formula
Most AMMs, like Uniswap, use the constant product formula: x * y = k.
xis the quantity of the first token.yis the quantity of the second token.kis a constant product that must remain unchanged after a trade.
When you swap tokens, the quantities of x and y change, but their product k stays the same, determining the new price ratio.
How to Become an LP
Liquidity Providers (LPs) deposit an equal value of two tokens into a liquidity pool. For example, $1000 worth of ETH and $1000 worth of DAI.
In return, LPs receive Liquidity Pool (LP) tokens. These tokens represent their share of the pool.
LP tokens can be redeemed later to withdraw your original deposit plus any accumulated fees.
Adding Liquidity in Practice
When an LP adds liquidity, they increase the total supply of x and y in the pool, which also increases the constant k.
The AMM smart contract ensures that tokens are added in the correct ratio to maintain the pool's balance and prevent immediate price changes.
This process makes the pool deeper, allowing for larger trades with less price impact.
Swapping Tokens
When a user wants to swap Token A for Token B, they send Token A to the liquidity pool.
The AMM's smart contract then calculates how much Token B to give back, based on the x * y = k formula.
The pool's balance shifts, and the price of the tokens adjusts automatically according to the formula.
Code Example: Simulating a Swap
Let's see a simplified Solidity contract that simulates how an AMM might perform a swap using the constant product formula.
This example demonstrates the core calculation and reserve update.
pragma solidity ^0.8.0;
contract SimpleAmmSwap {
uint public tokenXReserve = 1000 * 10**18; // 1000 tokens (e.g., ETH) with 18 decimals
uint public tokenYReserve = 1000 * 10**18; // 1000 tokens (e.g., DAI) with 18 decimals
uint public k = tokenXReserve * tokenYReserve;
// Simulate a swap from Token X to Token Y
// For simplicity, no actual token transfers, just reserve updates.
function swapXForY(uint amountInX) public returns (uint amountOutY) {
require(amountInX > 0, "Amount in must be positive");
// Calculate new reserves and output amount
uint newXReserve = tokenXReserve + amountInX;
uint newYReserve = k / newXReserve;
amountOutY = tokenYReserve - newYReserve;
require(amountOutY > 0, "Not enough liquidity for swap");
// Update reserves
tokenXReserve = newXReserve;
tokenYReserve = newYReserve;
// Note: In a real AMM, actual token transfers would occur here.
}
// A getter to check current reserves
function getReserves() public view returns (uint, uint) {
return (tokenXReserve, tokenYReserve);
}
}Understanding Impermanent Loss
One major risk for LPs is impermanent loss. This occurs when the price of the assets in the liquidity pool changes relative to when you deposited them.
If the price ratio diverges significantly, the value of your tokens withdrawn from the pool might be less than if you had simply held them outside the pool.
It's called "impermanent" because the loss only becomes permanent if you withdraw your liquidity before the prices return to their original ratio.
LP Fees and Rewards
Despite impermanent loss, LPs are incentivized by earning a share of the trading fees generated by the pool.
Each time a trade occurs, a small percentage (e.g., 0.3%) of the swapped amount is added to the liquidity pool.
These fees accumulate in the pool and are distributed proportionally to LPs when they withdraw their liquidity, compensating them for providing capital and taking on impermanent loss risk.
Quick Check: AMM Basics
Test your understanding of Automated Market Makers and Liquidity Pools.
Recap: AMMs & Liquidity Pools
We've explored how Automated Market Makers (AMMs) provide decentralized trading by using liquidity pools instead of traditional order books.
Key takeaways:
- AMMs use formulas like
x * y = kto determine prices. - Liquidity Providers (LPs) fund pools and earn trading fees.
- Impermanent loss is a risk for LPs due to price divergence.
Understanding these concepts is vital for navigating the DeFi landscape!
자주 묻는 질문
“AMM과 유동성 풀” 강의는 무료인가요?
네 — “AMM과 유동성 풀” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Blockchain Smart Contracts with Solidity 강의 전체를 잠금 해제할 수 있습니다. Blockchain Smart Contracts with Solidity 강의에는 총 4개의 강의가 포함되어 있습니다.
“AMM과 유동성 풀”에서 뭘 배우나요?
Uniswap과 같은 자동화된 시장 조성자(AMM)의 작동 방식과 유동성 풀이 탈중앙화 거래를 가능하게 하는 방식을 이해합니다. 브라우저에서 직접 실행하는 실습 코드로 Blockchain Smart Contracts with Solidity을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Blockchain Smart Contracts with Solidity을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Blockchain Smart Contracts with Solidity은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“AMM과 유동성 풀” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Blockchain Smart Contracts with Solidity 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Blockchain Smart Contracts with Solidity 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- AMM과 유동성 풀
- 대출 및 차입 프로토콜
- 플래시 론과 차익 거래
- 이자 농사와 스테이킹 보상