0Pricing
Web3 & DApp Development Fundamentals · Lesson

Building a Simple Swap

AMM mechanics.

Building a Simple Swap is a free Web3 & DApp Development Fundamentals lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Web3 & DApp Development Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Building a Simple Swap” lesson free?

Yes — the full text of “Building a Simple Swap” is free to read here on the web, and the Web3 & DApp Development Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Web3 & DApp Development Fundamentals course, upgrade to CoddyKit PRO.

What will I learn in “Building a Simple Swap”?

AMM mechanics. You practise Web3 & DApp Development Fundamentals with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Web3 & DApp Development Fundamentals?

No prior experience is required. Web3 & DApp Development Fundamentals on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Building a Simple Swap” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Web3 & DApp Development Fundamentals lesson?

Yes. Every Web3 & DApp Development Fundamentals lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. DeFi Overview
  2. Automated Market Makers
  3. Liquidity Pools
  4. Building a Simple Swap
← Back to Web3 & DApp Development Fundamentals