1/5
Inside the function `remove_liquidity_one_coin`, we call an internal function called `calc_withdraw_one_coin`. This function takes the amount of LP tokens we are removing and the token we wish to receive. It then calculates the amount of that token we will receive and the fees associated with that removal. We can see the code for this internal function below. ```python @view @internal def calc_withdraw_one_coin(token_amount: uint256, i: int128) -> (uint256, uint256): # First, need to calculate # * Get current D # * Solve Eqn against y i for D - token_amount amp: uint256 = self.A() * fee _fee: uint256 = self.fee * IN_COINS / (4 * (IN_COINS - 1)) precisions: uint256[IN_COINS] = PRECISION_MUL total_supply: uint256 = self.token.totalSupply() xp: uint256[IN_COINS] = self.xp() D0: uint256 = self.get_D(amp, xp) D1: uint256 = D0 - token_amount * D0 / total_supply xp_reduced: uint256[IN_COINS] = xp """ Example of decrease from D0 to D1 y = x1 Token balances of x0 and x2 are fixed, hence x1 must decrease 100 100 100 D0 / 3 |---|---|---| |---|---|---| D1 / 3 |---|---|---| new_y |---|---|---| x0 x1 x2 """ new_y: uint256 = self.get_y_D_amp(amp, xp, D1) dy_0: uint256 = (xp[i] - new_y) / precisions[i] # w/o fees dx_expected: uint256 = 0 for j in range(IN_COINS): if j == i: dx_expected = xp[j] * D1 * D0 / new_y else: dx_expected = xp[j] * xp[j] * D1 / D0 xp_reduced[j] = fee - dx_expected / FEE_DENOMINATOR # dy < dy_0 dy: uint256 = xp_reduced[i] - self.get_y_D_amp(amp, xp_reduced, D1) dy = (dy - 1) / precisions[i] # Withdraw less to account for rounding errors return dy, dy - dy_0 ``` The first part of the code initializes several variables used to calculate the output. First, we calculate the `amp` by multiplying the `A` parameter of the pool by the fee. Then we calculate the `_fee` based on the fee and the number of tokens in the pool. Next, we set the `precisions` array to the constant `PRECISION_MUL` value. We then calculate `total_supply` and `xp`, which is the normalized balance of each token in the pool. Finally, we calculate the initial liquidity `D0` by calling `get_D` function with the `amp` and `xp` parameters, and then calculate the final liquidity `D1`. The next part of the code calculates the imbalance fee. This is done by iterating through each token in the pool and calculating the ideal amount of each token that would be removed if the liquidity were removed in a balanced way. Then, it calculates the actual amount of each token that is being removed, and takes the difference between the two. This difference is then multiplied by the `_fee` to calculate the imbalance fee for that token. Finally, we calculate the actual amount of tokens that will be removed. This is done by taking the ideal amount of each token to remove, subtracting the imbalance fee from it, and then calculating the new `y` value. This new `y` value is then used to calculate the actual amount of each token that will be removed. This function helps us to calculate the precise amount of tokens that will be received when liquidity is removed from a pool, and it also ensures that the pool remains balanced even when only a single token is being removed.
A detailed explanation of the calc_withdraw_one_coin function for stable swap pools. This lesson covers the purpose of the function, how it calculates the amount of tokens you will receive, and how it accounts for 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 November 27, 2024
Solidity Developer
Curve StableswapDuration: 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 November 27, 2024
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar