EIPs Explained

ERC-3234 · Stagnant · ERC · 2021-01-31

ERC-3234: Batch Flash Loans

Lender and borrower interfaces for flash loans of several tokens in one call, the multi-asset sibling of ERC-3156, useful for refinancing many positions at once.

What problem does it solve?

A flash loan lends tokens with no collateral, on the condition that they are repaid (plus a fee) before the transaction ends. ERC-3156 standardised single-token flash loans. Many lenders, though, can lend several tokens from one contract, and moving several positions between protocols at once needs several assets simultaneously.

ERC-3234 defines a separate but consistent standard for these batch flash loans, so a borrower contract can work with any compliant lender.

How it works

Two contracts talk through a callback.

Lender (IERC3234BatchFlashLender):

Function Purpose
maxFlashLoan(tokens) Max loan per token; returns 0 for unsupported tokens
flashFee(tokens, amounts) Fee per loan; reverts for unsupported tokens
batchFlashLoan(receiver, tokens, amounts, data) Start the loan, returns true on success

Receiver (IERC3234BatchFlashBorrower):

function onBatchFlashLoan(
    address initiator,
    address[] calldata tokens,
    uint256[] calldata amounts,
    uint256[] calldata fees,
    bytes calldata data
) external returns (bytes32);

Flow inside batchFlashLoan:

  1. Transfer amounts[i] of each tokens[i] to the receiver.
  2. Call onBatchFlashLoan(msg.sender, tokens, amounts, fees, data) and require it to return a fixed magic hash.
  3. Pull amounts[i] + fees[i] of each token back from the receiver with transferFrom, or revert.

The receiver must therefore approve the lender for amount + fee of every token before its callback returns. Repayment is pulled rather than pushed so the lender can keep using transferFrom-based accounting.

Sample code

A minimal borrower (ERC-20 calls simplified; use SafeERC20 in practice):

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

interface IERC20 { function approve(address, uint256) external returns (bool); }

contract BatchBorrower {
    address public immutable lender;
    bytes32 constant CALLBACK_SUCCESS = keccak256("ERC3234BatchFlashBorrower.onBatchFlashLoan");

    constructor(address _lender) { lender = _lender; }

    function onBatchFlashLoan(
        address initiator,
        address[] calldata tokens,
        uint256[] calldata amounts,
        uint256[] calldata fees,
        bytes calldata /* data */
    ) external returns (bytes32) {
        require(msg.sender == lender, "untrusted lender");
        require(initiator == address(this), "untrusted initiator");

        // ... use the borrowed tokens here ...

        for (uint256 i = 0; i < tokens.length; i++) {
            IERC20(tokens[i]).approve(lender, amounts[i] + fees[i]);
        }
        return CALLBACK_SUCCESS;
    }
}

Things to know

  • Status is Stagnant. The single-asset ERC-3156 is the one widely referenced in practice.
  • The spec text is inconsistent: the interface declares bytes[] data but the example uses bytes data, and the required return value is named both ERC3234BatchFlashBorrower.onBatchFlashLoan and ERC3156BatchFlashBorrower.onBatchFlashLoan. Confirm the exact hash with your lender.
  • Never trust callback arguments blindly. Check that msg.sender is a known lender and that initiator is someone you trust, otherwise anyone can trigger your callback with fake data.
  • Lenders must verify the magic return value, or a contract that left an open approval could be drained through fees.
  • Flash-minted liquidity can be enormous; the spec gives examples of interest-rate manipulation and overflow attacks that integrating protocols must defend against.

#defi #flash-loan #lending #interface #erc

Details

Authors
Alberto Cuesta Cañada (@albertocuestacanada), Fiona Kobayashi (@fifikobayashi), fubuloubu (@fubuloubu), Austin Williams (@onewayfunction)
Status
Stagnant
Type
Standards Track / ERC
Created
2021-01-31

Machine-readable: Markdown · JSON