Path: balancer_math.py file discussed in this article.
This file uses balancer_constants.py as an imported file.
Important notice: all swaps in Balancer pools are occurring between two tokens. It means, that if the pool has 8 tokens, all trades are executing between different trading pairs (token1 vs token2, token1 vs token3, etc).
When the user sends tokens to the pool for swapping them to other tokens the "i" label refers to "tokens in", or to tokens that were "sold" in this swap. The "o" refers to "tokens out", which means that these tokens were "bought" by the user.
The Balancer AMM math from Whitepaper doesn't contain a swap fee/exit fee, but Solidity and Python implementations contain it. The fee is set to zero in the constants (as in the smart contracts)
All methods except spot price return the class BalancerMathResult
@dataclass
class BalancerMathResult:
# The relevant result of the operation
result: Decimal
# Amount of tokens the pool keeps in the token going into the pool (for join and joinswaps) or out the pool (exits)
fee: Decimal
calc_sport_price
This function returns the spot price based on the ratio of token balances in the pool taking into account their weights:
SPio=WiB0⋅BiWo⋅1−swapfee1
def calc_spot_price(
token_balance_in: Decimal,
token_weight_in: Decimal,
token_balance_out: Decimal,
token_weight_out: Decimal,
swap_fee: Decimal):
numer = token_balance_in / token_weight_in
denom = token_balance_out / token_weight_out
ratio = numer / denom
scale = 1 / (1 - swap_fee)
return ratio * scale
Note, that spot price is the "price without slippage" since the amount of asset for trading is assumed to be very low (lim -> 0).
calc_out_given_in
This function is the function for computing the result of swapping assets.
A - amount of tokens (in and out)
B - token balance in the pool (for "token in" and "token out")
W - weights of these tokens inside the pool (for "token in" and "token out")
This function returns the number of pool shares (or BPT tokens) issued as a result of single-side liquidity provision (join a pool with a specified number of "token in" asset).
Bt is a pool balance, and At is the amount of this token deposited to the pool.
@staticmethod
def calc_pool_out_given_single_in(
token_balance_in: Decimal,
token_weight_in: Decimal,
pool_supply: Decimal,
total_weight: Decimal,
token_amount_in: Decimal,
swap_fee: Decimal):
# Charge the trading fee for the proportion of tokenAi
# which is implicitly traded to the other pool tokens.
# That proportion is (1- weightTokenIn)
# tokenAiAfterFee = tAi * (1 - (1-weightTi) * poolFee)
normalized_weight = token_weight_in / total_weight
zaz = (BONE - normalized_weight) * swap_fee
token_amount_in_after_fee = token_amount_in * (BONE - zaz)
new_token_balance_in = token_balance_in + token_amount_in_after_fee
token_in_ratio = new_token_balance_in / token_balance_in
# new_pool_supply = (ratio_ti ^ weight_ti) * pool_supply
pool_ratio = pow(token_in_ratio, normalized_weight)
new_pool_supply = pool_ratio * pool_supply
pool_amount_out = new_pool_supply - pool_supply
return BalancerMathResult(pool_amount_out, token_amount_in - token_amount_in_after_fee)
calc_single_in_given_pool_out
This is another single-side liquidity provision function. It returns the number of At (token in) that is necessary to supply for minting a specified number of Pool Shares.