0Pricing
Web3 & DApp Development Fundamentals · レッスン

シンプルなSwapの構築

AMMの仕組み

「シンプルなSwapの構築」はCoddyKit上の無料Web3 & DApp Development Fundamentalsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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 pool

Computing 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.

よくある質問

「シンプルなSwapの構築」レッスンは無料ですか?

はい。「シンプルなSwapの構築」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Web3 & DApp Development Fundamentalsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Web3 & DApp Development Fundamentalsコースには全4レッスンが含まれています。

「シンプルなSwapの構築」で何を学びますか?

AMMの仕組み ブラウザで直接実行するハンズオンコードでWeb3 & DApp Development Fundamentalsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Web3 & DApp Development Fundamentalsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのWeb3 & DApp Development Fundamentalsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「シンプルなSwapの構築」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このWeb3 & DApp Development Fundamentalsレッスンでコードを書いて実行できますか?

はい。すべてのWeb3 & DApp Development Fundamentalsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. DeFi概要
  2. 自動マーケットメーカー
  3. 流動性プール
  4. シンプルなSwapの構築
← Web3 & DApp Development Fundamentalsに戻る