Challenge - 5 Problems
Liquidity Pool Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediateCalculate 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))
Attempts:
2 left
๐ก Hint
Calculate LP tokens minted based on the smaller proportional deposit relative to the pool.
โ Incorrect
The LP tokens minted are proportional to the smallest ratio of deposited tokens to pool tokens multiplied by total LP tokens. Here, 100/1000 = 0.1 and 200/2000 = 0.1, so LP minted = 0.1 * 100 = 10.
๐ง Conceptual
intermediateUnderstanding impermanent loss
What is impermanent loss in the context of liquidity pools?
Attempts:
2 left
๐ก Hint
Think about how price changes affect liquidity providers compared to just holding tokens.
โ Incorrect
Impermanent loss happens when the price of tokens in the pool changes compared to when they were deposited, causing the liquidity provider to have less value than if they just held the tokens outside the pool.
๐ง Debug
advancedFix 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))
Attempts:
2 left
๐ก Hint
Check if the original code actually raises an error or if the problem is elsewhere.
โ Incorrect
The original code is correct and runs without error, printing 10. The correct choice is option A, which matches the original. Option A uses max instead of min (wrong), C uses integer division (gives 0), D returns float instead of int.
๐ Syntax
advancedIdentify 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
Attempts:
2 left
๐ก Hint
Look carefully at the assignment operators used.
โ Incorrect
Option A uses '=+' which is not a valid operator in Python. It should be '+=' to add and assign. The other options (B, C, D) use valid syntax.
๐ Application
expertCalculate 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))
Attempts:
2 left
๐ก Hint
Remember to apply the fee before calculating the new pool balance and use the constant product formula.
โ Incorrect
After applying the 0.3% fee, input_X_after_fee = 50 * 0.997 = 49.85. New pool X = 500 + 49.85 = 549.85. k = 500 * 1000 = 500000. New pool Y = 500000 / 549.85 โ 909.12. Output Y = 1000 - 909.12 โ 90.88, rounded down to 90.
