What Are Flash Loans in DeFi and How Do They Work?
A flash loan is an uncollateralized DeFi loan that must be borrowed and repaid inside one blockchain transaction. Here is how it works and what it costs.
On this page
Short answer
A flash loan is a loan in decentralized finance that lets a smart contract borrow any amount of an asset with no collateral, as long as the borrowed amount plus a fee is returned before the same blockchain transaction ends. If the money is not back in time, the transaction reverts and every step inside it is undone, as if the loan never happened.
What is a flash loan in DeFi?
A flash loan is a loan in decentralized finance that lets a smart contract borrow any amount of an asset with no collateral, as long as the borrowed amount plus a fee is returned before the same blockchain transaction ends. There is no credit check, no deposit and no repayment schedule, because the loan only exists for the few seconds a transaction takes to settle.
The trick is atomicity: a blockchain transaction either completes every step or fails completely, with nothing in between. The lending protocol hands the funds to the borrower's contract, lets its code run, and then checks whether the money came back plus the fee. If the check fails, the whole transaction reverts and every step inside it is undone.
That makes the lender's risk close to zero. The protocol does not need collateral because repayment is not a promise to keep later; it is a condition enforced by code before the transaction can finalize. Aave made the idea famous when its flash loans went live on Ethereum in January 2020. For the wider DeFi picture, see the top DeFi protocols.
How a flash loan executes, step by step
A flash loan follows a fixed sequence enforced by the lending contract. Every step happens inside one transaction, so the order cannot be changed and nothing can be skipped.
- Request. Your smart contract calls the lending pool's flash loan function, naming the asset, the amount and your receiver contract.
- Funds out. The pool sends the tokens to your contract and calls a callback function on it, such as executeOperation on Aave.
- Use. Your code runs inside the callback: trades, swaps, liquidations or whatever the strategy needs.
- Approve repayment. Your contract approves the pool to pull back the borrowed amount plus the fee.
- Check. The pool verifies its balance. If the money is back, the transaction confirms; if not, everything reverts.
Only a smart contract can take a flash loan, because the protocol must call your code during the loan. A regular wallet has no code to call, so it cannot borrow this way. For building on DeFi, see DeFi development.
What are flash loans used for?
Flash loans are a developer tool, not a consumer product. The people using them write smart contracts, and most uses fall into a small set of patterns where temporary access to a large amount of capital is the whole point.
| Use | How it works | Who uses it |
|---|---|---|
| Arbitrage | Buy low on one exchange, sell high on another | Trading bots and searchers |
| Liquidation | Repay a bad debt, claim the bonus | Liquidators |
| Collateral swap | Swap the collateral behind a loan | Borrowers managing risk |
| Debt refinancing | Move a loan to a cheaper rate | Borrowers and refinancing tools |
| Liquidity switch | Change how a position is funded | Aave users and builders |
Arbitrage is the most common use. A price gap between two exchanges may last only seconds, and a flash loan lets a trader act on it without holding the capital first. Liquidation is the second: lending markets reward anyone who repays an undercollateralized position, and a flash loan supplies the capital to clear the debt and claim the bonus in one move.
What does a flash loan cost?
A flash loan costs three things: the protocol fee, the network gas and the slippage on any trades inside the transaction. The fee is the only fixed part; gas and slippage depend on the network and the size of the trade.
| Cost | Typical size | What sets it |
|---|---|---|
| Protocol fee | 0.05% on Aave V3 | Set by governance, read from the pool |
| Network gas | Varies by chain and congestion | Complexity of the transaction |
| Slippage | Varies by trade size and depth | How much the trade moves the price |
On Aave V3 the flash loan fee is 0.05% of the amount borrowed, set at deployment and changeable by governance vote, so serious builders read the current value from the pool at runtime rather than hardcoding it. Balancer V2 charges no fee on its flash loans. The honest way to judge a strategy is net profit: gross profit minus the protocol fee, minus gas, minus slippage.
Example (hypothetical): a $100,000 arbitrage in one transaction
Example (hypothetical): a trader's bot spots a token priced at $10.00 on one decentralized exchange and $10.18 on another. The bot's contract borrows 100,000 USDC as a flash loan from Aave and buys 10,000 tokens at $10.00, spending the full $100,000.
It then sells the 10,000 tokens on the second exchange at $10.18, receiving $101,800. The gross profit is $101,800 minus $100,000, which is $1,800. The Aave fee is 0.05% of $100,000, which is $50, and the network gas costs $30.
The net profit is $1,800 minus $50 minus $30, which is $1,720. The contract repays $100,050 to the pool, keeps the $1,720, and the transaction confirms. If the second exchange's price had moved before the sale, the profit would have vanished and the whole transaction would have reverted instead.
What are the risks and attacks?
Flash loans have a darker reputation because they have been used to amplify attacks on other DeFi protocols. A borrowed pile of capital can be used to manipulate a price feed or a governance vote inside a single transaction, which is why the same tool that helps liquidators also helps attackers.
The usual pattern is a price-oracle attack. The attacker borrows a large sum, uses it to push a token's price on a thin market, and then borrows against the inflated price from a lending protocol that trusts that price feed. The profit comes from the second protocol, and the flash loan itself is repaid, so the attacker risks only the gas.
Protocols defend against this with time-weighted price feeds, which average prices over time instead of trusting a single moment, and with limits on how much can be borrowed against a single asset. For the audits that catch these issues, see what a smart contract audit is.
What are the common mistakes?
The most common mistakes with flash loans are treating them as free money, assuming the trade will still be there when the transaction runs, and forgetting that the whole transaction reverts if any step fails.
- Thinking the loan is free. The fee, gas and slippage all come out of the profit, and a thin margin can turn into a loss.
- Assuming the price holds. Prices move between blocks, and a gap that exists when you check may be gone when your transaction runs.
- Ignoring slippage. A large trade on a thin pool moves its own price, which can eat the whole arbitrage.
- Forgetting the revert. If any sub-call fails, the entire transaction is undone, including the loan, so a failed attempt still costs gas.
- Trusting a single price feed. A strategy that reads one oracle is exposed to the same manipulation attackers use.
- Skipping the test. Running an untested flash loan contract on mainnet risks the gas on every failed attempt and the funds on a logic error.
For building a flash loan strategy safely, a partner for smart contract development can help with the contract, the testing and the audit before mainnet.
Key takeaways
- A flash loan borrows with no collateral and must be repaid in the same transaction
- Atomicity is the collateral: a failed repayment undoes the whole transaction
- Arbitrage and liquidation are the most common uses
- Aave V3 charges a 0.05% fee; gas and slippage are the other costs
- Price-oracle attacks are the main risk, and time-weighted feeds are the main defence
Frequently asked questions
Can anyone take a flash loan?
No. Only a smart contract can take one, because the lending protocol must call your code during the loan to run the strategy and check repayment. A regular wallet has no code to call.
What happens if a flash loan is not repaid?
The whole transaction reverts. Every step inside it is undone, as if the loan never happened, and the only cost to the borrower is the gas spent on the failed transaction.
How much does a flash loan cost on Aave?
Aave V3 charges a fee of 0.05% of the amount borrowed, set by governance and readable from the pool. Gas and slippage on the trades inside the transaction are extra costs.
Are flash loans legal?
Flash loans are a neutral tool, and using them for arbitrage or liquidation is a normal part of DeFi. Using them to manipulate a price feed or exploit a protocol is an attack, and the legality depends on the jurisdiction and the act.
Which protocols offer flash loans?
Aave is the best known, with flashLoan and flashLoanSimple on V3, and Balancer offers flash loans with no fee. The ERC-3156 standard describes a common interface that other lending protocols can follow.
Can a flash loan be used to steal funds?
A flash loan itself only borrows and repays inside one transaction, so it cannot take funds on its own. Attacks use the borrowed capital to exploit a weakness in another protocol, such as a price feed that trusts a single thin market.