بناء Swap بسيط
آليات AMM
بناء Swap بسيط درس مجاني في Web3 & DApp Development Fundamentals على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Web3 & DApp Development Fundamentals، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Web3 & DApp Development Fundamentals 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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.
تعلم Web3 & DApp Development Fundamentals مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 29
- الدروس
- 105
الأسئلة الشائعة
هل درس «بناء Swap بسيط» مجاني؟
نعم — نص درس «بناء Swap بسيط» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Web3 & DApp Development Fundamentals، انتقل إلى CoddyKit PRO. تتضمن دورة Web3 & DApp Development Fundamentals 4 دروس في المجموع.
ماذا ستتعلم في «بناء Swap بسيط»؟
آليات AMM تتمرن على Web3 & DApp Development Fundamentals مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Web3 & DApp Development Fundamentals؟
لا تُشترط خبرة سابقة. Web3 & DApp Development Fundamentals على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «بناء Swap بسيط»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Web3 & DApp Development Fundamentals هذا؟
نعم. كل درس في Web3 & DApp Development Fundamentals يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- نظرة عامة على DeFi
- صنّاع السوق الآليون
- مجمّعات السيولة
- بناء Swap بسيط