What is a flash loan and how is it used in DeFi?

Answer

A flash loan is an uncollateralized loan that must be borrowed and repaid within a single blockchain transaction. If the loan isn't repaid with fee by transaction end, the entire transaction reverts — making it atomically safe for the lender. Since the lender faces zero risk, there's no collateral requirement. Flash loans are possible because of smart contract atomicity. Legitimate uses: (1) Arbitrage — borrow 1M USDC, buy ETH on Uniswap, sell on Sushiswap at higher price, repay loan, keep profit — all in one transaction; (2) Collateral swapping — replace one type of collateral for another in a lending position without manual multi-step process; (3) Self-liquidation — repay a loan to retrieve collateral without needing the repayment funds upfront; (4) Protocol testing — security researchers use flash loans to test protocol resilience. Exploitative uses: (1) Oracle manipulation — borrow large amount, manipulate AMM price used as oracle, exploit vulnerable protocol, repay loan (Harvest Finance hack: $34M); (2) Governance attacks — borrow tokens to temporarily acquire voting power. Providers: Aave (0.09% fee), Uniswap V2/V3 (0.3% fee), dYdX (0 fee).