Automated Market Makers (AMM) in Blockchain / Solidity - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using Automated Market Makers (AMM), it is important to understand how the time to process trades grows as more users interact with the system.
We want to know how the number of operations changes when the number of trades or liquidity providers increases.
Analyze the time complexity of the following simplified AMM swap function.
function swap(inputAmount, inputReserve, outputReserve) {
const inputAmountWithFee = inputAmount * 997;
const numerator = inputAmountWithFee * outputReserve;
const denominator = inputReserve * 1000 + inputAmountWithFee;
const outputAmount = numerator / denominator;
return outputAmount;
}
This function calculates how many output tokens a user receives when swapping input tokens, based on current reserves.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The function performs a fixed number of arithmetic calculations.
- How many times: These calculations happen once per swap call, no loops or recursion inside.
The number of operations stays the same no matter how large the input amount or reserves are.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 arithmetic operations |
| 100 | 5 arithmetic operations |
| 1000 | 5 arithmetic operations |
Pattern observation: The work done does not increase with input size; it stays constant.
Time Complexity: O(1)
This means the time to calculate a swap does not grow as the input or reserves grow; it takes the same amount of time every call.
[X] Wrong: "The swap function takes longer if the input amount is bigger because it has to do more work."
[OK] Correct: The function only does a fixed set of calculations regardless of input size, so bigger numbers don't mean more steps.
Understanding that AMM swap calculations run in constant time helps you explain how decentralized exchanges handle many trades efficiently.
"What if the swap function had to loop through a list of liquidity pools to find the best rate? How would the time complexity change?"
Practice
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]
- Confusing AMM with mining or token creation
- Thinking AMM stores passwords
- Assuming AMM creates tokens automatically
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]
- Using addition or subtraction instead of multiplication
- Confusing division with the formula
- Mixing up variables and constants
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]
- Adding instead of dividing to find new y
- Using old y value without adjustment
- Forgetting to add tokens to x before calculation
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_ySolution
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]
- Ignoring swap fees in calculations
- Confusing return values
- Using integer division in Python 3 (which is float by default)
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]
- Adding fee instead of subtracting
- Multiplying input by fee only
- Dividing input by (1 - fee) incorrectly
