ERC-3234 Batch flash loan (Batch Flash Loans)

Stagnant · Vaults & DeFi · created 2021-01-31

Defines a standard callback interface that lender and borrower contracts must implement so that multiple kinds of tokens can be borrowed and repaid simultaneously within a single transaction.

Why it is needed

A flash loan is an unsecured loan that is borrowed and repaid within a single transaction, and cases such as simultaneously refinancing positions across several platforms often require borrowing multiple assets at once. However, previously there was only a single-asset flash loan standard (ERC-3156), and each lender implemented batch flash loans for multiple tokens with its own different interface. As a result, borrower contracts had to write custom code for each lender, and a sloppy implementation that failed to properly validate callback return values risked having pre-approved funds stolen outright. Without a common interface, compatibility and security levels across the ecosystem end up inconsistent.

In depth

Lender interface rules

The lender must implement IERC3234BatchFlashLender. maxFlashLoan(tokens) must return the maximum amount that can be borrowed for each token, and must return 0 rather than reverting for unsupported tokens. flashFee(tokens, amounts) must return the fee to be charged for each token and amount combination, and must revert for unsupported tokens, because returning 0 could be misinterpreted as meaning there is no fee.

batchFlashLoan execution order and constraints

batchFlashLoan must first transfer the amounts specified in tokens and amounts to the receiver before the callback. The callback must pass msg.sender as initiator, and the tokens, amounts, and data received must be passed to onBatchFlashLoan unmodified. The fees[i] for each token must equal the value of flashFee(tokens[i], amounts[i]). After the callback completes, amounts[i]+fees[i] must be collected from the receiver for each token, and if this fails the entire transaction must be reverted. If the whole process succeeds, batchFlashLoan must return true.

Callback return value verification

batchFlashLoan must verify that the return value of onBatchFlashLoan matches keccak256("ERC3234BatchFlashBorrower.onBatchFlashLoan"). If this verification is omitted, the loan proceeds even if the receiver returns an arbitrary value, creating a risk that funds from an untrusted contract that was only pre-approved could be stolen.

Borrower (Receiver) interface rules

The receiver must implement IERC3234BatchFlashBorrower and provide onBatchFlashLoan(initiator, tokens, amounts, fees, data). For the transaction not to revert, the function must approve the msg.sender (lender) to take amounts[i]+fees[i] for each token before it finishes. The original Receiver Specification states that on success it must return keccak256("ERC3156BatchFlashBorrower.onBatchFlashLoan").

Pull-based repayment design

This standard adopts a "pull" approach in which the lender pulls repayment from the receiver using transferFrom. A "push" approach, where the receiver sends the repayment directly, is also possible, but it would require redesigning all of the lender's other functions around transfers as well, increasing complexity. The pull approach is simpler and matches the structure of existing flash lenders such as Aave, dYdX, and Uniswap, which lend multiple tokens from a single contract.

Standard status

This proposal is currently in Stagnant status, halted without active discussion. Its contents may change or may not progress further, so you should check the latest status again before reviewing it for actual implementation.

Things to watch out for

Original ERC · ← ERCs