Balancer Flash Loans TL;DR:V2 architecture gives access to combined liquidity from all Balancer PoolsBorrow several different tokens in one transactionPay zero feesImagine 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 LoansIn 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/0xe7c656a50db6f13b6b97cf1f864592da40871cf1f9932877b6d08f16866ecbcaWorkflow for a Flash Loan1. Take a loanBorrow X amount of USDC, up to the total amount of USDC available in the Vault2. Match the loan with an opportunityThe 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 collateralTrade collateral for TokenATake out another USDC loan with TokenA as collateralArbitrage 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 DEXTrade TokenA for USDC on another DEX3. Repay the loanAt 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 SwapAnyone 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 LineFlash 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.Capitalizing on Balancer’s Flash Loans was originally published in Balancer Protocol on Medium, where people are continuing the conversation by highlighting and responding to this story.

Read More

Leave a Reply