Let's start by understanding a common pattern in the code for Curve v1. We can see that the functions `exchange`, `add_liquidity`, and the `remove_liquidity` functions are called on the StableSwap3Pool contract. The pattern we'll see in all the functions is that they first calculate the `A` parameter, and then they calculate the liquidity `D`. Let's examine the `exchange` function. We can see that it first normalizes the token balances, transfers the token in, updates the token balance, and then calculates the token out balance. After that, it calculates the `A` parameter, the liquidity `D`, calculates the token out, calculates the swap fee, updates the token balances, and then transfers the token out. The `add_liquidity` function has a similar pattern. It calculates an imbalance fee multiplier, calculates `A`, calculates the liquidity `D0`, transfers the tokens in, updates the token balances, calculates liquidity `D1`, calculates the imbalance fee, calculates liquidity `D2`, updates the token balances, calculates the liquidity provider shares, and then mints the liquidity provider's LP shares. The `remove_liquidity` function is a little different. It first calculates the token out amounts, updates the token balances, and then transfers the tokens out. Finally, it burns the LP shares. The `remove_liquidity_one_coin` function first calculates `dy` and the imbalance fee, calculates `A`, calculates `D`, calculates the imbalance fee, updates the token balances, burns the LP shares, and then transfers the token out. So, why do we see a pattern of calculating the `A` parameter before calculating the liquidity `D`? We can see that the `A` parameter is a fixed number, but an admin can decide to set a new `A` parameter. Over time, as the `A` parameter changes, the liquidity `D` will also change. Let's take a look at an example graph of Curve v1's equation. In this graph, we are looking at the equation for two tokens. We can see that the `x0` and `y0` values must be on this curve. This curve will adjust as tokens are swapped. For example, if token `x` goes out and token `y` comes in, the point will move up and to the left. Conversely, if token `x` comes in and token `y` goes out, the point will move down and to the right. Let's now see what happens when the `A` parameter changes. In this graph, we can see that the current token balance is represented by a blue dot. An admin could decide to update the `A` parameter to a higher number. We can see that the token balance is no longer on the curve. To get the blue dot back on the curve, we need to change the liquidity `D` so that the curve will go through the token balances. We see, then, that the code first calculates the `A` parameter and then calculates the `D` parameter. This is because the code needs to find the liquidity `D` that will satisfy the equation given the `A` parameter and the token balances.
Let's start by understanding a common pattern in the code for Curve v1. We can see that the functions exchange, add_liquidity, and the remove_liquidity functions are called on the StableSwap3Pool contract.
The pattern we'll see in all the functions is that they first calculate the A parameter, and then they calculate the liquidity D.
Let's examine the exchange function. We can see that it first normalizes the token balances, transfers the token in, updates the token balance, and then calculates the token out balance. After that, it calculates the A parameter, the liquidity D, calculates the token out, calculates the swap fee, updates the token balances, and then transfers the token out.
The add_liquidity function has a similar pattern. It calculates an imbalance fee multiplier, calculates A, calculates the liquidity D0, transfers the tokens in, updates the token balances, calculates liquidity D1, calculates the imbalance fee, calculates liquidity D2, updates the token balances, calculates the liquidity provider shares, and then mints the liquidity provider's LP shares.
The remove_liquidity function is a little different. It first calculates the token out amounts, updates the token balances, and then transfers the tokens out. Finally, it burns the LP shares.
The remove_liquidity_one_coin function first calculates dy and the imbalance fee, calculates A, calculates D, calculates the imbalance fee, updates the token balances, burns the LP shares, and then transfers the token out.
So, why do we see a pattern of calculating the A parameter before calculating the liquidity D?
We can see that the A parameter is a fixed number, but an admin can decide to set a new A parameter. Over time, as the A parameter changes, the liquidity D will also change.
Let's take a look at an example graph of Curve v1's equation. In this graph, we are looking at the equation for two tokens. We can see that the x0 and y0 values must be on this curve.
This curve will adjust as tokens are swapped. For example, if token x goes out and token y comes in, the point will move up and to the left. Conversely, if token x comes in and token y goes out, the point will move down and to the right.
Let's now see what happens when the A parameter changes. In this graph, we can see that the current token balance is represented by a blue dot. An admin could decide to update the A parameter to a higher number.
We can see that the token balance is no longer on the curve. To get the blue dot back on the curve, we need to change the liquidity D so that the curve will go through the token balances.
We see, then, that the code first calculates the A parameter and then calculates the D parameter. This is because the code needs to find the liquidity D that will satisfy the equation given the A parameter and the token balances.
A technical explanation of Curve Stableswap's A parameter and its relationship to liquidity D. The lesson explains the common pattern in Curve Stableswap functions, how an admin can change the A parameter, and how this change impacts the D parameter.
Previous lesson
Previous
Next lesson
Next
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 August 11, 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 August 11, 2025