Batch Swaps.png

https://embed.notionlytics.com/wt/ZXlKd1lXZGxTV1FpT2lJME1tVTJaRGcxTkdFMFl6RTBOR1JqT0RNME9HTTNNVEJqT0dZd1pHSmxZeUlzSW5kdmNtdHpjR0ZqWlZSeVlXTnJaWEpKWkNJNklrUm1aa015YmpsbFYyZDRNMGMwVGtoNVZsQjFJbjA9

When attempting to swap tokens intermediate swaps are often necessary. This occurs when there is no pool containing both tokens or a cheaper route is available. These swaps usually require multiple function calls. Balancer provides a gas efficient ****solution, batch swaps.

How Does it Work?

Batch swaps are a single function call to Balancer’s Vault which then updates the balance of the pools.

The Balancer Vault aggregates tokens across all pools. This allows for the separation of accounting logic from pool contracts. The Vault accounts for token balances, while the Swaps contract implements the logic behind swaps, joins, and exits. A single batch swap can break a trade into stages. Token balances update at the end of each swap so trades can navigate through several pools.

A very useful feature, especially for UIs and aggregators, is batch swap simulation. This functionality is achieved using the queryBatchSwap function from the Vault. QueryBatchSwap mimics the BatchSwap function but reverts at the end returning the encoded balance update data.

Use Cases

1. No Pool Containing all the Tokens Being Traded

When no pool contains all the necessary tokens, multiple swaps must be made to receive the desired token. For example, when there is a trade from WBTC to wstETH on Balancer, there is no pool which contains both tokens. The only pool containing wstETH is the wstETH/WETH pool. Therefore, an intermediate trade will be necessary through Balancer’s WBTC/WETH pool - first trading WBTC for WETH:

Screen Shot 2022-07-04 at 4.29.27 PM.png

then WETH for wstETH:

Screen Shot 2022-07-04 at 4.30.42 PM.png

Instead of calling swaps on the two individual pools, the Vault’s batchSwap function can be called and the Vault will adjust token balances for both pools.

Screen Shot 2022-06-14 at 7.59.04 PM.png

2. Cheaper Route - Large Trade/High Slippage

Even if there is an existing pool with both tokens, a portion of the trade may be routed through additional pools if the trade is large enough to incur significant slippage. For instance, if there were a trade of 1000 ETH for USDC on Balancer, to minimize slippage, the trade will be broken into 3 portions and routed through different pools. The majority of the trade will be routed through the WETH/USDC pool. Another portion will be routed through two pools - first swapping WETH for DAI using the WETH/DAI pool, then DAI for USDC in the DAI/USDC/USDT pool. The third portion will be routed through Balancer’s boosted and linear pools. All 5 of these trades can be made with a single batch swap.

batchSwap.png