Hexagon Docs
Search…
⌃K

Weighted Math

Overview

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.
Hexagon's Weighted Math equation is a generalization of the
xy=kx*y=k
constant product formula recommended for Automated Market Makers (AMMs) in a post by Vitalik Buterin. Hexagon's generalization accounts for cases with
n2n \geq2
tokens as well as weightings that are not an even 50/50 split.
As the price of each token changes, traders and arbitrageurs rebalance the pool by making swaps. This maintains the desired weighting of the value held by each token whilst collecting trading fees from the traders.

Invariant

The value function
VV
is defined as:
V=tBtWtV= \prod_t B_t^{W_t}
Where
  • tt
    ranges over the tokens in the pool
  • BtB_t
    is the balance of the token in the pool
  • WtW_t
    ​is the normalized weight of the tokens, such that the sum of all normalized weights is 1.

Spot Price

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,
SpotPriceioSpotPrice^o_i
, or in short
SPioSP^o_i
, is the the ratio of the token balances normalized by their weights:
SPio=BiWiBoWoSP^o_i = \frac{\frac{B_i}{W_i}}{\frac{B_o}{W_o}}
  • BiB_i
    is the balance of token
    ii
    , the token being sold by the trader which is going into the pool
  • BoB_o
    is the balance of token
    oo
    , the token being bought by the trader which is going out of the pool
  • WiW_i
    is the weight of token
    ii
  • WoW_o
    is the weight of token
    oo

Spot Price with Swap Fees

When we consider swap fees, we do exactly the same calculations as without fees, but using
Ai(1swapFee)A_i \cdot (1-swapFee)
instead of
AiA_i
since fees are taken out of the input amount. The equation then becomes:
SPio=BiWiBoWo11swapFeeSP^o_i = \frac{\frac{B_i}{W_i}}{\frac{B_o}{W_o}} \cdot \frac{1}{1-swapFee}
For more formulas and derivations of the below formulas, please refer to the Balancer Whitepaper.