---
eip: 6787
label: ERC-6787
title: Order Book DEX with Two Phase Withdrawal
status: Draft
type: Standards Track
category: ERC
created: 2023-03-27
official: https://eips.ethereum.org/EIPS/eip-6787
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-6787.md
canonical: https://eips.huatuo.xyz/zh/eip-6787/
lang: zh
tldr: 一份面向订单簿 DEX 的草案接口，增加了两阶段强制提款：用户随时都能取出资金，但需要经过一个窗口期，让交易所结算其未平仓头寸。
tags: [defi, dex, order-book, withdrawal, layer2]
---

# ERC-6787: Order Book DEX with Two Phase Withdrawal

> An order book-based DEX Interface that ensures the asset security of both users and the exchange

**TL;DR:** 一份面向订单簿 DEX 的草案接口，增加了两阶段强制提款：用户随时都能取出资金，但需要经过一个窗口期，让交易所结算其未平仓头寸。

## 它解决什么问题？

运行在 Layer 2 上的订单簿交易所通常把用户存款放在合约里，并在链下撮合交易。用户需要一个逃生通道：如果运营方不再配合，他们必须能够在链上强制取出自己的资金。但简单的强制提款可能被滥用。持有亏损永续合约头寸的交易者可以在亏损结算之前抽走抵押品，让交易所来承担损失。

ERC-6787 为这类 DEX 提出了一个通用接口，把强制提款拆成两步，从而同时保护双方。

## 它如何运作

该接口有四个函数和一个事件：

| 函数 | 用途 |
| --- | --- |
| `deposit(token, amount)` | 把代币存入交易所合约 |
| `withdraw(token, amount)` | 正常的协作式提款 |
| `prepareForceWithdraw(token, amount)` | 阶段 1：声明强制提款；返回唯一的 `requestID` |
| `commitForceWithdraw(requestID)` | 阶段 2：等待期过后执行强制提款 |

阶段 1 成功时必须触发 `PrepareForceWithdraw(user, tokenAddress, amount)`。

两个阶段之间，交易所有一个超时窗口，用来检查用户账户、强制取消未成交订单并结算交易。之后，用户可以不依赖运营方，直接提交提款。每个请求都有自己的 `requestID`，因此用户可以并行发起多个请求。该设计假设交易所在合约中以 1:1 的比例保留所存资产的储备。

## 示例代码

```solidity
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;

interface IOrderBookDEX {
    event PrepareForceWithdraw(address indexed user, address indexed tokenAddress, uint256 amount);

    function deposit(address token, uint256 amount) external;
    function withdraw(address token, uint256 amount) external;
    function prepareForceWithdraw(address token, uint256 amount) external returns (uint256 requestID);
    function commitForceWithdraw(uint256 requestID) external;
}
```

用户流程（ethers v6）：

```ts
// Simulate first to learn the requestID the call will return, then send it.
const requestId = await dex.prepareForceWithdraw.staticCall(usdc, amount);
await (await dex.prepareForceWithdraw(usdc, amount)).wait();

// ...after the exchange's waiting period has elapsed:
await (await dex.commitForceWithdraw(requestId)).wait();
```

## 需要注意

- 状态为 **Draft**。规范很简短，许多事项留给实现自行决定。
- 等待期的长度、交易所在此期间如何结算头寸，以及它没有结算时会发生什么，均未规定。
- prepare 事件不包含 `requestID`，因此链下工具无法仅凭日志把它与之后的 commit 对应起来。示例中的 `staticCall` 技巧只有在下一个 ID 可预测时才有效；真实的集成应从实现自己的事件或 view 函数中获取。
- 规范中的安全注意事项标注为"needs discussion"。任何实现都应视为需要仔细审计。


---

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