## Code Outline of the Exchange Function In this lesson, we'll outline the steps of the `exchange` function. First, the function calculates the `A` and `gamma` parameters. Then, it copies token balances from the state variable `balances` into an array in memory called `xp`. The function then updates the `xp` array for the token in, and the state variable `balance` for the token in. Next, the code unpacks the `price_scale`. The `price_scale` is stored in a single `uint256`. After unpacking the `price_scale`, the code calculates the transformed normalized balances into the `xp` array using the following calculation: ```javascript xp[i] = price_scale[i] * xp[i] * (precisions[i] / 10**18) ``` This calculation takes the `price_scale` element and multiplies it by the corresponding current value of `xp` element and then by a `precisions` value, which normalizes token balances to have 18 decimals. Because `price_scale` has 18 decimals of precision, we cancel out the `10**18` by dividing by `10**18`. The result is that each element of `xp` will hold a transformed balance with 18 decimals. Next, the code checks if A and gamma are updating. If they are, a value D will be calculated and updated. This logic handles cases when the curve of the b-two amm is changing its shape. Then, using the current parameters, the code calculates the value D, which determines the shape of the curve at this moment. Following this, the code calculates the token out balance. Taking the difference between the new token out balance and the previous token out balance, the code will determine the amount of the token to send out. Once the amount of token out is known, the swap fee can be calculated. Then, the token out balance is updated, the token in is transferred, the token out is transferred, and an internal function called `tweak_price` is called. The `tweak_price` function is called if the difference between the current price and the center of liquidity is significant enough. Curve b2 will then decide to re-peg if it has made enough profit. This completes the code outline for the `exchange` function.
In this lesson, we'll outline the steps of the exchange function.
First, the function calculates the A and gamma parameters. Then, it copies token balances from the state variable balances into an array in memory called xp. The function then updates the xp array for the token in, and the state variable balance for the token in.
Next, the code unpacks the price_scale. The price_scale is stored in a single uint256. After unpacking the price_scale, the code calculates the transformed normalized balances into the xp array using the following calculation:
This calculation takes the price_scale element and multiplies it by the corresponding current value of xp element and then by a precisions value, which normalizes token balances to have 18 decimals. Because price_scale has 18 decimals of precision, we cancel out the 10**18 by dividing by 10**18. The result is that each element of xp will hold a transformed balance with 18 decimals.
Next, the code checks if A and gamma are updating. If they are, a value D will be calculated and updated. This logic handles cases when the curve of the b-two amm is changing its shape. Then, using the current parameters, the code calculates the value D, which determines the shape of the curve at this moment.
Following this, the code calculates the token out balance. Taking the difference between the new token out balance and the previous token out balance, the code will determine the amount of the token to send out.
Once the amount of token out is known, the swap fee can be calculated. Then, the token out balance is updated, the token in is transferred, the token out is transferred, and an internal function called tweak_price is called. The tweak_price function is called if the difference between the current price and the center of liquidity is significant enough. Curve b2 will then decide to re-peg if it has made enough profit.
This completes the code outline for the exchange function.
A comprehensive guide to Vyper smart contract development with Moccasin. The course covers the basics of setting up a Vyper development environment, deploying your first contracts, understanding the core features of Vyper, and testing your contracts using Moccasin.
Previous lesson
Previous
Next lesson
Next
Course Overview
About the course
AMM math for Curve Cryptoswap
How liquidity is concentrated
Price-repegging
How function calls interact with the AMM
Curve Cryptoswap state variables
How the function exchange works
How to swap tokens
How to add and remove liquidity
Math for Curve Cryptoswap’s internal price oracle
Implicit differentiation
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on August 11, 2025
Duration: 4min
Duration: 1h 21min
Duration: 28min
Duration: 26min
Duration: 14min
Duration: 24min
Duration: 59min
Duration: 5min
Course Overview
About the course
AMM math for Curve Cryptoswap
How liquidity is concentrated
Price-repegging
How function calls interact with the AMM
Curve Cryptoswap state variables
How the function exchange works
How to swap tokens
How to add and remove liquidity
Math for Curve Cryptoswap’s internal price oracle
Implicit differentiation
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Blockchain Financial Analyst
$100,000 - $150,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on August 11, 2025