Weighted AMM
In previous sections, we introduced the fundamental concepts of pools and the vault for pool interaction. This section delves into a unique type of pool, known as a weighted pool, housed within the amm module.
Weighted Math
Weighted pools utilize a constant product equation:
Where and denote the balance and weight of the th token in the pool, respectively.
Our implementations incorporate a notion termed virtual balances. This involves augmenting a token's actual balance with a virtual number for specific use-cases. A typical instance is when a new token is introduced into a pool, we generate a virtual balance for the token to regulate its price. The specifics of each use-case will be detailed in the relevant documentation. For now, we'll include virtual balances in the equation as follows:
Here, represents the virtualized balance of token , encompassing both the token's actual balance and virtual amounts.
Pool Interactions
This section outlines the equations used in pool interactions, such as trades, spot prices, liquidity deposit, and withdrawal.
Swaps
Let denote the trading fee. Our pool trading formulas, similar to Balancer's, account for virtual balances. Specifically, if the real balances in the pool prior to a trade are , the virtual balances are , and the weights are , the quantity of token received by the trader when depositing an amount of token is
The amount of token required to deposit to obtain an amount of token is
If a trader deposits an amount of token and receives an amount of token , and denote the real balances in the pool post-trade, then
as expected.
Note that we must ensure to execute the trade, unless the token is being removed from the pool. In this case, we also accept and subsequently remove the token from the pool.
Spot Prices
As alluded to earlier, virtual balances are used in lieu of real balances when trading pool assets. Therefore, for all , the spot price of in terms of is
LP token spot price: To calculate the LP token's price of the pool in terms of , we first determine the pool's total value in this token as follows:
Considering the quantity of LP tokens in circulation as , the LP token's spot price in terms of the token becomes
Liquidity Deposit
Proportional all-asset deposits: These deposits follow the standard procedure, where a user deposits amounts of each asset in the pool, proportional to the pool's current balances. Specifically, if are the balances in the pool, a user must deposit amounts of each asset that satisfy
Thus, the user will receive LP tokens, where is the number of LP tokens in circulation. If we consider as a scalar in the virtual balance designs, all virtual balances scale by . It's straightforward to see that the spot prices remain unaltered after this operation.
Single-asset deposits given LP amount: Here, the user aims to deposit a single asset (token ) and receive a certain LP token amount. We need to determine the token amount the user must deposit.
Suppose is the LP token amount the user wishes to receive. Let be the real balances, and are the virtual balances of the pool. Let be the pool fee. To facilitate the single-asset deposit of token , we contemplate several trades of token against other tokens, followed by a proportional all-asset deposit.
Let . Note, is the pool share corresponding to the LP tokens the user wishes to obtain. For , let be the amount the user acquires from the trades and will be used in the proportional all-asset deposit. Since, for each , the pool's real balance of token after trades is , and the user wishes to receive LP tokens which correspond to a pool fraction, we deduce that
Hence,
for each . Let denote the quantity of token a user must trade in the pool to acquire quantities of token , for each , excluding transaction fees. This relationship is represented by the following equation:
From this, we can derive:
When fees are accounted for, the user must trade an amount of token , where . The post-trade balance of token becomes . If a proportional all-asset deposit equal to a pool share is performed after the trades, the user must provide of token . Consequently, the total deposit required from the user is to obtain LP tokens through a single-asset deposit of token .
Non-proportional multi-asset deposits: In this scenario, the user intends to deposit varying amounts of one or more pool assets, and we must calculate the amount of LP tokens to be issued.
Suppose the real balances of the pool are and the virtual balances are . The user's deposit amounts are . We will assume a feeless pool for this discussion.
To undertake a proportional all-asset deposit, the user would need to exchange appropriate amounts of some assets to acquire more of others so that the user's post-trade asset amounts are proportional to the pool's real balances. Subsequently, the user can make a proportional all-asset deposit with their assets. Let represent the user's LP token share for the deposit; hence, the user will receive LP tokens.
We will determine the value of , allowing us to perform a single transaction instead of several trades followed by a proportional all-asset deposit. Note that the pool's real balances post-trade and following the all-asset deposit will be , as the user takes some assets from the pool only to deposit them back shortly after. Consider as the virtual balances of the pool post-deposit. Given that the outcome of Balancer's weighted geometric mean formula remains constant with trades and increases in proportion to all-asset deposits, we get:
Let's denote the value of the parameter after the deposit as . For all , there is a such that:
This gives us:
Our goal is to find that satisfies:
or equivalently:
To find the value of , we apply Newton's method. Define as:
For each , let
Hence,
Observe:
We apply Newton's method with:
and define:
Simulations indicate that 10 iterations of Newton's method suffice in most cases. We've established that this operation entails a swap step, which necessitates a swap fee. To streamline swap fee calculations, we apply the fee to the LP amount using the pool's swap fee ratio. As these estimations hinge on the ratio of assets used in swaps versus those used in proportional join, we first execute the largest possible proportional join. The remaining amounts are then calculated using the equations, and the swap fee is only applied to this portion.
Liquidity Withdrawals
Proportional all-asset withdrawals: These are conducted in the usual manner, where users redeeming a certain quantity of LP tokens receive proportional amounts of all pool assets, corresponding to their pool share. Specifically, if a user redeems an amount of LP tokens and the pool's real balances are , the user will receive, for each , an amount equal to of asset of the pool. Here, represents the number of LP tokens in circulation.
As with the all-asset deposit case, we'll now demonstrate that spot prices remain unchanged after an all-asset liquidity withdrawal. Let's assume are the pool's real balances post-withdrawal, are the virtual balances post-withdrawal, and represents the value of parameter post-withdrawal. Let . Note that for all ,
and
If we use parameter as a scaler in the virtual balances, they will change proportionally to the real balances and parameter , implying that spot prices remain unchanged after an all-asset liquidity withdrawal.
Single-asset withdrawals given LP amount: Here, the user redeems a specific quantity of LP tokens and wishes to receive only token , instead of a proportional amount of all pool assets. We aim to calculate the quantity of token the user will receive.
Let represent the quantity of LP tokens the user wishes to redeem. Let