Automated Market Makers (AMMs) help people trade cryptocurrencies without needing a middleman. They use math formulas to set prices and let anyone buy or sell tokens easily.
Automated Market Makers (AMM) in Blockchain / Solidity
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Blockchain / Solidity
function getAmountOut(amountIn, reserveIn, reserveOut) {
const amountInWithFee = amountIn * 0.997;
const numerator = amountInWithFee * reserveOut;
const denominator = reserveIn * 1 + amountInWithFee;
return numerator / denominator;
}This function calculates how many tokens you get when you swap.
It uses a simple formula from the constant product model: reserveIn * reserveOut = constant.
Examples
Blockchain / Solidity
const amountOut = getAmountOut(100, 1000, 1000); console.log(amountOut);
Blockchain / Solidity
const amountOut = getAmountOut(50, 500, 1500); console.log(amountOut);
Sample Program
This program shows how many TokenB you get when swapping 100 TokenA using an AMM formula.
Blockchain / Solidity
function getAmountOut(amountIn, reserveIn, reserveOut) {
const amountInWithFee = amountIn * 0.997;
const numerator = amountInWithFee * reserveOut;
const denominator = reserveIn * 1 + amountInWithFee;
return numerator / denominator;
}
const reserveTokenA = 1000;
const reserveTokenB = 1000;
const amountInTokenA = 100;
const amountOutTokenB = getAmountOut(amountInTokenA, reserveTokenA, reserveTokenB);
console.log(`Swapping ${amountInTokenA} TokenA gives you ${amountOutTokenB.toFixed(2)} TokenB`);Important Notes
AMMs use a fee (like 0.3%) to reward liquidity providers.
The constant product formula keeps the pool balanced and prices fair.
Prices change automatically based on how much you swap and the pool reserves.
Summary
AMMs let you trade tokens without a middleman using math formulas.
They use reserves and a constant product formula to set prices.
You can calculate how many tokens you get from a swap with a simple function.
Practice
1. What is the main purpose of an Automated Market Maker (AMM) in blockchain?
easy
Solution
Step 1: Understand AMM's role
AMMs allow users to trade tokens directly without needing a traditional exchange or middleman.Step 2: Identify the key feature
They use mathematical formulas and token reserves to set prices and enable swaps.Final Answer:
To enable token trading without a middleman using math formulas -> Option AQuick Check:
AMM = trading without middleman [OK]
Hint: AMMs trade tokens using formulas, no middleman needed [OK]
Common Mistakes:
- Confusing AMM with mining or token creation
- Thinking AMM stores passwords
- Assuming AMM creates tokens automatically
2. Which of the following is the correct formula used by a constant product AMM to maintain balance?
easy
Solution
Step 1: Recall AMM constant product formula
AMMs use the formula where the product of token reserves remains constant.Step 2: Identify the correct formula
The formula is x * y = k, where x and y are token reserves and k is constant.Final Answer:
x * y = k -> Option DQuick Check:
Product of reserves = constant [OK]
Hint: Remember: AMM uses multiplication for constant product [OK]
Common Mistakes:
- Using addition or subtraction instead of multiplication
- Confusing division with the formula
- Mixing up variables and constants
3. Given an AMM with reserves x = 100 and y = 200, what is the new y reserve after adding 10 tokens to x and keeping k constant?
medium
Solution
Step 1: Calculate constant k
k = x * y = 100 * 200 = 20000.Step 2: Calculate new y after adding 10 to x
New x = 100 + 10 = 110. New y = k / new x = 20000 / 110 ≈ 181.82.Final Answer:
181.82 -> Option AQuick Check:
New y = 20000 / 110 ≈ 181.82 [OK]
Hint: Divide k by new x to find new y quickly [OK]
Common Mistakes:
- Adding instead of dividing to find new y
- Using old y value without adjustment
- Forgetting to add tokens to x before calculation
4. Identify the error in this Python function that calculates output tokens from an AMM swap:
def get_output_amount(x_reserve, y_reserve, x_in):
k = x_reserve * y_reserve
new_x = x_reserve + x_in
new_y = k / new_x
return y_reserve - new_ymedium
Solution
Step 1: Review function logic
The function calculates k correctly and finds new reserves after swap.Step 2: Check for missing AMM details
It does not include swap fees, which reduce the effective input amount.Final Answer:
The function does not account for swap fees -> Option CQuick Check:
Missing fees in calculation [OK]
Hint: Remember to subtract swap fees from input amount [OK]
Common Mistakes:
- Ignoring swap fees in calculations
- Confusing return values
- Using integer division in Python 3 (which is float by default)
5. You want to implement a function that calculates the output token amount from a swap on an AMM with a 0.3% fee. Given reserves x=500, y=1000, and input x_in=50, which code snippet correctly calculates the output amount?
hard
Solution
Step 1: Calculate effective input after fee
The input tokens are reduced by the fee: x_in_with_fee = x_in * (1 - 0.003).Step 2: Calculate new reserves and output
Use constant product k = x * y, then new_x = x + x_in_with_fee, new_y = k / new_x, output = y - new_y.Final Answer:
Code snippet B correctly applies the fee and calculates output -> Option BQuick Check:
Subtract fee before adding input [OK]
Hint: Multiply input by (1 - fee) before calculation [OK]
Common Mistakes:
- Adding fee instead of subtracting
- Multiplying input by fee only
- Dividing input by (1 - fee) incorrectly
