Section 14 of 18
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 dedicatedflash()with an explicit price: the fee is computed up front withmulDivRoundingUp(amount, fee, 1e6), tokens are transferred optimistically,uniswapV3FlashCallbackfires onmsg.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 packedfeeProtocolnibbles and the LP share is pushed intofeeGrowthGlobalper unit of liquidity.setFeeProtocolpacks two 1/x ratios (0 or 4–10) into one byte;collectProtocolclamps 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
Requirements
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 continueAlready have an account? Log in