Weighted Maths

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.

A360's AMM's Weighted Math equation is derived from BalancerV2's generalization of the constant product formula, accounting for cases with n>2 tokens as well as uneven weightings.

Invariant

The A360 AMM Mathemathical Invariant is defined as follows:

V=tBtWt V = \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

The Spot Price for an asset inside of the pool is defined by both the weights defined during pool creation and the token balances of the pool at a specific point in time.

SPio=BiWiBoWoSP_i^o = \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 swapper which is going into the pool

  • BoB_o is the balance of token oo, the token being bought by the swapper 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

Swap Fees must be accounted for to calculate effective Spot Price. To do so we perform the same calculations, but subtracting the swap fees. The formula looks as follows:

SPio=BiWiBoWo11swapFeeSP_i^o = \frac{\frac{B_i}{W_i}}{\frac{B_o}{W_o}} \cdot \frac{1}{1 - \text{swapFee}}

Swap Formulas

outGivenIn

In a swap equation the token balances impacted are the input and the output tokens. The formula which calculates output balance based on input balance is as follows:

Ao=Bo(1(BiBi+Ai)WiWo)A_o = B_o \cdot \left( 1 - \left( \frac{B_i}{B_i + A_i} \right)^{\frac{W_i}{W_o}} \right)

inGivenOut

To calculate the required asset input to receive a specific target output balance of the desired asset is defined below:

Ai=Bi((BoBoAo)WoWi1)A_i = B_i \cdot \left( \left( \frac{B_o}{B_o - A_o} \right)^{\frac{W_o}{W_i}} - 1 \right)

Last updated