Weighted Math is designed to allow for swaps between any assets whether or not they have any price correlation. Prices are determined by the pool balances, pool weights, and amounts of the tokens that are being swapped.
Balancer's Weighted Math equation is a generalization of the
For more formulas and derivations of the below formulas, please refer to the Balancer Whitepaper.
Developers can use the TypeScript math implementations used by the Smart Order router
There are also Python implementations in progress
The value function $$V$$is defined as:
Where
-
$$t$$ ranges over the tokens in the pool -
$$B_t$$ is the balance of the token in the pool -
$$W_t$$ is the normalized weight of the tokens, such that the sum of all normalized weights is 1.
Each pair of tokens in a pool has a spot price defined entirely by the weights and balances of just that pair of tokens. The spot price between any two tokens,$$SpotPrice^o_i$$, or in short
-
$$B_i$$ is the balance of token$$i$$ , the token being sold by the trader which is going into the pool -
$$B_o$$ is the balance of token$$o$$ , the token being bought by the trader which is going out of the pool -
$$W_i$$ is the weight of token$$i$$ -
$$W_o$$ is the weight of token$$o$$
When we consider swap fees, we do exactly the same calculations as without fees, but using
When a user sends tokens
{% hint style="info" %}
If you're computing this value yourself, remember that the pool collects swap fees as a percentage of the input token. In the equation above,$$A_i$$ is the amount that the pool actually swaps into the output token, not the amount sent by a trader,
It is also very useful for traders to know how much they need to send of the input token