Section 14 of 18

Build
+15 Lynx

Flash Loans and Protocol Fees

Key takeaway: V2 hid flash loans inside swap() and priced them implicitly through the K-invariant. V3 gives them a dedicated flash() with an explicit price: the fee is computed up front with mulDivRoundingUp(amount, fee, 1e6), tokens are transferred optimistically, uniswapV3FlashCallback fires on msg.sender, and repayment is verified by comparing balances — balanceBefore + fee <= balanceAfter, reverts 'F0'/'F1'. Whatever was paid above the loan (including voluntary overpayment) is split by the packed feeProtocol nibbles and the LP share is pushed into feeGrowthGlobal per unit of liquidity. setFeeProtocol packs two 1/x ratios (0 or 4–10) into one byte; collectProtocol clamps withdrawals and decrements by one wei rather than clearing the slot, keeping future SSTOREs cheap.

What You Are Building

Three functions from UniswapV3Pool: flash, setFeeProtocol, and collectProtocol. Together they are the pool's lending desk and the protocol's cash register.

Your Code

Solution.sol
Solidity
Loading editor...

Requirements

Write your implementation, then click Run Tests. Tests execute on the server.

Sign up free — keep reading + earn 15 Lynx

Zealynx Academy is free. Track your progress, earn Lynx, and climb the leaderboard.

Sign up free to continue

Already have an account? Log in