---
eip: 3234
label: ERC-3234
title: Batch Flash Loans
status: Stagnant
type: Standards Track
category: ERC
created: 2021-01-31
official: https://eips.ethereum.org/EIPS/eip-3234
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-3234.md
canonical: https://eips.huatuo.xyz/zh/eip-3234/
lang: zh
tldr: 用于在一次调用中对多种代币发放闪电贷的出借方与借款方接口，是 ERC-3156 的多资产版本，适合一次性对多个仓位进行再融资。
tags: [defi, flash-loan, lending, interface, erc]
---

# ERC-3234: Batch Flash Loans

> This ERC provides standard interfaces and processes for multiple-asset flash loans.

**TL;DR:** 用于在一次调用中对多种代币发放闪电贷的出借方与借款方接口，是 ERC-3156 的多资产版本，适合一次性对多个仓位进行再融资。

## 它解决什么问题？

闪电贷无需抵押地借出代币，条件是在交易结束前归还（并支付费用）。[ERC-3156](/eip-3156/) 标准化了单一代币的闪电贷。但许多出借方可以从同一个合约借出多种代币，而一次性在协议之间迁移多个仓位需要同时用到多种资产。

ERC-3234 为这类批量闪电贷定义了一个独立但保持一致的标准，让借款合约可以与任何合规的出借方协作。

## 它如何运作

两个合约通过回调进行交互。

**出借方**（`IERC3234BatchFlashLender`）：

| 函数 | 用途 |
| --- | --- |
| `maxFlashLoan(tokens)` | 每种代币的最大借款额；对不支持的代币返回 0 |
| `flashFee(tokens, amounts)` | 每笔借款的费用；对不支持的代币会 revert |
| `batchFlashLoan(receiver, tokens, amounts, data)` | 发起借款，成功时返回 `true` |

**接收方**（`IERC3234BatchFlashBorrower`）：

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

`batchFlashLoan` 内部的流程：

1. 把每个 `tokens[i]` 的 `amounts[i]` 转给接收方。
2. 调用 `onBatchFlashLoan(msg.sender, tokens, amounts, fees, data)`，并要求它返回一个固定的魔术哈希。
3. 用 `transferFrom` 从接收方拉回每种代币的 `amounts[i] + fees[i]`，否则 revert。

因此，接收方必须在回调返回之前，为每种代币向出借方授权 `amount + fee`。还款采用拉取而非推送，这样出借方可以继续使用基于 `transferFrom` 的记账方式。

## 示例代码

一个最简的借款方（ERC-20 调用已简化；实际请使用 `SafeERC20`）：

```solidity
// 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;
    }
}
```

## 需要注意

- 状态为 **Stagnant**。实践中被广泛引用的是单一资产的 [ERC-3156](/eip-3156/)。
- 规范文本前后不一致：接口声明的是 `bytes[] data`，而示例使用的是 `bytes data`；所需的返回值既被称为 `ERC3234BatchFlashBorrower.onBatchFlashLoan`，也被称为 `ERC3156BatchFlashBorrower.onBatchFlashLoan`。请向你的出借方确认确切的哈希。
- 切勿盲目信任回调参数。要检查 `msg.sender` 是已知的出借方，`initiator` 是你信任的人，否则任何人都可以用伪造的数据触发你的回调。
- 出借方必须验证魔术返回值，否则留有未清除授权的合约可能通过费用被掏空。
- 闪电铸造的流动性可能极其庞大；规范给出了利率操纵和溢出攻击的例子，集成它的协议必须对此加以防御。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-3234
