ERC-3156 · Final · ERC · 2020-11-15
ERC-3156: Flash Loans
A standard lender/borrower interface for single-asset flash loans, so a contract can borrow tokens uncollateralized within one transaction from any compliant lender.
What problem does it solve?
A flash loan lends tokens with no collateral on one condition: they are paid back, plus any fee, before the transaction ends. If not, the whole transaction reverts as if the loan never happened. Flash loans power arbitrage, collateral swaps, self-liquidation and refinancing.
Early lenders each invented their own interface: some pulled repayment, some expected it to be pushed back, some had multi-purpose entry points. Every integration was custom. ERC-3156 defines one interface for lenders and one for borrowers so a borrower contract can work with any compliant lender, including tokens that support "flash minting".
How it works
There are two roles, linked by a callback.
Lender (IERC3156FlashLender):
| Function | Behavior |
|---|---|
maxFlashLoan(token) |
Max amount available; 0 (not revert) for unsupported tokens |
flashFee(token, amount) |
Fee for that loan; reverts for unsupported tokens |
flashLoan(receiver, token, amount, data) |
Runs the loan, returns true on success |
Receiver (IERC3156FlashBorrower):
function onFlashLoan(address initiator, address token, uint256 amount, uint256 fee, bytes calldata data)
external returns (bytes32);
The flow inside flashLoan:
- Transfer
amountoftokentoreceiver. - Call
receiver.onFlashLoan(msg.sender, token, amount, fee, data), passingtoken,amountanddataunchanged andfee == flashFee(token, amount). - Require the return value to equal
keccak256("ERC3156FlashBorrower.onFlashLoan"). - Pull
amount + feefrom the receiver withtransferFrom, or revert. So the receiver must approve the lender for that amount before returning.
Sample code
A minimal borrower:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IERC3156FlashBorrower} from "@openzeppelin/contracts/interfaces/IERC3156FlashBorrower.sol";
import {IERC3156FlashLender} from "@openzeppelin/contracts/interfaces/IERC3156FlashLender.sol";
contract Borrower is IERC3156FlashBorrower {
bytes32 private constant CALLBACK_SUCCESS = keccak256("ERC3156FlashBorrower.onFlashLoan");
IERC3156FlashLender public immutable lender;
constructor(IERC3156FlashLender _lender) { lender = _lender; }
function borrow(address token, uint256 amount, bytes calldata data) external {
lender.flashLoan(this, token, amount, data);
}
function onFlashLoan(address initiator, address token, uint256 amount, uint256 fee, bytes calldata data)
external override returns (bytes32)
{
require(msg.sender == address(lender), "untrusted lender");
require(initiator == address(this), "untrusted initiator");
// ... use `amount` of `token` here (arbitrage, refinance, etc.) using `data` ...
IERC20(token).approve(address(lender), amount + fee); // lender pulls repayment
return CALLBACK_SUCCESS;
}
}
Things to know
- Status is Final. OpenZeppelin ships
IERC3156FlashLender/IERC3156FlashBorrowerinterfaces and anERC20FlashMintextension that makes a token its own flash lender. - Never trust callback arguments blindly. Check that
msg.senderis a lender you trust and thatinitiatoris an address you trust; otherwise someone can call your receiver with fake parameters. - Lenders must check the magic return value. Without it, a contract or EOA that left a standing approval to the lender could be drained by an attacker initiating a loan on its behalf.
- Flash minting makes huge amounts (up to
2^256 - 1if uncapped) available to anyone for one transaction. Protocols that read balances, prices or utilization within a transaction must assume an attacker can temporarily hold vast sums. - The standard covers single-asset loans only; multi-asset lenders expose one loan per token call.