0
0
Blockchain / Solidityprogramming~10 mins

Liquidity pools in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Liquidity pools
User deposits tokens
Tokens added to pool
Pool holds reserves
Users trade tokens via pool
Pool updates reserves
Users withdraw tokens + fees
End
Users add tokens to a pool, which holds reserves. Traders swap tokens through the pool, changing reserves. Users can withdraw their share plus fees.
Execution Sample
Blockchain / Solidity
pool = {'tokenA': 1000, 'tokenB': 1000}
user_deposit = {'tokenA': 100, 'tokenB': 100}
pool['tokenA'] += user_deposit['tokenA']
pool['tokenB'] += user_deposit['tokenB']
print(pool)
This code shows a user adding tokens to a liquidity pool, increasing the pool's reserves.
Execution Table
StepActionPool tokenAPool tokenBOutput
1Initial pool reserves10001000{'tokenA': 1000, 'tokenB': 1000}
2User deposits 100 tokenA11001000Updated tokenA reserve
3User deposits 100 tokenB11001100Updated tokenB reserve
4Print pool state11001100{'tokenA': 1100, 'tokenB': 1100}
💡 User deposit added, pool reserves updated, final pool state printed.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
pool['tokenA']1000110011001100
pool['tokenB']1000100011001100
Key Moments - 2 Insights
Why does the pool's tokenA reserve increase after the user deposit?
Because the user adds 100 tokenA to the pool, increasing the pool's tokenA reserve from 1000 to 1100 as shown in step 2 of the execution table.
Does the pool update both tokens at the same time?
No, the pool updates tokenA first (step 2), then tokenB (step 3), reflecting the separate deposit actions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the pool's tokenB reserve after step 2?
A1000
B1100
C900
D1200
💡 Hint
Check the 'Pool tokenB' column at step 2 in the execution table.
At which step does the pool's tokenA reserve become 1100?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Pool tokenA' column in the execution table.
If the user deposited 200 tokenB instead of 100, what would be the pool's tokenB reserve after step 3?
A1100
B1300
C1200
D1400
💡 Hint
Add 200 to the initial 1000 tokenB reserve shown in the variable tracker.
Concept Snapshot
Liquidity pools hold token reserves supplied by users.
Users deposit tokens, increasing pool reserves.
Traders swap tokens via the pool, changing reserves.
Users withdraw tokens plus fees earned.
Pool reserves update step-by-step with deposits and trades.
Full Transcript
Liquidity pools are smart contracts holding tokens from users. Users deposit tokens, which increases the pool's reserves. Traders swap tokens through the pool, changing the amounts held. Users can later withdraw their tokens plus fees earned from trades. In the example, a pool starts with 1000 tokens each of tokenA and tokenB. A user deposits 100 tokens of each. The pool's reserves update step-by-step, first tokenA then tokenB, ending with 1100 tokens each. This process is shown in the execution table and variable tracker. Key moments include understanding how deposits increase reserves and that tokens update separately. The visual quiz checks understanding of these steps. The snapshot summarizes the main points about liquidity pools and their token reserve updates.