0
0
Blockchain / Solidityprogramming~20 mins

Liquidity pools in Blockchain / Solidity - Practice Problems & Coding Challenges

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