KyberSwap Docs
  • Introduction to KyberSwap
  • Getting Started
    • Quickstart
      • FAQ
    • Supported Exchanges And Networks
    • Foundational Topics
      • Decentralized Finance
        • Tokens
        • Stablecoins
        • DEX/DeFi Aggregator
        • Slippage
        • Price Impact
        • Zap
        • Maximal Extractable Value (MEV)
      • Decentralized Technologies
        • Wallets
        • Dapps
        • RPC
        • Oracles
        • On-Chain vs Off-Chain Data
      • Other Valuable Resources
  • KyberSwap Solutions
    • KyberSwap Interface
      • User Guides
        • Connect Your Wallet
        • Switching Networks
        • Instantly Swap At Superior Rates
        • Swap At Your Preferred Rates
        • Cross-chain Swap
        • Add Your Favourite Tokens
        • Get Crypto With Fiat
        • Bridge Your Assets Across Multiple Chains
      • Profiles
        • Profile Creation
        • Profile Customization
        • Sync Profile Across Devices
      • Notifications
        • Notification Center
    • KyberSwap Aggregator
      • Concepts
        • Dynamic Trade Routing
      • User Guides
        • Instantly Swap At Superior Rates
      • Developer Guides
        • Execute A Swap With The Aggregator API
        • Upgrading To APIv1
      • Aggregator API Specification
        • EVM Swaps
        • Permit
      • Contracts
        • Aggregator Contract Addresses
      • DEX IDs
      • Subgraphs
      • FAQ
    • KyberSwap Zap as a Service
      • KyberSwap Zap as a Service (ZaaS) API
        • ZaaS HTTP API
        • ZaaS GRPC API
      • KyberSwap Zap Liquidity Widget
      • Zap Fee Model
      • Zap's Supported Chains/Dexes
      • Zap's Deployed Contract Addresses
      • Zap's DEX IDs
    • KyberSwap Widget
      • Developer Guides
        • Integrating The KyberSwap Widget
        • Customizing The KyberSwap Widget
      • iFrame Alternative
      • Widget/iFrame Fee
    • KyberSwap Liquidity Widget
      • Integrating The KyberSwap Liquidity Widget
    • Limit Order
      • Concepts
        • Off-Chain Relay, On-Chain Settlement
        • Gasless Cancellation
      • User Guides
        • Swap At Your Preferred Rates
        • Update Limit Orders
        • Cancel Limit Orders
      • Developer Guides
        • Create Limit Order
        • Gasless Cancel
        • Hard Cancel
        • Fill Limit Order
      • Contracts
        • Limit Order Contract Addresses
      • Limit Order API Specification
        • General APIs
        • Maker APIs
        • Taker APIs
      • FAQ
    • KyberSwap OnChain Price Service
    • Fee Schedule
  • Governance
    • KyberDAO
      • User Guides
        • Participating in KyberDAO
        • Staking
        • Voting
        • Stake KNC And Enjoy Gas Savings
      • Fees to KyberDAO
      • KyberDAO Operator MultiSig
      • Contracts
        • KyberDAO Contract Repo
        • KyberDAO Contract Addresses
      • FAQ - Others
    • KNC Token
      • KNC Tokenomics & Utility
      • Gas Refund Program
      • KNC Contract Addresses
  • Security
    • Audits
  • Reference
    • Legacy
      • KyberSwap Classic
        • Concepts
          • Programmable Pricing Curves
          • Dynamic Auto-Adjusting Fees
          • Virtual Balances
          • Protocol Fees
        • Contracts
          • Classic Contract Repo
          • Classic Contract Addresses
          • Classic Contract Farming Addresses
      • KyberSwap Elastic
        • Concepts
          • Concentrated Liquidity
          • Reinvestment Curve
          • Tick-Range Mechanism
          • Pool Process Flows
          • Anti-Sniping Mechanism
          • Tick-Based Farming
          • Elastic Zap
          • TWAP Oracle
          • Elastic APR Calculations
        • Contracts
          • Elastic Contract Repo
          • Elastic Contract Addresses
          • Elastic Farming Contract Addresses
          • Elastic Zap Contract Addresses
          • Elastic Core Contracts
          • Elastic Core Libraries
          • Elastic Periphery Core Contracts
          • Elastic Peripheral Library Contracts
          • Elastic Peripheral Base Contracts
        • Subgraphs
      • Whitepapers
      • Audits
      • KyberAI
        • KyberScore
        • Concepts
        • On-Chain Indicators
          • Number Of Trades
          • Trading Volume
          • Netflow To Whale Wallets
          • Netflow To CEX
          • Number Of Transfers
          • Volume Of Transfers
          • Number Of Holders
          • Top Holders
        • Technical Indicators
          • Live Charts
          • Support & Resistance Levels
          • Live Trades
          • Funding Rate On CEX
          • Liquidations On CEX
        • Liquidity Analysis
      • Elastic Legacy
        • Elastic Legacy Contract Repo
        • Elastic Legacy Contract Addresses
        • Elastic Legacy Farming Contract Addresses
        • Remove Elastic Legacy Liquidity
      • Protocol
        • Overview
        • Smart Contract Architecture
        • Trust and Security Model
      • Integrations
        • Getting Started
        • Use Cases
        • Integration Types
        • Smart Contracts
        • Ethers JS
        • RESTful API
        • Slippage Rate Protection
        • Price Feed Security
        • Contract Events
        • Platform Fees
      • Reserves
        • Getting Started
          • Overview
          • Why Develop On Kyber
          • Create New Reserve
          • Existing Reserves
          • Customising Existing Reserves
        • Development Guides
          • Fed Price Reserve
          • Automated Price Reserve
          • Reserves with Ganache
          • Orderbook Reserve
        • Operations
          • Listing Policies
          • Reserve IDs
          • Reserve Rebates
          • Sanity Rates
      • Addresses
        • Introduction
        • Mainnet
        • Kovan
        • Rinkeby
        • Ropsten
      • API/ABI
        • Introduction
        • RESTful API
          • RESTful API Overview
          • RESTful API
        • Core Smart Contracts
          • IKyberNetworkProxy
          • KyberNetworkProxy
          • IKyberNetwork
          • ISimpleKyberProxy
          • IKyberMatchingEngine
          • KyberMatchingEngine
          • IKyberHint
          • KyberHintHandler
          • IKyberHintHandler
          • IKyberFeeHandler
          • IKyberStaking
          • KyberStaking
          • IKyberDao
          • KyberDao
          • IKyberStorage
          • KyberStorage
          • IKyberHistory
          • KyberHistory
          • IKyberReserve
          • KyberReserve
          • ConversionRates
          • LiquidityConversionRates
          • EpochUtils
          • IEpochUtils
          • KyberFeeHandler
        • Contract ABIs
          • ABIs
        • Code Snippets
          • Token Quantity Conversion
        • Misc Contracts
          • KyberNetwork
          • ConversionRatesInterface
          • PermissionGroups
          • SanityRates
          • Withdrawable
          • OrderbookReserveInterface
          • OrderbookReserveLister
    • KyberSwap Operator MultiSig
    • Permitable Tokens
    • Third-Party Integrations
    • KyberSwap Analytics
    • KyberSwap App
    • GitHub
    • KyberSwap Analytics
    • KyberSwap Blog
    • Kyber Network Press Kit
  • Socials
    • X
    • Discord
    • Telegram
    • LinkedIn
    • Reddit
    • Instagram
    • Tik Tok
  • Support
    • KyberSwap Help Center
    • Complaints Handling Process
Powered by GitBook
On this page
  • Introduction
  • Example​

Was this helpful?

  1. Reference
  2. Legacy
  3. KyberSwap Classic
  4. Concepts

Virtual Balances

Introduction

To guarantee the safety of the pool, there are some conditions which need to be fulfilled when adding liquidity to KyberSwap Classic pools:

  1. After LP contributions, the token price remains unchanged.

  2. PminP_{min}Pmin​ and PmaxP_{max}Pmax​ are also unchanged after LP contributions.

In KyberSwap, the pool for pair X-Y needs to maintain 4 parameters:

  1. The initial amount of token XXX that is used for amplification, denoted by x0​x_0​x0​​

  2. The initial amount of token YYY that is used for amplification, denoted by y0​y_0​y0​​

  3. The change in token XXX amount after trading activities, denoted by Δx0​Δx_0​Δx0​​

  4. The change in token YYY amount after trading activities, denoted by Δy0​Δy_0​Δy0​​

Therefore, the real balances and virtual balances of the reserves are:

Real Balances

x=x0+Δx0y=y0+Δy0x = x_0 + \Delta x_0 \\ y = y_0 + \Delta y_0x=x0​+Δx0​y=y0​+Δy0​

Virtual Balances

x′=a⋅x0+Δx0y′=a⋅y0+Δy0x' = a \cdot x_0 + \Delta x_0 \\ y' = a \cdot y_0 + \Delta y_0x′=a⋅x0​+Δx0​y′=a⋅y0​+Δy0​

The constant product x′⋅y′=(a⋅x0+Δx0)⋅(a⋅y0+Δy0)=k′x' \cdot y' = (a \cdot x_0 + \Delta x_0) \cdot (a \cdot y_0 + \Delta y_0) = k'x′⋅y′=(a⋅x0​+Δx0​)⋅(a⋅y0​+Δy0​)=k′. Note that PminP_{min}Pmin​ and PmaxP_{max}Pmax​ at this time are:

{Pmin=(y0⋅a−y0)2k′Pmax=k′(x0⋅a−x0)2\begin{cases} P_{min} = \cfrac{(y_0 \cdot a - y_0)^2}{k'} \\ \\ P_{max} = \cfrac{k'}{(x_0 \cdot a - x_0)^2} \end{cases}⎩⎨⎧​Pmin​=k′(y0​⋅a−y0​)2​Pmax​=(x0​⋅a−x0​)2k′​​

The current price: P=y′x′=a⋅y0+Δy0a⋅x0+Δx0P = \cfrac{y'}{x'} = \cfrac{a \cdot y_0 + \Delta y_0}{a \cdot x_0 + \Delta x_0}P=x′y′​=a⋅x0​+Δx0​a⋅y0​+Δy0​​

Liquidity Providers have to contribute in the same proportion for all 4 amount types. We denote the contribution ratio to be bbb. LPs have to contribute x_1 + \Delta x_1$, $y_1 + \Delta y_1in which:

{x1=b⋅x0Δx1=b⋅Δx0y1=b⋅y0Δy1=b⋅Δy0\begin{cases} x_1 = b \cdot x_0 \\ \Delta x_1 = b \cdot \Delta x_0 \\ y_1 = b \cdot y_0 \\ \Delta y_1 = b \cdot \Delta y_0 \end{cases}⎩⎨⎧​x1​=b⋅x0​Δx1​=b⋅Δx0​y1​=b⋅y0​Δy1​=b⋅Δy0​​

The real balances and virtual balances of the reserve after contribution are:

Real Balances

x=(x0+x1)+(Δx0+Δx1)=(b+1)⋅(x0+Δx0)y=(y0+y1)+(Δy0+Δy1)=(b+1)⋅(y0+Δy0)x = (x_0 + x_1) + (\Delta x_0 + \Delta x_1) = (b + 1) \cdot (x_0 + \Delta x_0) \\ y = (y_0 + y_1) + (\Delta y_0 + \Delta y_1) = (b + 1) \cdot (y_0 + \Delta y_0)x=(x0​+x1​)+(Δx0​+Δx1​)=(b+1)⋅(x0​+Δx0​)y=(y0​+y1​)+(Δy0​+Δy1​)=(b+1)⋅(y0​+Δy0​)

Virtual Balances

x′=a⋅(x0+x1)+(Δx0+Δx1)=(b+1)⋅(a⋅x0+Δx0)y′=a⋅(y0+y1)+(Δy0+Δy1)=(b+1)⋅(a⋅y0+Δy0)x' = a \cdot (x_0 + x_1) + (\Delta x_0 + \Delta x_1) = (b + 1) \cdot (a \cdot x_0 + \Delta x_0) \\ y' = a \cdot (y_0 + y_1) + (\Delta y_0 + \Delta y_1) = (b + 1) \cdot (a \cdot y_0 + \Delta y_0)x′=a⋅(x0​+x1​)+(Δx0​+Δx1​)=(b+1)⋅(a⋅x0​+Δx0​)y′=a⋅(y0​+y1​)+(Δy0​+Δy1​)=(b+1)⋅(a⋅y0​+Δy0​)

The constant product, after the LP contribution, becomes:

x′⋅y′=(b+1)2⋅(a⋅x0+Δx0)⋅(a⋅y0+Δy0)=(b+1)2⋅k′x' \cdot y' = (b + 1)^2 \cdot (a \cdot x_0 + \Delta x_0) \cdot (a \cdot y_0 + \Delta y_0) = (b + 1)^2 \cdot k'x′⋅y′=(b+1)2⋅(a⋅x0​+Δx0​)⋅(a⋅y0​+Δy0​)=(b+1)2⋅k′

PminP_{min}Pmin​ and PmaxP_{max}Pmax​ at this time are:

{Pmin=((y0+y1)⋅a−(y0+y1))2(b+1)2⋅k′=(y0⋅a−y0)2k′Pmax=(b+1)2⋅k′((x0+x1)⋅a−(x0+x1))2=(x0⋅a−x0)2k′\begin{cases} P_{min} = \cfrac{((y_0 + y_1) \cdot a - (y_0 + y_1))^2}{(b + 1)^2 \cdot k'} = \cfrac{(y_0 \cdot a - y_0)^2}{k'} \\ P_{max} = \cfrac{(b + 1)^2 \cdot k'}{((x_0 + x_1) \cdot a - (x_0 + x_1))^2} = \cfrac{(x_0 \cdot a - x_0)^2}{k'} \end{cases}⎩⎨⎧​Pmin​=(b+1)2⋅k′((y0​+y1​)⋅a−(y0​+y1​))2​=k′(y0​⋅a−y0​)2​Pmax​=((x0​+x1​)⋅a−(x0​+x1​))2(b+1)2⋅k′​=k′(x0​⋅a−x0​)2​​

The current price is updated to be P=y′x′=(a⋅y0+Δy0)⋅(b+1)(a⋅x0+Δx0)⋅(b+1)=a⋅y0+Δy0a⋅x0+Δx0P = \cfrac{y'}{x'} = \cfrac{(a \cdot y_0 + \Delta y_0) \cdot (b + 1)}{(a \cdot x_0 + \Delta x_0) \cdot (b + 1)} = \cfrac{a \cdot y_0 + \Delta y_0}{a \cdot x_0 + \Delta x_0}P=x′y′​=(a⋅x0​+Δx0​)⋅(b+1)(a⋅y0​+Δy0​)⋅(b+1)​=a⋅x0​+Δx0​a⋅y0​+Δy0​​

We see that after LP contributes, the current price, PminP_{min}Pmin​ and PmaxP_{max}Pmax​ are unchanged. It is similar in the case of LPs withdrawals, where the ratio bbb is negative.

  • Initially, the first LP put 100 XXX and 100 YYY to the reserve, we have: x=100,y=100,Δx=0,Δy=0x = 100, y = 100, \Delta x = 0, \Delta y = 0x=100,y=100,Δx=0,Δy=0.

  • A user trades 20 XXX for 15 YYY, so we have the updated parameters: x=100,y=100,Δx=20,Δy=−15x = 100, y = 100, \Delta x = 20, \Delta y = −15x=100,y=100,Δx=20,Δy=−15.

  • Suppose an LP wants to contribute 20% of the current token amounts in the pool, so he should deposit:

    0.2⋅100+0.2⋅20=24(X)0.2⋅100+0.2⋅(−15)=17(Y)0.2 · 100 + 0.2 · 20 = 24 (X) \\ 0.2 · 100 + 0.2 · (−15) = 17 (Y)0.2⋅100+0.2⋅20=24(X)0.2⋅100+0.2⋅(−15)=17(Y)

ie. deposit 24X and 17Y tokens.

The parameters are then updated to be: x = 120$, $y = 120$, $\Delta x = 24$, $\Delta y = −18.

PreviousDynamic Auto-Adjusting FeesNextProtocol Fees

Last updated 1 year ago

Was this helpful?

where aaa is the . \

Example

amplification factor
​