Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Liquidity pools 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
๐ŸŽ–๏ธ
Liquidity Pool Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Calculate LP token share after deposit
Given a liquidity pool with 1000 tokens of A and 2000 tokens of B, and 100 LP tokens issued, what will be the LP token amount minted if a user deposits 100 tokens of A and 200 tokens of B?
Blockchain / Solidity
pool_A = 1000
pool_B = 2000
lp_total = 100
user_deposit_A = 100
user_deposit_B = 200

# LP tokens minted calculation
lp_minted = min(user_deposit_A * lp_total / pool_A, user_deposit_B * lp_total / pool_B)
print(int(lp_minted))
A5
B20
C15
D10
Attempts:
2 left
๐Ÿ’ก Hint
Calculate LP tokens minted based on the smaller proportional deposit relative to the pool.
๐Ÿง  Conceptual
intermediate
1:30remaining
Understanding impermanent loss
What is impermanent loss in the context of liquidity pools?
AThe permanent loss of tokens caused by smart contract bugs.
BThe temporary loss of value experienced by liquidity providers due to price changes of pooled tokens compared to holding them separately.
CThe fee charged by the exchange for swapping tokens.
DThe loss of tokens due to network transaction fees.
Attempts:
2 left
๐Ÿ’ก Hint
Think about how price changes affect liquidity providers compared to just holding tokens.
๐Ÿ”ง Debug
advanced
2:30remaining
Fix the error in LP token minting calculation
This code is intended to calculate LP tokens minted when a user deposits tokens into a liquidity pool, but it raises an error. Identify the error and select the correct fixed code.
Blockchain / Solidity
def mint_lp_tokens(pool_A, pool_B, lp_total, deposit_A, deposit_B):
    ratio_A = deposit_A / pool_A
    ratio_B = deposit_B / pool_B
    lp_minted = min(ratio_A, ratio_B) * lp_total
    return int(lp_minted)

print(mint_lp_tokens(1000, 2000, 100, 100, 200))
A
def mint_lp_tokens(pool_A, pool_B, lp_total, deposit_A, deposit_B):
    ratio_A = deposit_A / pool_A
    ratio_B = deposit_B / pool_B
    lp_minted = min(ratio_A, ratio_B) * lp_total
    return int(lp_minted)

print(mint_lp_tokens(1000, 2000, 100, 100, 200))
B
def mint_lp_tokens(pool_A, pool_B, lp_total, deposit_A, deposit_B):
    ratio_A = deposit_A / pool_A
    ratio_B = deposit_B / pool_B
    lp_minted = max(ratio_A, ratio_B) * lp_total
    return int(lp_minted)
C
def mint_lp_tokens(pool_A, pool_B, lp_total, deposit_A, deposit_B):
    ratio_A = deposit_A // pool_A
    ratio_B = deposit_B // pool_B
    lp_minted = min(ratio_A, ratio_B) * lp_total
    return int(lp_minted)
D
def mint_lp_tokens(pool_A, pool_B, lp_total, deposit_A, deposit_B):
    ratio_A = deposit_A / pool_A
    ratio_B = deposit_B / pool_B
    lp_minted = min(ratio_A, ratio_B) * lp_total
    return lp_minted

print(mint_lp_tokens(1000, 2000, 100, 100, 200))
Attempts:
2 left
๐Ÿ’ก Hint
Check if the original code actually raises an error or if the problem is elsewhere.
๐Ÿ“ Syntax
advanced
1:30remaining
Identify the syntax error in liquidity pool update code
Which option contains a syntax error when updating the liquidity pool balances after a swap?
Blockchain / Solidity
def update_pool(pool, token_in, amount_in, token_out, amount_out):
    pool[token_in] += amount_in
    pool[token_out] -= amount_out
    return pool
A
def update_pool(pool, token_in, amount_in, token_out, amount_out):
    pool[token_in] =+ amount_in
    pool[token_out] -= amount_out
    return pool
B
loop nruter    
tuo_tnuoma =- ]tuo_nekot[loop    
ni_tnuoma =+ ]ni_nekot[loop    
:)tuo_tnuoma ,tuo_nekot ,ni_tnuoma ,ni_nekot ,loop(loop_etadpu fed
C
def update_pool(pool, token_in, amount_in, token_out, amount_out):
    pool[token_in] += amount_in
    pool[token_out] -= amount_out
    return pool
D
ef update_pool(pool, token_in, amount_in, token_out, amount_out):
    pool[token_in] += amount_in
    pool[token_out] -= amount_out
    return pool
Attempts:
2 left
๐Ÿ’ก Hint
Look carefully at the assignment operators used.
๐Ÿš€ Application
expert
3:00remaining
Calculate final token amounts after a swap with fee
A liquidity pool has 500 tokens of X and 1000 tokens of Y. A user swaps 50 tokens of X for Y. The pool charges a 0.3% fee on the input amount. Using the constant product formula (x * y = k), what is the amount of Y tokens the user receives? (Round down to nearest integer)
Blockchain / Solidity
pool_X = 500
pool_Y = 1000
input_X = 50
fee_rate = 0.003

input_X_after_fee = input_X * (1 - fee_rate)
new_pool_X = pool_X + input_X_after_fee
k = pool_X * pool_Y
new_pool_Y = k / new_pool_X
output_Y = pool_Y - new_pool_Y
print(int(output_Y))
A85
B92
C90
D88
Attempts:
2 left
๐Ÿ’ก Hint
Remember to apply the fee before calculating the new pool balance and use the constant product formula.

Practice

(1/5)
1. What is the main purpose of a liquidity pool in blockchain?
easy
A. To allow users to trade tokens directly without a middleman
B. To create new tokens automatically
C. To store user passwords securely
D. To mine new blocks faster

Solution

  1. Step 1: Understand liquidity pool function

    Liquidity pools let users trade tokens directly without needing a middleman like an exchange.
  2. Step 2: Compare options to definition

    Only To allow users to trade tokens directly without a middleman describes this function correctly; others describe unrelated blockchain features.
  3. Final Answer:

    To allow users to trade tokens directly without a middleman -> Option A
  4. Quick Check:

    Liquidity pools enable direct token trading = B [OK]
Hint: Liquidity pools remove middlemen in token trading [OK]
Common Mistakes:
  • Confusing liquidity pools with token creation
  • Thinking liquidity pools mine blocks
  • Assuming liquidity pools store passwords
2. Which of the following is the correct way to represent a liquidity pool share in code?
easy
A. shares = {'user1': 100, 'user2': 50}
B. shares = ['user1', 'user2', 100, 50]
C. shares = (100, 50, 'user1', 'user2')
D. shares = 'user1:100, user2:50'

Solution

  1. Step 1: Identify data structure for mapping users to shares

    A dictionary (key-value pairs) is best to map user names to their share amounts.
  2. Step 2: Check options for dictionary syntax

    shares = {'user1': 100, 'user2': 50} uses a dictionary with user keys and numeric values, which is correct syntax and logic.
  3. Final Answer:

    shares = {'user1': 100, 'user2': 50} -> Option A
  4. Quick Check:

    Use dictionary for user-share mapping = A [OK]
Hint: Use dictionaries to map users to their shares [OK]
Common Mistakes:
  • Using lists instead of dictionaries for key-value pairs
  • Incorrect tuple ordering for mapping
  • Using string instead of structured data
3. Given this Python code simulating a liquidity pool token ratio update:
pool = {'tokenA': 1000, 'tokenB': 2000}
new_tokenA = 100
new_tokenB = 200
pool['tokenA'] += new_tokenA
pool['tokenB'] += new_tokenB
price_ratio = pool['tokenB'] / pool['tokenA']
print(round(price_ratio, 2))

What is the printed output?
medium
A. 1.90
B. 2.20
C. 2.0
D. 3.00

Solution

  1. Step 1: Calculate updated token amounts in pool

    tokenA = 1000 + 100 = 1100; tokenB = 2000 + 200 = 2200
  2. Step 2: Compute price ratio and round

    price_ratio = 2200 / 1100 = 2.0; rounded to 2 decimals is 2.0
  3. Final Answer:

    2.0 -> Option C
  4. Quick Check:

    2200 รท 1100 = 2.0 [OK]
Hint: Add tokens first, then divide for ratio [OK]
Common Mistakes:
  • Dividing before adding new tokens
  • Rounding incorrectly
  • Mixing tokenA and tokenB values
4. This code snippet tries to update a liquidity pool but has a bug:
pool = {'tokenA': 500, 'tokenB': 1000}
new_tokenA = 50
new_tokenB = 100
pool['tokenA'] =+ new_tokenA
pool['tokenB'] =+ new_tokenB
print(pool)

What is the bug?
medium
A. The dictionary keys 'tokenA' and 'tokenB' are misspelled
B. The operator '=+' is incorrect; should be '+='
C. The print statement is missing parentheses
D. The new_token variables should be strings, not integers

Solution

  1. Step 1: Identify operator usage in assignment

    The code uses '=+' which is not a valid operator; it assigns positive new_tokenA instead of adding.
  2. Step 2: Correct operator for addition assignment

    The correct operator is '+=' to add new_tokenA to pool['tokenA'] and similarly for tokenB.
  3. Final Answer:

    The operator '=+' is incorrect; should be '+=' -> Option B
  4. Quick Check:

    Use '+=' to add values in place [OK]
Hint: Use '+=' to add and assign in one step [OK]
Common Mistakes:
  • Confusing '=+' with '+=' operator
  • Assuming print needs no parentheses in Python 3
  • Thinking keys are misspelled
5. You want to write a function that calculates each user's share percentage in a liquidity pool given a dictionary of shares like {'Alice': 300, 'Bob': 700}. Which code correctly returns a new dictionary with user names and their share percentages rounded to 2 decimals?
hard
A. def calc_shares(shares): total = len(shares) return {user: amount / total for user, amount in shares.items()}
B. def calc_shares(shares): total = sum(shares.keys()) return {user: amount / total for user, amount in shares.items()}
C. def calc_shares(shares): total = sum(shares.values()) return [round(amount / total * 100, 2) for amount in shares.values()]
D. def calc_shares(shares): total = sum(shares.values()) return {user: round(amount / total * 100, 2) for user, amount in shares.items()}

Solution

  1. Step 1: Calculate total shares correctly

    Sum the values of the shares dictionary to get total tokens contributed.
  2. Step 2: Compute percentage per user and round

    Use dictionary comprehension to divide each user's amount by total, multiply by 100, and round to 2 decimals.
  3. Final Answer:

    def calc_shares(shares): total = sum(shares.values()) return {user: round(amount / total * 100, 2) for user, amount in shares.items()} -> Option D
  4. Quick Check:

    Sum values, divide each, round = correct share % [OK]
Hint: Sum values, then divide each share by total and round [OK]
Common Mistakes:
  • Summing keys instead of values
  • Returning list instead of dictionary
  • Using length instead of sum for total