Budowanie prostego swapa
Mechanika AMM
Budowanie prostego swapa to bezpłatna lekcja Web3 & DApp Development Fundamentals na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Web3 & DApp Development Fundamentals, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Web3 & DApp Development Fundamentals zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Goal: A Minimal AMM Swap
In this lesson we sketch a minimal constant-product swap contract in Solidity to see how the formula becomes code.
We keep it simple to focus on mechanics, not production safety.
Pool State
Our contract tracks two token reserves and the LP token supply. These three values fully describe the pool.
contract SimpleSwap {
IERC20 public tokenA;
IERC20 public tokenB;
uint public reserveA;
uint public reserveB;
uint public totalShares;
}Adding Liquidity
When a provider adds liquidity, we pull both tokens, update reserves, and mint shares proportional to the deposit.
function addLiquidity(uint amtA, uint amtB) external {
tokenA.transferFrom(msg.sender, address(this), amtA);
tokenB.transferFrom(msg.sender, address(this), amtB);
reserveA += amtA;
reserveB += amtB;
// mint shares to msg.sender
}The Swap Function Signature
A swap takes an input amount of one token and returns the maximum possible output of the other, governed by x * y = k.
function swapAforB(uint amountAIn)
external
returns (uint amountBOut)
{
// ... apply constant product
}Applying the Fee
Before computing output, we deduct a 0.30% fee by scaling the input to 99.7% of its value.
uint amountInWithFee = amountAIn * 997 / 1000;
// 0.30% of the input stays in the poolComputing the Output Amount
Using constant product, the output is derived so the product of new reserves still equals k.
amountBOut =
(reserveB * amountInWithFee) /
(reserveA + amountInWithFee);Updating Reserves
After computing the output we update both reserves and transfer the output token to the trader.
reserveA += amountAIn;
reserveB -= amountBOut;
tokenB.transfer(msg.sender, amountBOut);Putting the Swap Together
Here is the full swap path: pull input, apply fee, compute output, update reserves, send output.
function swapAforB(uint amountAIn) external returns (uint out) {
tokenA.transferFrom(msg.sender, address(this), amountAIn);
uint inWithFee = amountAIn * 997 / 1000;
out = (reserveB * inWithFee) / (reserveA + inWithFee);
reserveA += amountAIn;
reserveB -= out;
tokenB.transfer(msg.sender, out);
}Protecting Against Slippage
Real swaps accept a minAmountOut parameter and revert if the output is worse than expected.
This shields traders from price changes (or front-running) between submitting and executing.
require(out >= minAmountOut, "slippage");What We Left Out
A production AMM adds much more:
- Reentrancy guards
- Fee accounting for LPs
- Flash-loan and price-manipulation defenses
- Events for indexers
Never deploy a teaching contract to mainnet.
Putting It Together
A swap is just the constant-product formula expressed in code: take input minus fee, compute the output that keeps k, update reserves, and transfer.
Everything else (LP accounting, safety) wraps around this core idea.
Quick Check
Test your swap implementation understanding.
Recap: Building a Simple Swap
You learned that:
- A pool tracks reserves and shares
- The swap applies a fee then the constant product formula
- Output =
reserveB * inWithFee / (reserveA + inWithFee) - minAmountOut guards against slippage
- Production needs guards, events, and audits
Course complete! Next course: DAO Development.
Ucz się Web3 & DApp Development Fundamentals dzięki korepetycjom AI — za darmo
Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.
- Kursy
- 29
- Lekcje
- 105
Często zadawane pytania
Czy lekcja „Budowanie prostego swapa” jest bezpłatna?
Tak — pełny tekst „Budowanie prostego swapa” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Web3 & DApp Development Fundamentals, przejdź na CoddyKit PRO. Kurs Web3 & DApp Development Fundamentals zawiera 4 lekcji w sumie.
Co nauczysz się w „Budowanie prostego swapa”?
Mechanika AMM Ćwiczysz Web3 & DApp Development Fundamentals z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Web3 & DApp Development Fundamentals?
Nie wymagamy żadnego doświadczenia. Web3 & DApp Development Fundamentals w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Budowanie prostego swapa”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Web3 & DApp Development Fundamentals?
Tak. Każda lekcja Web3 & DApp Development Fundamentals zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Przegląd DeFi
- Automatyczni animatorzy rynku
- Pule płynności
- Budowanie prostego swapa