We are going to talk about the value D in an AMM and how it's calculated. When we talk about a perfectly balanced pool, the amount of each token will be D/N, where N is the number of tokens. For example, if we have 50 tokens, each token will be 10 tokens. This is represented in the first line of the following code: ```javascript D = 50 ``` This is just an example, and you can change the value of D. We are using a StableSwap 3 Pool contract, where D is calculated by using Newton's method. The function, `get_D` takes the number of coins in the pool, `N_COINS`, and an amount, `amp`, which represents the amplification coefficient in the StableSwap 3 pool. Let's have a look at this code: ```javascript @internal @pure def get_D(xp: uint256[N_COINS], amp: uint256) -> uint256: # sum(x_i) S: uint256 = 0 for x in xp: S += x if S <= 0: return 0 Dprev: uint256 = 0 D: uint256 = S Ann: uint256 = amp * N_COINS for i in range(255): # D^(N+1) / (N * prod(x_i)) D_P: uint256 = D * D / (N * prod(xp[i])) for x in xp: # D * x * D / (L * N_COINS) # If division by 0, this will be bogus D_P = D_P * x * D / (x * N_COINS) Dprev = D # equivalent to D - f(D) / (df/dD)(D) D = (Ann * S + D_P * N_COINS) * D / (Ann * (N - 1) * D + (N_COINS + 1) * D_P) # Equality with the precision of 1 if D > Dprev: if D - Dprev <= 1: break else: if Dprev - D <= 1: break return D ``` The first part of this code sums up all the normalized token balances. This is performed within this `for` loop: ```javascript for x in xp: S += x ``` If this sum is less than or equal to zero, we return zero: ```javascript if S <= 0: return 0 ``` If the sum is greater than zero, we continue with the calculation. Next, the code uses Newton's method to calculate the value of D. We use this `for` loop to iterate a maximum of 255 times. ```javascript for i in range(255): ``` The code then calculates the value of D_P, which is equivalent to the expression D^(N+1) / (N * prod(x_i)), and it will use this value of D_P for the rest of the loop. ```javascript D_P: uint256 = D * D / (N * prod(xp[i])) ``` The code then iterates through the token balances to calculate a new value of D_P. ```javascript for x in xp: D_P = D_P * x * D / (x * N_COINS) ``` The new value of D is calculated based on the previous value of D, Dprev, and the new values of D_P. ```javascript D = (Ann * S + D_P * N_COINS) * D / (Ann * (N - 1) * D + (N_COINS + 1) * D_P) ``` This equation is equivalent to this equation: D - f(D) / (df/dD)(D) Finally, the code checks if the difference between the new value of D and the previous value of D is less than or equal to 1. ```javascript if D > Dprev: if D - Dprev <= 1: break else: if Dprev - D <= 1: break ``` If the difference is less than or equal to one, the code will break out of the for loop and return the current value of D, which has been calculated using Newton's method. ```javascript return D ``` If Newton's method does not converge, the code will complete the 255 iterations and return the final value of D. As you can see, the code is written in Python, and this function will return a value D that satisfies the curve B1 equation for the given parameters.
We are going to talk about the value D in an AMM and how it's calculated.
When we talk about a perfectly balanced pool, the amount of each token will be D/N, where N is the number of tokens. For example, if we have 50 tokens, each token will be 10 tokens. This is represented in the first line of the following code:
This is just an example, and you can change the value of D.
We are using a StableSwap 3 Pool contract, where D is calculated by using Newton's method.
The function, get_D takes the number of coins in the pool, N_COINS, and an amount, amp, which represents the amplification coefficient in the StableSwap 3 pool. Let's have a look at this code:
The first part of this code sums up all the normalized token balances. This is performed within this for loop:
If this sum is less than or equal to zero, we return zero:
If the sum is greater than zero, we continue with the calculation.
Next, the code uses Newton's method to calculate the value of D. We use this for loop to iterate a maximum of 255 times.
The code then calculates the value of D_P, which is equivalent to the expression D^(N+1) / (N * prod(x_i)), and it will use this value of D_P for the rest of the loop.
The code then iterates through the token balances to calculate a new value of D_P.
The new value of D is calculated based on the previous value of D, Dprev, and the new values of D_P.
This equation is equivalent to this equation: D - f(D) / (df/dD)(D)
Finally, the code checks if the difference between the new value of D and the previous value of D is less than or equal to 1.
If the difference is less than or equal to one, the code will break out of the for loop and return the current value of D, which has been calculated using Newton's method.
If Newton's method does not converge, the code will complete the 255 iterations and return the final value of D.
As you can see, the code is written in Python, and this function will return a value D that satisfies the curve B1 equation for the given parameters.
A technical explanation of the Solidity function get_D() - The function utilizes Newton’s method to calculate the value D, which represents the liquidity of an AMM pool. It sums up all the token balances, performs iterations of Newton’s method to refine the value of D, and returns the final value D.
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