LogoLogo
  • Learn about Balancer
  • Background
    • Team Goal
  • Fundamentals
    • White Paper
      • The Value Function
        • USDC WETH WBTC 33/33/33
        • BAL WETH 80/20
        • Price Change Proof
      • Spot & Effective Price
        • Spot Price
        • Effective Price
        • Price Impact
        • Price Impact - With Swap Fees
      • Trading Formulas
        • Out-Given-In
          • Including Swap Fees
        • In-Given-Out
          • Including Swap Fees
        • In-Given-Price
          • In-Given-Price Proof
          • Arbitrage 80/20
          • Arbitrage 50/50
        • Summary
      • Deposits & Withdrawals
        • Deposits
          • Proportional Deposits
            • BAL / WETH 80/20
          • Single Sided Deposits
            • Price Impact; 10,000 BAL
            • Price Impact; 100,000 BAL
          • Multi-token Deposits
            • Price Impact WMATIC / MTA / WETH
            • Price Impact [2] WMATIC / MTA / WETH
        • Withdrawals
          • Proportional Withdrawals
            • BAL/WETH 80/20
          • Singled Sided Withdrawals
            • Withdrawal Price Impact
    • Impermanent Loss
      • 50/50 Pools
      • 80/20 Pools
      • Multi-token Pools
    • veBAL Tokenomics
      • TLDR
      • Vote-Escrowed Governance
      • Financial Implications
        • Boosting BAL Incentives
          • Minimum veBAL for Max Boost
          • Maximum Boost
          • Calculating my Boost
          • Boost Delegation for Contract Wallet
        • Protocol Revenue Distribution
        • Gauge Voting
      • Inflation Schedule
      • veBAL FAQ
  • More Information
    • For Developers
      • GitHub Integration
    • For Support
    • References & Related Content
  • Documented Questions
    • Coming soon
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Fundamentals
  2. White Paper
  3. Trading Formulas

Out-Given-In

PreviousTrading FormulasNextIncluding Swap Fees

Last updated 3 years ago

Was this helpful?

Out-Given-In

The first function we will examine is the Out-Given-In which is used to calculate what you will receive based on what you are willing to sell. For example, if you know you will sell or spend a given amount of a token, we can calculate what tokens you will receive in return. Hence what amount you will get out, given what amount you put in; based upon the traits of the pool the trade is routed through.

Ao=Bo ∗ ( 1−(BiBi+Ai)WiWo )A_{o}=B_{o} \ * \ \Bigg( \ 1-\bigg({\frac{B_{i}}{B{i}+A_{i}}}\bigg)^{\frac{W_{i}}{W_{o}}} \ \Bigg)Ao​=Bo​ ∗ ( 1−(Bi+Ai​Bi​​)Wo​Wi​​ )

Let’s look at a very common trade, we will consider trading WETH for BAL. We have intentions of trading 2.5 WETH for the maximum amount of BAL possible. We will look at two possible pools we can route through to mimic what the Smart Order Router is doing under the hood of the Balancer Protocol to make sure users receive the best possible deal.

The two pools we will consider are the 80/20 (0.05%) and 50/50 (0.1%) BAL/WETH pools. At the time of writing, they hold the following balances.

Pool (Wo / Wi)
WETH Balance (Bi)
BAL Balance (Bo)

80/20

6,005.3009

5,315,514

50/50

135.9922

30,158

(Please note these are excluding swap fees)

First, we will calculate the amount out for the potential 80/20 pool interaction

Next, we will determine the amount of BAL we would receive if interacting with the 50/50 pool

We can see here the SOR would direct us to the 80/20 pool for our swap to be executed, because it benefits us as the user to receive as much BAL as possible. If another pool exists which would give the trader even more BAL, that route would be executed. See the Smart Order Router section for an example of trade optimization routing through multiple pools as opposed to one. Please note the example above would yield the optimal number of BAL for a user if the trade was routed through both pools depending on the gas price.