Einen einfachen Swap erstellen
AMM-Mechanik
Einen einfachen Swap erstellen ist eine kostenlose Web3 & DApp Development Fundamentals-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Web3 & DApp Development Fundamentals-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Web3 & DApp Development Fundamentals-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „Einen einfachen Swap erstellen“ kostenlos?
Ja — der vollständige Text von „Einen einfachen Swap erstellen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Web3 & DApp Development Fundamentals-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Web3 & DApp Development Fundamentals-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Einen einfachen Swap erstellen“?
AMM-Mechanik Du übst Web3 & DApp Development Fundamentals mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Web3 & DApp Development Fundamentals zu starten?
Keine Vorkenntnisse erforderlich. Web3 & DApp Development Fundamentals auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Einen einfachen Swap erstellen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Web3 & DApp Development Fundamentals-Lektion Code schreiben und ausführen?
Ja. Jede Web3 & DApp Development Fundamentals-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- DeFi-Überblick
- Automated Market Maker
- Liquiditätspools
- Einen einfachen Swap erstellen