EIPs Explained

ERC-7399 · Review · ERC · 2023-07-25

ERC-7399: ⚡ Flash Loans ⚡

A common interface for flash loans of ERC-20 tokens, letting borrowers switch between lenders without code changes and separating who receives the loan, who handles the callback and where repayment goes.

What problem does it solve?

A flash loan is a loan that is borrowed and repaid inside one transaction: if the money (plus any fee) is not back by the end, the whole transaction reverts. They power arbitrage, liquidations, collateral swaps and refinancing.

Every flash lender implemented them differently: different function names, different callback shapes, some pulling repayment with transferFrom, others expecting the borrower to push it back, and different rules for flash minting. Borrowers had to write custom adapters per lender. ERC-7399 is the successor to ERC-3156 and aims to cover all these variations with one interface.

How it works

A lender implements three functions:

Function Purpose
maxFlashLoan(asset) Most that can be borrowed. Never reverts; returns 0 if unsupported.
flashFee(asset, amount) Fee on top of principal. Never reverts; returns type(uint256).max if the loan is impossible.
flash(loanReceiver, asset, amount, data, callback) Executes the loan and returns whatever the callback returned.

The flow of flash:

  1. The lender transfers amount of asset to loanReceiver.
  2. It calls callback(initiator, paymentReceiver, asset, amount, fee, data), where initiator is msg.sender of flash and fee == flashFee(asset, amount).
  3. Before the callback ends, paymentReceiver (chosen by the lender) must have received amount + fee, otherwise the lender reverts.

callback is a Solidity external function type: 24 bytes packing the callback receiver's address (20 bytes) and the function selector (4 bytes). So the loan receiver, the contract that runs the logic and the function name can all be chosen freely, and one borrower can expose several callbacks.

Repayment is push-based: the borrower sends funds to paymentReceiver rather than the lender pulling them. The non-reverting view functions let aggregators sort lenders by cost without try/catch.

Sample code

A borrower that takes a loan and repays it:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

interface IERC7399 {
    function maxFlashLoan(address asset) external view returns (uint256);
    function flashFee(address asset, uint256 amount) external view returns (uint256);
    function flash(
        address loanReceiver,
        address asset,
        uint256 amount,
        bytes calldata data,
        function(address, address, IERC20, uint256, uint256, bytes memory) external returns (bytes memory) callback
    ) external returns (bytes memory);
}

contract Borrower {
    IERC7399 public immutable lender;

    constructor(IERC7399 _lender) { lender = _lender; }

    function borrow(address asset, uint256 amount) external {
        lender.flash(address(this), asset, amount, "", this.onFlashLoan);
    }

    function onFlashLoan(
        address initiator,
        address paymentReceiver,
        IERC20 asset,
        uint256 amount,
        uint256 fee,
        bytes memory /* data */
    ) external returns (bytes memory) {
        require(msg.sender == address(lender), "untrusted lender");
        require(initiator == address(this), "untrusted initiator");

        // ... use `amount` of `asset` here (arbitrage, liquidation, etc.) ...

        asset.transfer(paymentReceiver, amount + fee); // push repayment
        return "";
    }
}

Things to know

  • Status: Review. The interface may still change before Final.
  • Callback arguments cannot be trusted on their own: anyone can call your callback with made-up values. Check that msg.sender is a lender you trust and, if you rely on data, that initiator is trusted too.
  • Not directly backwards compatible with ERC-3156, but a simple wrapper can expose ERC-7399 on top of an ERC-3156 lender (or any "pull"-style lender).
  • Flash-minted assets can have their supply inflated to huge values within a transaction. Oracles that depend on total supply, and math that could overflow, need protection.

#defi #flash-loan #erc20 #lending

Details

Authors
Alberto Cuesta Cañada (@alcueca), Michael Amadi (@AmadiMichaels), Devtooligan (@devtooligan), Ultrasecr.eth (@ultrasecreth), Sam Bacha (@sambacha)
Status
Review
Type
Standards Track / ERC
Created
2023-07-25
Requires

Machine-readable: Markdown · JSON