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