5/5
## Imbalance Fee The imbalance fee is a fee that is charged when a user adds or removes liquidity from a pool in a way that changes the ratio of the tokens in the pool. This fee is designed to incentivize users to maintain a balanced pool, which helps to ensure that the pool is always liquid and that the price of the tokens is stable. To understand how the imbalance fee is calculated, we can walk through a visual example. Let's assume that we have a StableSwap pool with the following token balances: * DAI: 100 * USDC: 90 * USDT: 110 We can calculate the liquidity of the pool (D0) as follows: ``` D0 = calc_D([100, 90, 110], [1, 1, 1], [0, 0, 0], A, [0, 0, 0]) ``` D0 in this case is 299. To determine the ideal balances for each token if the pool were perfectly balanced, we can divide D0 by 3: * DAI: 299/3 = 100 * USDC: 299/3 = 100 * USDT: 299/3 = 100 Now, let's say that a user adds 300 DAI to the pool. This will increase the DAI balance to 400. To calculate the new ideal balances, we need to first calculate the new liquidity (D1): ``` D1 = calc_D([400, 90, 110], [1, 1, 1], [0, 0, 0], A, [0, 0, 0]) ``` D1 in this case is 581. The ideal balances for each token if the pool were perfectly balanced with the new liquidity are: * DAI: 581/3 = 194 * USDC: 581/3 = 194 * USDT: 581/3 = 194 The imbalance fee is calculated by taking the difference between the actual token balances and the ideal token balances, and then multiplying that difference by the fee multiplier. For example, the imbalance fee for DAI would be calculated as follows: * Difference: 400 - 194 = 206 * Imbalance Fee: 206 * fee = 2.06 The fee multiplier in this example is 1%, so the imbalance fee for DAI would be 2.06 DAI. We would perform the same calculation for USDC and USDT: * USDC: (90 - 194) * fee = 8.4 * USDT: (110 - 194) * fee = 8.1 The actual token balances after the imbalance fee is deducted would be: * DAI: 400 - 2.06 = 397.94 * USDC: 90 - 0.84 = 89.16 * USDT: 110 - 1.03 = 108.97 We can use Python to calculate the imbalance fee for this example: ```python def calc_xD(x0, x1, x2, A, D0): # ... return # result of the calculation # Initial balances b = [100, 90, 110] # Adding 300 DAI b1 = [b[0] + 300, b[1], b[2]] # Initial liquidity D0 = calc_D(b[0], b[1], b[2], A, [0, 0, 0]) # Liquidity after adding liquidity D1 = calc_D(b1[0], b1[1], b1[2], A, D0) # Imbalance fee diffs = [D1 * b[i] / D0 for i in range(len(b))] fees = [abs(d - b[i]) * fee for i, d in enumerate(diffs)] ``` We can then plot these results on a graph to visualize the imbalance fee: ```python import matplotlib.pyplot as plt # ... plt.show() ``` To experiment with the Python script, you need to: 1. Install Jupyter Labs and Python by running these commands in your terminal: ```bash pip install jupyterlab ``` ```bash pip install python ``` 2. Navigate to the project repo and open the `curve-v1_imbalance-fee.ipynb` file in Jupyter Labs. 3. Run the script and experiment with different values for the token balances, the amount of tokens added, and the fee multiplier.
The imbalance fee is a fee that is charged when a user adds or removes liquidity from a pool in a way that changes the ratio of the tokens in the pool. This fee is designed to incentivize users to maintain a balanced pool, which helps to ensure that the pool is always liquid and that the price of the tokens is stable.
To understand how the imbalance fee is calculated, we can walk through a visual example. Let's assume that we have a StableSwap pool with the following token balances:
DAI: 100
USDC: 90
USDT: 110
We can calculate the liquidity of the pool (D0) as follows:
D0 in this case is 299.
To determine the ideal balances for each token if the pool were perfectly balanced, we can divide D0 by 3:
DAI: 299/3 = 100
USDC: 299/3 = 100
USDT: 299/3 = 100
Now, let's say that a user adds 300 DAI to the pool. This will increase the DAI balance to 400. To calculate the new ideal balances, we need to first calculate the new liquidity (D1):
D1 in this case is 581.
The ideal balances for each token if the pool were perfectly balanced with the new liquidity are:
DAI: 581/3 = 194
USDC: 581/3 = 194
USDT: 581/3 = 194
The imbalance fee is calculated by taking the difference between the actual token balances and the ideal token balances, and then multiplying that difference by the fee multiplier.
For example, the imbalance fee for DAI would be calculated as follows:
Difference: 400 - 194 = 206
Imbalance Fee: 206 * fee = 2.06
The fee multiplier in this example is 1%, so the imbalance fee for DAI would be 2.06 DAI.
We would perform the same calculation for USDC and USDT:
USDC: (90 - 194) * fee = 8.4
USDT: (110 - 194) * fee = 8.1
The actual token balances after the imbalance fee is deducted would be:
DAI: 400 - 2.06 = 397.94
USDC: 90 - 0.84 = 89.16
USDT: 110 - 1.03 = 108.97
We can use Python to calculate the imbalance fee for this example:
We can then plot these results on a graph to visualize the imbalance fee:
To experiment with the Python script, you need to:
Install Jupyter Labs and Python by running these commands in your terminal:
Navigate to the project repo and open the curve-v1_imbalance-fee.ipynb file in Jupyter Labs.
Run the script and experiment with different values for the token balances, the amount of tokens added, and the fee multiplier.
A visual explanation of the imbalance fee in Curve.fi - This lesson dives into the concept of imbalance fees in Curve, illustrating how the fee is calculated in a stable swap pool, and its role in rebalancing the pool to ensure fairness.
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