Gaming DEX
  • Gaming DEX
    • Gaming DEX Overview
  • PRODUCT
    • Pools
      • Weight Pools
      • Stable Pool
      • Extra Rewards
    • The Vault
    • Smart Order Router (SOR)
    • Anti-Trader Field (AT-Field)
    • MultiGemSwap
  • LOGIC
    • Weight Math
    • Stable Math
    • Service Fee
  • BRIDGE
    • Bridge Specification
  • TOKEN
    • Z Token
    • Gauge
  • CONTRACTS
    • Oasys Hub
    • DeFi Verse
  • OTHRES
    • Roadmap
    • FAQ
    • Disclaimer
    • Revision History
Powered by GitBook
On this page
  1. LOGIC

Weight Math

PreviousLOGICNextStable Math

Last updated 6 months ago

Overview

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

Invariant

The value function V is defined as:

V=∏tBtWtV=\prod_tB^{Wt}_tV=t∏​BtWt​

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.

Spot Price

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.

SPio=BiWiBoWoSP^o_i=\vcenter{ \cfrac{B_i}{\cfrac{W_i}{\cfrac{B_o}{W_o}}}}SPio​=Wo​Bo​​Wi​​Bi​​​

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

Spot Price With Swap Fees

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:

SPio=BiWiBoWo・11−swapFeeSP^o_i=\vcenter{ \cfrac{B_i}{\cfrac{W_i}{\cfrac{B_o}{W_o}}}}・\cfrac{1}{1-swapFee}SPio​=Wo​Bo​​Wi​​Bi​​​・1−swapFee1​

outGivenIn

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.

Ao=Bo・(1−(BiBi+Ai)WiWo)A_o=B_o・(1-(\cfrac{B_i}{B_i + A_i})^{\frac{W_i}{W_o}})Ao​=Bo​・(1−(Bi​+Ai​Bi​​)Wo​Wi​​)

*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:

Ai=Asent・(1−swapFee)A_i=A_{sent}・(1-swapFee)Ai​=Asent​・(1−swapFee)

inGivenOut

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.

Ai=Bi・(1−(BoBo+Ao)WoWi−1)A_i=B_i・(1-(\cfrac{Bo}{B_o + A_o})^{\frac{W_o}{W_i}}-1)Ai​=Bi​・(1−(Bo​+Ao​Bo​)Wi​Wo​​−1)

Vitalik Buterin's post