5/5
### When is Tweak Price Called? Here, we explain the relationship between external functions, the internal function `tweak_price`, and state variables `virtual_price` and `D`. The user has the option to call functions: `exchange`, `add_liquidity`, `remove_liquidity`, and `remove_liquidity_one_coin`. In all cases except for the function call `remove_liquidity`, the internal function `tweak_price` is called. The `tweak_price` function, as the name suggests, is called when the Curve V2 AMM may need to change its price and re-peg. The price changes when the `exchange`, `add_liquidity`, or `remove_liquidity_one_coin` functions are called. Because we can add single-sided liquidity, when the `add_liquidity` function is called, it may change the internal price of a token. The `remove_liquidity_one_coin` function changes the ratio of token balances, which may change the internal price. The `tweak_price` function is called when `exchange`, `add_liquidity`, or `remove_liquidity_one_coin` are called. However, when `remove_liquidity` is called, it doesn't change the price because all the tokens are withdrawn so the internal price remains the same. When the function `tweak_price` is called, a decision must be made whether to re-peg or keep the peg the same. When re-pegging, this will affect the `virtual_price` and the variable `D`. First, it will affect the value of `D`. When we re-peg, we're trying to find this new value for `D`, the new center of liquidity. The `virtual_price` is calculated based on this value of `D`. So, when the value of `D` changes, the `virtual_price` is affected as well. Another factor that will affect the value of `D` is when the functions `exchange`, `add_liquidity`, or `remove_liquidity_one_coin` are called to collect some fees. A swap fee is collected when the function `exchange` is called, and an imbalance fee is collected when either `add_liquidity` or `remove_liquidity_one_coin` is called. When fees are collected, this will increase liquidity. So, this value of `D` will also increase, which will affect the `virtual_price`.
Here, we explain the relationship between external functions, the internal function tweak_price
, and state variables virtual_price
and D
.
The user has the option to call functions: exchange
, add_liquidity
, remove_liquidity
, and remove_liquidity_one_coin
.
In all cases except for the function call remove_liquidity
, the internal function tweak_price
is called.
The tweak_price
function, as the name suggests, is called when the Curve V2 AMM may need to change its price and re-peg. The price changes when the exchange
, add_liquidity
, or remove_liquidity_one_coin
functions are called.
Because we can add single-sided liquidity, when the add_liquidity
function is called, it may change the internal price of a token.
The remove_liquidity_one_coin
function changes the ratio of token balances, which may change the internal price.
The tweak_price
function is called when exchange
, add_liquidity
, or remove_liquidity_one_coin
are called.
However, when remove_liquidity
is called, it doesn't change the price because all the tokens are withdrawn so the internal price remains the same.
When the function tweak_price
is called, a decision must be made whether to re-peg or keep the peg the same. When re-pegging, this will affect the virtual_price
and the variable D
. First, it will affect the value of D
. When we re-peg, we're trying to find this new value for D
, the new center of liquidity. The virtual_price
is calculated based on this value of D
. So, when the value of D
changes, the virtual_price
is affected as well.
Another factor that will affect the value of D
is when the functions exchange
, add_liquidity
, or remove_liquidity_one_coin
are called to collect some fees.
A swap fee is collected when the function exchange
is called, and an imbalance fee is collected when either add_liquidity
or remove_liquidity_one_coin
is called.
When fees are collected, this will increase liquidity. So, this value of D
will also increase, which will affect the virtual_price
.
Learn when Curve V2’s tweak_price is triggered and how it impacts re-pegging, virtual price, and the D invariant.
Previous lesson
Previous
Next lesson
Next
Give us feedback
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