4/5
## Introduction to the exchange function We're going to take a walk-through of the *exchange* function in the *StableSwap* pool. ### Exchange Function Walk-through The *exchange* function has the following steps: 1. **Normalizes token balances (memory)** This is the first thing the function does. It makes sure all token balances have 18 decimals, regardless of the actual token's decimal places. 2. **Transfer token in (memory)** The *exchange* function will then transfer the token *in* to the pool. 3. **Update token in balance (memory)** The normalized balance of the token *in* is stored in memory. 4. **Calculate token out balance** * **Calculate A** * **Calculate D** The function will calculate the new balance of the token *out*. 5. **Calculate token out** The difference between the new token *out* balance and the old token *out* balance is calculated. 6. **Calculate swap fee** The *exchange* function calculates the swap fee which is taken out of the token *out*. 7. **Update token balances (storage)** This step updates the token balances in storage to reflect the changes. 8. **Transfer token out** Finally, the *exchange* function transfers the token *out* to the user. This walk-through provides a basic understanding of the *exchange* function.
We're going to take a walk-through of the exchange function in the StableSwap pool.
The exchange function has the following steps:
Normalizes token balances (memory)
This is the first thing the function does. It makes sure all token balances have 18 decimals, regardless of the actual token's decimal places.
Transfer token in (memory)
The exchange function will then transfer the token in to the pool.
Update token in balance (memory)
The normalized balance of the token in is stored in memory.
Calculate token out balance
Calculate A
Calculate D
The function will calculate the new balance of the token out.
Calculate token out
The difference between the new token out balance and the old token out balance is calculated.
Calculate swap fee
The exchange function calculates the swap fee which is taken out of the token out.
Update token balances (storage)
This step updates the token balances in storage to reflect the changes.
Transfer token out
Finally, the exchange function transfers the token out to the user.
This walk-through provides a basic understanding of the exchange function.
A comprehensive guide to understanding the steps involved in the 'exchange' function of StableSwap. This lesson outlines the algorithm for the function, including token balance normalization, token transfers, and the calculation of swap fees.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
AMM math for Curve Stableswap
How to calculate swap amount and liquidity
Curve Stableswap contracts
How to implement a swap function
How to implement the add and remove liquidity functions
How to quantify liquidity pools
How to control the flatness of the curve
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Last updated on June 6, 2025
Duration: 14min
Duration: 32min
Duration: 26min
Duration: 23min
Duration: 20min
Duration: 23min
Course Overview
About the course
AMM math for Curve Stableswap
How to calculate swap amount and liquidity
Curve Stableswap contracts
How to implement a swap function
How to implement the add and remove liquidity functions
How to quantify liquidity pools
How to control the flatness of the curve
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Last updated on June 6, 2025