Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Automated Market Makers (AMM) in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
AMM Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this AMM liquidity pool share calculation?
Consider a simple AMM liquidity pool with 1000 tokens of TokenA and 2000 tokens of TokenB. A user adds 100 TokenA and 200 TokenB to the pool. What is the user's share of the pool after adding liquidity?
Blockchain / Solidity
initial_tokenA = 1000
initial_tokenB = 2000
user_tokenA = 100
user_tokenB = 200

# Total pool tokens before adding liquidity
pool_tokens_before = 10000

# Calculate share based on TokenA added
shareA = user_tokenA / (initial_tokenA + user_tokenA)

# Calculate share based on TokenB added
shareB = user_tokenB / (initial_tokenB + user_tokenB)

# User share is minimum of both shares
user_share = min(shareA, shareB)

# New pool tokens minted
new_pool_tokens = pool_tokens_before * user_share

print(round(new_pool_tokens))
A909
B1000
C500
D1100
Attempts:
2 left
💡 Hint
The user's share depends on the smaller ratio of tokens added relative to the pool.
🧠 Conceptual
intermediate
1:30remaining
Which option correctly describes the constant product formula in AMMs?
In an Automated Market Maker using the constant product formula, which statement is true?
AThe difference between the quantities of two tokens in the pool remains constant after each trade.
BThe sum of the quantities of two tokens in the pool remains constant after each trade.
CThe product of the quantities of two tokens in the pool remains constant after each trade.
DThe ratio of the quantities of two tokens in the pool remains constant after each trade.
Attempts:
2 left
💡 Hint
Think about the formula x * y = k used in many AMMs.
🔧 Debug
advanced
2:30remaining
Find the error in this AMM swap function code snippet
This Python function is supposed to calculate the output amount of TokenB when swapping TokenA in an AMM with a 0.3% fee. What error causes incorrect output?
Blockchain / Solidity
def get_amount_out(amount_in, reserve_in, reserve_out):
    fee = 0.003
    amount_in_with_fee = amount_in * (1 - fee)
    numerator = amount_in_with_fee * reserve_out
    denominator = reserve_in + amount_in_with_fee
    return numerator / denominator

# Example usage
print(round(get_amount_out(100, 1000, 2000), 2))
AThe fee should be applied as 'amount_in * 0.997' instead of 'amount_in * (1 - fee)'.
BThe fee calculation is correct, but the denominator should be 'reserve_in - amount_in_with_fee'.
CThe fee should be subtracted after multiplying numerator, not before.
DThe function incorrectly uses floating division instead of integer division."
Attempts:
2 left
💡 Hint
Check how the fee is applied to the input amount.
📝 Syntax
advanced
1:30remaining
Which option causes a syntax error in this Solidity AMM contract snippet?
Given this Solidity function snippet for swapping tokens, which option contains a syntax error?
Blockchain / Solidity
function swap(uint amountIn, uint reserveIn, uint reserveOut) public pure returns (uint amountOut) {
    uint amountInWithFee = amountIn * 997 / 1000;
    uint numerator = amountInWithFee * reserveOut;
    uint denominator = reserveIn + amountInWithFee;
    amountOut = numerator / denominator;
}
A
function swap(uint amountIn, uint reserveIn, uint reserveOut) public pure returns (uint amountOut) {
    uint amountInWithFee = amountIn * 997 / 1000
    uint numerator = amountInWithFee * reserveOut;
    uint denominator = reserveIn + amountInWithFee;
    amountOut = numerator / denominator;
}
B
function swap(uint amountIn, uint reserveIn, uint reserveOut) public pure returns (uint amountOut) {
    uint amountInWithFee = amountIn * 997 / 1000;
    uint numerator = amountInWithFee * reserveOut;
    uint denominator = reserveIn + amountInWithFee;
    amountOut = numerator / denominator;
}
C
function swap(uint amountIn, uint reserveIn, uint reserveOut) public pure returns (uint amountOut) {
    uint amountInWithFee = amountIn * 997 / 1000;
    uint numerator = amountInWithFee * reserveOut;
    uint denominator = reserveIn + amountInWithFee;
    amountOut = numerator / denominator;
    return amountOut;
}
D
function swap(uint amountIn, uint reserveIn, uint reserveOut) public pure returns uint amountOut {
    uint amountInWithFee = amountIn * 997 / 1000;
    uint numerator = amountInWithFee * reserveOut;
    uint denominator = reserveIn + amountInWithFee;
    amountOut = numerator / denominator;
}
Attempts:
2 left
💡 Hint
Check the syntax of the returns statement in Solidity.
🚀 Application
expert
3:00remaining
How many unique price points can this AMM produce with given reserves?
An AMM uses the constant product formula x * y = k with reserves x=500 and y=500. If a trader can swap TokenA in increments of 50 tokens, how many unique output amounts of TokenB can the AMM produce (excluding zero swaps)?
A10
B9
C11
D8
Attempts:
2 left
💡 Hint
Calculate how many increments fit into the reserve and consider that swapping all tokens is not possible.

Practice

(1/5)
1. What is the main purpose of an Automated Market Maker (AMM) in blockchain?
easy
A. To enable token trading without a middleman using math formulas
B. To store user passwords securely
C. To mine new blocks in the blockchain
D. To create new tokens automatically

Solution

  1. Step 1: Understand AMM's role

    AMMs allow users to trade tokens directly without needing a traditional exchange or middleman.
  2. Step 2: Identify the key feature

    They use mathematical formulas and token reserves to set prices and enable swaps.
  3. Final Answer:

    To enable token trading without a middleman using math formulas -> Option A
  4. Quick 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
A. x + y = k
B. x - y = k
C. x / y = k
D. x * y = k

Solution

  1. Step 1: Recall AMM constant product formula

    AMMs use the formula where the product of token reserves remains constant.
  2. Step 2: Identify the correct formula

    The formula is x * y = k, where x and y are token reserves and k is constant.
  3. Final Answer:

    x * y = k -> Option D
  4. Quick 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
A. 181.82
B. 220
C. 190
D. 200

Solution

  1. Step 1: Calculate constant k

    k = x * y = 100 * 200 = 20000.
  2. Step 2: Calculate new y after adding 10 to x

    New x = 100 + 10 = 110. New y = k / new x = 20000 / 110 ≈ 181.82.
  3. Final Answer:

    181.82 -> Option A
  4. Quick 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_y
medium
A. The function returns new_y instead of the difference
B. The function uses addition instead of multiplication for k
C. The function does not account for swap fees
D. The function uses integer division instead of float division

Solution

  1. Step 1: Review function logic

    The function calculates k correctly and finds new reserves after swap.
  2. Step 2: Check for missing AMM details

    It does not include swap fees, which reduce the effective input amount.
  3. Final Answer:

    The function does not account for swap fees -> Option C
  4. Quick 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
A. def swap_output(x, y, x_in): fee = 0.003 x_in_with_fee = x_in + fee k = x * y new_x = x + x_in_with_fee new_y = k / new_x return y - new_y
B. def swap_output(x, y, x_in): fee = 0.003 x_in_with_fee = x_in * (1 - fee) k = x * y new_x = x + x_in_with_fee new_y = k / new_x return y - new_y
C. def swap_output(x, y, x_in): fee = 0.003 x_in_with_fee = x_in * fee k = x * y new_x = x + x_in_with_fee new_y = k / new_x return y - new_y
D. def swap_output(x, y, x_in): fee = 0.003 x_in_with_fee = x_in / (1 - fee) k = x * y new_x = x + x_in_with_fee new_y = k / new_x return y - new_y

Solution

  1. Step 1: Calculate effective input after fee

    The input tokens are reduced by the fee: x_in_with_fee = x_in * (1 - 0.003).
  2. 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.
  3. Final Answer:

    Code snippet B correctly applies the fee and calculates output -> Option B
  4. Quick 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