Jump to content
Crypto Front Page

Crypto networks, markets and policy

A flash loan turns one transaction into a complete trade

How does a flash loan work in DeFi? It lends funds without collateral for one transaction, then checks repayment plus a fee before that transaction can finish.

The Crypto Front Page Desk4 min read

A flash loan turns one transaction into a complete trade

A DeFi flash loan lends available funds without collateral, provided the borrower repays the amount plus a fee before the same transaction ends. Because the blockchain executes that transaction atomically, the lender either gets repaid within it or the transaction’s state changes are undone.

A flash loan can fund an arbitrage trade, but the trade must complete as one unit. Searchers may send trades in a searcher bundle through Flashbots; a flashbots builder can change the searcher’s situation by giving the bundle a path to block inclusion, so an attempt that simulation shows would lose can be left out instead of reverting on-chain and paying gas.

How does a flash loan work, step by step?

A flash loan moves funds from a lender to a borrower contract, runs the borrower’s instructions, then checks that the lender receives repayment plus its fee.

  • Borrow call: the borrower requests an asset from a lending pool.
  • Borrower contract: it runs the trade or other instructions using the funds.
  • Repayment check: the lender collects the borrowed amount plus a fee, or the transaction reverts.

These parts happen in order inside one transaction. The borrower contract receives the funds and carries out its logic; the lender then checks repayment. “Atomic” means the steps succeed together or the transaction’s state changes are rolled back. It is like a checkout that completes only if every part of the payment clears: the lender does not leave the funds outstanding for a later transaction.

Aave offers flash loans, with a fee set by governance. Uniswap V2 offers a related feature called a flash swap: tokens can leave a pool before the transaction finishes, but the pool must be repaid or the transaction reverts. The precise contract calls differ, but both rely on completing the borrowing and repayment in one transaction.

Say you borrow USDC, buy ETH on one decentralized exchange where it is cheaper, then sell that ETH on another where it is priced higher. The contract uses the sale proceeds to repay the USDC loan and its fee. Any remaining USDC is the spread left over for the trader, before other costs. If the sale proceeds fall short, the repayment check fails and the trade reverts.

A revert undoes the transaction’s state changes, including the attempted borrowing and swaps, but it does not refund gas. The network still used resources to process the failed transaction. That is why an opportunity that looks profitable before execution can still cost money if it fails once submitted on-chain.

Why can a flash loan expose a weak oracle?

A flash loan supplies capital, but it does not create a profitable opportunity by itself; the borrower still needs a price difference or a protocol weakness that can be acted on within one transaction.

That distinction matters for oracles, which provide price information to DeFi protocols. A spot-price oracle that reads a decentralized exchange’s current price can be vulnerable if a large trade temporarily moves that price. A borrower can use flash-loaned funds to make the move, interact with a protocol that trusts the distorted spot price, and attempt to repay before the transaction ends.

The bZx flash-loan attacks in February 2020 showed how this could happen: borrowed funds helped manipulate market prices used by a protocol as an oracle. The key weakness was the reliance on a price that could be moved within the same transaction. A time-weighted average price (TWAP) spreads readings over time, while a Chainlink feed draws on oracle infrastructure; both resist a single sudden DEX trade distorting the reported price in the same way.

Searchers also use flash loans for arbitrage when they can identify a temporary price gap and fit the trade and repayment into one transaction. Flashbots provides MEV research and infrastructure that helps searchers send bundles for block inclusion. A bundle can group the planned transactions and be evaluated before inclusion, so a searcher can leave out a trade that simulation shows will lose rather than pay gas for an on-chain revert.

The outcome is simple: a flash loan makes temporary capital available, while atomic execution enforces repayment before the transaction can stand. When assessing a proposed trade, follow the borrowed asset through each swap, check whether the proceeds cover repayment and fees, and ask whether any protocol in the route depends on a price that one transaction can move.