Capitalizing on Balancer’s Flash Loans

Orb
Balancer Protocol
Published in
4 min readNov 16, 2022

Balancer Flash Loans TL;DR:

  • V2 architecture gives access to combined liquidity from all Balancer Pools
  • Borrow several different tokens in one transaction
  • Pay zero fees

Imagine you had $1000 in ETH deposited into Compound with $200 USDC borrowed. To withdraw all your ETH, you need USDC. With flash loans, you can borrow $200 USDC, repay your debt, withdraw the ETH from Compound, deposit ETH into Aave, borrow $200 USDC against that ETH and repay your loan. All in one transaction. Sounds crazy, right?

Flash loans are uncollateralized loans that must be repaid (including interest) in a single transaction.

The novelty Balancer brings to Flash Loans is the Vault architecture, consolidating liquidity across all Balancer Pools. The Vault is a smart contract that holds and manages tokens across all Balancer Pools thus separating token accounting and management from Pool logic. Since all tokens are under the Vault, consolidated balances are available for Flash Loans. As a result, users have access to deeper, combined liquidity across all pools, enabling larger loans. Flash Loans are atomic transactions, if a given strategy is not executable, the transaction is reverted.

Free Flash Loans

In addition to having access to deeper liquidity across all Balancer pools, users also get free Flash Loans. Yes, that is correct! In contrast to other protocols that charge a fee for performing flash loans, Balancer offers zero fee flash loans. The fees collected as interest on flash loans go to the DAO Treasury. However, at deployment flash loans fees were set to zero, and at the time of writing they have still not been activated by the governance.

https://etherscan.io/tx/0xe7c656a50db6f13b6b97cf1f864592da40871cf1f9932877b6d08f16866ecbca

Workflow for a Flash Loan

1. Take a loan

Borrow X amount of USDC, up to the total amount of USDC available in the Vault

2. Match the loan with an opportunity

The most common use cases for flash loans are arbitrage and collateral swaps.

Collateral Swap (swapping the collateral backing the user’s loan for another type of collateral):

  • Pay off USDC loan for collateral
  • Trade collateral for TokenA
  • Take out another USDC loan with TokenA as collateral

Arbitrage Trade (traders can make money by looking for price discrepancies on different exchanges.

Let’s assume two DEXs are pricing tokenA differently. An arbitrageur can use a flash loan and call a separate smart contract to buy TokenA on the exchange with the lower price, and then sell the tokens on the other exchange where the price of TokenA is higher:

  • Trade USDC for TokenA on one DEX
  • Trade TokenA for USDC on another DEX

3. Repay the loan

At this step you would need to check if;

$final >= principal * (1 + interest)$

If successful, the principal and fee are repaid but any profit made is kept. If unsuccessful the transaction reverts but the gas fee is lost.

Example of a Flash Loan:

Flash Swap

Anyone who identifies a price discrepancy in two Balancer Pools can execute a Flash Swap. After the trader identifies the imbalance, they tell the Vault to make the swap and they get rewarded with the profit. The arbitrageur who performs a flash swap does not need to hold any of the input tokens that would normally be required for the trade.

Batch Swaps are used for trades that hop through multiple pools. They are useful when swapping between two tokens that are in different pools, and for saving gas on routes with better prices than single swaps. For more complex flash swaps, you will have to use the batch swap. It’s important to note that when performing a flash swap both pools must have the desired tokens and the pool balances cannot be zero.

The Balancer SDK provides an example on how to encode a simple flash swap for a batch swap (encodesimpleFlashSwap). Additionally, there is a provided method to test if a flash swap is valid and to check its associated profit (querySimpleFlashSwap). You can find an example here.

The Bottom Line

Flash Loans are a form of DeFi lending that can have many interesting use cases. Moreover, anyone can make money if they identify a good opportunity regardless of available capital. If you want to build features that leverage Balancer’s free flash loans and capital efficiency, you can start by joining the #dev Slack channel and looking through the documentation.

--

--

Orb
Balancer Protocol

Orb is dedicated to propelling Balancer’s growth by scaling global utilization of the Protocol and growing the ecosystem.