0
0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style9 modes available
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.