Traversing the Balancer Vault with Smart Order Routing (SOR)

Balancer

The flexibility of Balancer Protocol’s SOR ensures any custom Pool built on Balancer can benefit from all Balancer liquidity.

Balancer is a generalized Automated Market Maker (AMM) with a single Vault architecture at its core. Balancer V2’s Vault separates the token accounting and management from the Pool logic. The Vault is a single contract that holds and manages all assets added by all Balancer Pools. If a project wants to design a new invariant for an AMM, they can do so by focusing on the math while retaining the security and asset management functionality of Balancer’s Vault.

Custom Invariants
Element Finance is a project built on Balancer focused on the fixed-rate yield that designed its own invariant to implement in a custom Balancer Pool, the Convergent Curve Pool. As the name suggests, it facilitates trades between assets that converge in value over time. By utilizing Balancer, they efficiently implemented their own invariant with a Balancer Vault integration, which inherits all the powerful features of Balancer v2.


where x and y are the reserves and t is the fraction of the period remaining


Additional AMM invariants built on Balancer include Tempus’ Principal-Yield AMM, allowing efficient trading between token pairs with predictable volatility and price correlation changes. Sense Space implements the yieldspace invariant and is the central market for Zero / Claim trading.

With this flexibility comes the need to route through various Pools with differing logic and that’s where Smart Order Routing comes in.

About Smart Order Routing
Smart Order Router (SOR) is an off-chain tool for finding the best price execution across Balancer Pools. The SOR finds the optimal trades, whether a direct swap in one Pool or a combination of trades hopping through multiple Pools.

It considers how much gas additional hops cost and determines if it makes economic sense to route through an additional Pool. The SOR keeps expanding as new Pool types that use different math under the hood are added.

Smart Order Routing ensures that all Pools in the Balancer ecosystem can support trades. By integrating with the SOR, any custom Pool built on Balancer can benefit from all the other Balancer liquidity.

How the SOR Works
  • The optimization mechanism finds the path through a set of Balancer Pools with the greatest output (after gas costs).
  • List of all Pools with the tokens a user wishes to trade
  • Performs optimization across all Pools for best overall execution price (taking into account Pool slippage and gas costs)
Multi-hop Gas Efficiency
The Vault handles all assets by all Balancer V2 Pools. Assets can shift around without emitting an ERC20 transfer event on-chain. For most DEXs each Pool is its own contract that holds its own tokens which means if you want to trade across multiple Pools, the assets need to be transferred from Pool to Pool.

In most DEXs, where token accounting pairs with Pool logic, multi-hop trading (A->B->C) can be costly since ERC20 tokens must transfer at each hop. Balancer’s differentiator is that all the tokens are stored in the same contract, The Vault.

This difference allows for far greater swap efficiency. Instead of transferring tokens on each step of a multi-hop trade, The Vault simply keeps internal records of which Pool is holding what, transferring tokens only at the input and output step. This reduction in token transfers ultimately saves a considerable amount of gas. Put simply, only the net amounts are transferred.

Flash Swaps
We can demonstrate the above mechanism with an example of a Flash Swap. A Flash Swap is when a user detects price asymmetry across multiple Balancer Pools and routes a profitable arbitrage trade through the Balancer V2 Vault without the need for initial capital or flash loans. The trader identifies the imbalance, tells the Vault to make the swap, and is rewarded with the profit.2



Three swaps were made in the above Flash Swap across three different Pools and used 146k gas. The user was able to produce a 1.32 ETH profit by simply rebalancing existing Balancer Pools.
For reference, here is a typical two-hop Uniswap V2 trade.


We can see 186k gas was used for this two-hop trade (USDC -> ETH -> bPRO); this transaction emits an additional ERC20 transfer event than the same swap on Balancer because the USDC->ETH swap would occur internally to the Vault. As the number of hops increases, this process gets more gas efficient when compared to typical DEX routing.

Complex Routing Over Multiple Pool Types

Balancer

In the above swap of 1 WBTC -> USDT, the WBTC first gets exchanged for USDC in the WBTC/ETH/USDC Weighted Pool, then routes through the USDC/DAI/USDT Stable Pool. This routing mechanism allows Liquidity Bootstrapping Pool users to pay in any token (provided there is a route on Balancer).

This swap results in two ERC20 transfers, 1 WBTC being transferred to the Vault and ~$39k USDT transferred to the user. Pool balances are all maintained internally in the V2 Vault.

High Output in Low Gas Environments
The efficiency of the SOR stands out in a low gas fee environment with diverse available liquidity. Let’s look at trade routing on Beethoven X, the first and only Balancer-friendly fork that operates on the Fantom Network.

Their core contracts are a 1:1 fork of Balancer, and they have built up a wide array of multi-token index Pools. With gas on Fantom being a fraction of what it costs on Ethereum mainnet, the SOR can route through many Pools since additional hops cost very little.

balancer

Precise Routing
In the above trade of 2 WBTC -> USDC, we can see the breakdown of the routing. If a Pool has both WBTC and USDC, the Pool will be considered for routing. But it gets interesting when the SOR identifies a multihop route with better price execution, such as the bottom-most route.

2.42% of the trade gets routed through two Pools, the WBTC/WETH/WFTM Pool, and the WFTM/BNB/USDC Pool, by first swapping WBTC->WFTM then WFTM->USDC.

This means as traders swap between assets, they are helping rebalance the Pools by capitalizing on any price discrepancies between Pools while liquidity providers earn swap fees along the way.


The Bottom Line
Security, flexibility, and gas efficiency are all core tenets of Balancer V2. This is reflected in the custom invariant AMMs built on Balancer, multihop gas efficient trades, and Smart Order Routing (SOR) to tie it all together.

The design space of Balancer Pools is infinite. Balancer Protocol offers the limitless capability for projects looking to build a novel AMM and tap into the primary source for programmable liquidity.

A special thank you to the Balancer Ballers for conducting the research and collaborating on the writing of this piece.


Previous Post Next Post