AMM dan Pool Likuiditas
Pahami cara kerja Automated Market Makers (AMM) seperti Uniswap dan cara pool likuiditas memungkinkan perdagangan terdesentralisasi.
AMM dan Pool Likuiditas adalah pelajaran Blockchain Smart Contracts with Solidity gratis di CoddyKit. Ini adalah pelajaran 1 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Blockchain Smart Contracts with Solidity, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Blockchain Smart Contracts with Solidity mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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!
Belajar Blockchain Smart Contracts with Solidity dengan tutor AI — gratis
Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.
- Kursus
- 12
- Pelajaran
- 48
Pertanyaan yang Sering Diajukan
Apakah pelajaran “AMM dan Pool Likuiditas” gratis?
Ya — teks lengkap “AMM dan Pool Likuiditas” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Blockchain Smart Contracts with Solidity, upgrade ke CoddyKit PRO. Kursus Blockchain Smart Contracts with Solidity mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “AMM dan Pool Likuiditas”?
Pahami cara kerja Automated Market Makers (AMM) seperti Uniswap dan cara pool likuiditas memungkinkan perdagangan terdesentralisasi. Kamu berlatih Blockchain Smart Contracts with Solidity dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Blockchain Smart Contracts with Solidity?
Tidak diperlukan pengalaman sebelumnya. Blockchain Smart Contracts with Solidity di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 1 dari 4.
Berapa lama pelajaran “AMM dan Pool Likuiditas” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Blockchain Smart Contracts with Solidity ini?
Ya. Setiap pelajaran Blockchain Smart Contracts with Solidity menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- AMM dan Pool Likuiditas
- Protokol Peminjaman dan Peminjaman Dana
- Flash Loan dan Arbitrase
- Yield Farming dan Imbalan Staking