> For the complete documentation index, see [llms.txt](https://docs.bsc.lista.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.bsc.lista.org/zh-cn/kai-fa-zhe-zhuan-qu/collateral-debt-position/flash-loan.md).

# 闪电贷

> **lisUSD CDP 正在逐步关闭** — 请参阅[机制](/zh-cn/kai-fa-zhe-zhuan-qu/collateral-debt-position/mechanics.md)以获取实时信息。闪电铸造仍然有效，因此现有的集成可以继续运行。新的集成应使用 [Lista Lending](/zh-cn/kai-fa-zhe-zhuan-qu/lista-lending.md)，其 `Moolah.flashLoan` 在[合约参考](/zh-cn/kai-fa-zhe-zhuan-qu/lista-lending/contract-reference.md)中。

`flash.sol` 是一个标准的 **ERC-3156** 闪电*铸造器*：它将 lisUSD 铸造到您的合约中，回调您，并在同一交易结束时再次销毁它。没有抵押品和头寸——如果贷款加上费用在调用结束前未归还，一切将回滚。

**主网贷款人：** [`0x64d94e715B6c03A5D8ebc6B2144fcef278EC6aAa`](https://bscscan.com/address/0x64d94e715B6c03A5D8ebc6B2144fcef278EC6aAa)（[智能合约](/zh-cn/kai-fa-zhe-zhuan-qu/collateral-debt-position/smart-contract.md)页面上的 `FlashMinter`）

## 接口

```solidity
function maxFlashLoan(address token) external view returns (uint256);
function flashFee(address token, uint256 amount) external view returns (uint256);
function flashLoan(IERC3156FlashBorrower receiver, address token, uint256 amount, bytes calldata data) external returns (bool);
```

* **`token` 必须是 lisUSD。** 其他任何东西都会回滚 `Flash/token-unsupported`；`maxFlashLoan` 对其返回 `0`。
* `flashFee` 应用 `toll` 费率并返回一个 18 位小数的 wad。
* `data` 被逐字转发到您的回调中——这是传递状态的唯一通道。

## 编写借款人

实现 [`IERC3156FlashBorrower`](https://github.com/lista-dao/lista-dao-contracts/blob/master/contracts/interfaces/IERC3156FlashBorrower.sol) 并在 `onFlashLoan` 中执行三件事：拒绝不是贷款人的调用者，运行您的逻辑，并在返回 `keccak256("ERC3156FlashBorrower.onFlashLoan")` 之前批准贷款人的 `amount + fee`。

```solidity
pragma solidity ^0.8.10;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../interfaces/IERC3156FlashBorrower.sol";
import "../interfaces/IERC3156FlashLender.sol";

contract FlashBorrower is IERC3156FlashBorrower {
    IERC3156FlashLender lender;

    constructor(IERC3156FlashLender lender_) {
        lender = lender_;
    }

    function flashBorrow(address token, uint256 amount) public {
        bytes memory data = abi.encode(/* your parameters */);
        lender.flashLoan(this, token, amount, data);
    }

    function onFlashLoan(
        address initiator,
        address token,
        uint256 amount,
        uint256 fee,
        bytes calldata data
    ) external override returns (bytes32) {
        require(msg.sender == address(lender), "untrusted lender");
        require(initiator == address(this), "untrusted initiator");

        // ... 您的逻辑。它必须使此合约持有 `amount + fee`。

        IERC20(token).approve(address(lender), amount + fee);
        return keccak256("ERC3156FlashBorrower.onFlashLoan");
    }
}
```

返回除该哈希之外的任何内容，调用将回滚 `Flash/callback-failed`。跳过批准，或在 `amount + fee` 之前结束回调，贷款人的 `transferFrom` 将回滚 `LisUSD/insufficient-allowance` 或 `LisUSD/insufficient-balance`。

> **闪电贷无法竞标 CDP 清算。** 此分叉使拍卖买方有权限限制——`Clipper.take` / `redo` 是 `auth`，`Interaction.buyFromAuction` 是白名单。对第三方开放的清算面在 Lista Lending 上：请参阅[清算人集成](/zh-cn/kai-fa-zhe-zhuan-qu/lista-lending/liquidator-integration.md)。

## 来源

* [`flash.sol`](https://github.com/lista-dao/lista-dao-contracts/blob/master/contracts/flash.sol) — 包括 `max` 上限和 `toll` 费率的贷款人。
* [`flashBorrower.sol`](https://github.com/lista-dao/lista-dao-contracts/blob/master/contracts/mock/flashBorrower.sol) — 更完整的借款人，带有对解码 `data` 的分支。
* [`flash.test.js`](https://github.com/lista-dao/lista-dao-contracts/blob/master/test/flash.test.js) — 一个完整的端到端示例。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.bsc.lista.org/zh-cn/kai-fa-zhe-zhuan-qu/collateral-debt-position/flash-loan.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
