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
- Callback arguments can never be trusted unconditionally: The initiator, tokens, amounts, fees, and data passed to onBatchFlashLoan can be falsified by the caller. To trust them, you must verify that the caller of onBatchFlashLoan is on a verified lender whitelist, and for the reliability of data it is also recommended to additionally check that the initiator is on a trusted list.
- Risk of pre-approval for untrusted borrowers: If approve has already been set before onBatchFlashLoan executes, and the lender does not verify the return value of onBatchFlashLoan, an attacker can call batchFlashLoan to steal the approved funds up to the limit. The lender must verify the fixed return hash and revert if it does not match.
- Flash mint interest rate attack: For a lending protocol that offers a fixed interest rate but has no limit or rebalancing mechanism for changes in liquidity, an attack is possible in which a large amount of tokens is flash minted and deposited to drive the fixed interest rate extremely low, a loan is then taken at that low rate, and the originally minted tokens are burned.
- Arithmetic overflow and underflow: If there is no limit on the quantity of tokens that can be flash minted, anyone can flash mint a quantity of 2^256-1. If the receiving contract cannot safely handle this quantity, overflow or underflow can occur, so safeguards such as SafeMath should be considered.
- Treasury theft from coupling business functions: When flash mint functionality is combined with business-specific features on the same platform, it can lead to unintended consequences. For example, an attack has been reported in which a large amount of tokens is flash minted to repay collateral, while the stabilization fee is raised in the meantime to induce an undercollateralized state, and the vault is then liquidated.