Weight Math
Last updated
Last updated
The Weighted Math is designed to allow for swaps between any assets, with or without price correlation. The price is determined by the pool balance, pool weight and the amount of tokens to be swapped. The Gaming DEX Weighted Math formula is a generalization of the constant product formula x * y = k recommended by Automated Market Makers (AMM) in . This generalization also considers weighted cases that are not evenly split 50/50 (n ≥ 2).
Traders and arbitrageurs balance the pool by performing swaps as the price of each token changes. This keeps the desired value ratio of each token while collecting transaction fees from traders.
Calculation Formula
The value function V is defined as:
t : ranges over the tokens in the pool Bt : is the balance of the token in the pool Wt : is the normalized weight of the tokens, such that the sum of all normalized weights is 1.
The spot price of each pair of tokens in the pool is determined solely by the weight and balance of the tokens in the pair. The spot price between two tokens () is a weight-normalized ratio of the token balances.
Bi : is the balance of token i ,the token being sold by the trader which is going into the pool Bo : is the balance of token o, the token being bought by the trader which is going out of the pool Wi : is the weight of token i Wo : is the weight of token o
When the swap fee is considered, the calculation is identical to that without the fee, but the fee is subtracted from the input amount using (1- swapFee).The formula is as follows:
All other token balances remain unchanged when the user sends tokeni to obtain tokeno. Thus, if the quantities of tokens I and o exchanged are defined as Ai and Ao, we can calculate the quantity Ao obtained when the user sends Ai, because the value function V must be constant before and after the transaction.
*If you calculate this value yourself, keep in mind that the Swap fee is calculated as a percentage of the input tokens.In the above equation, Ai is the amount Swapped into output tokens by the pool, not the amount sent by the trader, Asent . To calculate the throughput, the following must be calculated:
It is also very useful for traders to understand how much input token AI they must send in order to receive the desired amount of output token AO.