0
0
Blockchain / Solidityprogramming~10 mins

Automated Market Makers (AMM) in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Automated Market Makers (AMM)
User provides Token A and Token B
AMM Pool receives tokens
Calculate new token reserves
Apply constant product formula: x * y = k
Determine output token amount
User receives swapped tokens
Update pool reserves for next trade
The AMM takes tokens from the user, updates reserves using a formula, and returns swapped tokens, keeping the product of reserves constant.
Execution Sample
Blockchain / Solidity
reserveA = 1000
reserveB = 1000
inputA = 100
k = reserveA * reserveB
newReserveA = reserveA + inputA
newReserveB = k / newReserveA
outputB = reserveB - newReserveB
This code swaps Token A for Token B using the constant product formula in an AMM pool.
Execution Table
StepVariableValueCalculation/ActionExplanation
1reserveA1000Initial reserveStarting Token A in pool
2reserveB1000Initial reserveStarting Token B in pool
3inputA100User inputUser adds 100 Token A to swap
4k1000000reserveA * reserveBConstant product remains fixed
5newReserveA1100reserveA + inputAPool now has 1100 Token A
6newReserveB909.09k / newReserveACalculate new Token B reserve
7outputB90.91reserveB - newReserveBUser receives ~90.91 Token B
8---Swap complete, reserves updated
💡 Swap ends after calculating output tokens and updating reserves.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 6Final
reserveA10001000110011001100
reserveB100010001000909.09909.09
inputA-100100100100
k--100000010000001000000
newReserveA--110011001100
newReserveB---909.09909.09
outputB---90.9190.91
Key Moments - 3 Insights
Why do we multiply reserveA and reserveB to get k?
Because the AMM uses the constant product formula x * y = k to keep the pool balanced, as shown in step 4 of the execution_table.
Why does outputB equal reserveB minus newReserveB?
Because the user receives the difference in Token B reserves after the swap, as calculated in step 7 of the execution_table.
Why do we add inputA to reserveA before calculating newReserveB?
Because the pool's Token A reserve increases by the user's input before recalculating Token B reserve, shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the value of newReserveB?
A909.09
B1000
C1100
D90.91
💡 Hint
Check the 'Value' column at step 6 in the execution_table.
At which step does the pool calculate the constant product k?
AStep 3
BStep 5
CStep 4
DStep 7
💡 Hint
Look for the step where k = reserveA * reserveB is calculated in the execution_table.
If the user inputs 200 Token A instead of 100, which variable changes first in the variable_tracker?
AreserveB
BinputA
CnewReserveB
DoutputB
💡 Hint
Look at the variable_tracker to see which variable records the user input first.
Concept Snapshot
Automated Market Makers (AMM) use a formula x * y = k to keep token reserves balanced.
Users swap tokens by adding one token to the pool.
The pool calculates the output token amount by maintaining k constant.
Reserves update after each swap to reflect new balances.
This allows decentralized token trading without order books.
Full Transcript
Automated Market Makers (AMM) let users swap tokens by interacting with a pool of two tokens. The pool keeps the product of the token reserves constant, called k. When a user adds some amount of Token A, the pool calculates how much Token B to give back so that reserveA times reserveB stays equal to k. This process updates the reserves and completes the swap. The key steps are: starting reserves, user input, calculating k, updating reserves, and determining output tokens. This method allows smooth token swaps without needing buyers and sellers to match directly.