---
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/eip-6787/
lang: en
tldr: "A draft interface for order-book DEXs that adds a two-phase forced withdrawal: users can always get their funds out, but only after a window in which the exchange settles their open positions."
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:** A draft interface for order-book DEXs that adds a two-phase forced withdrawal: users can always get their funds out, but only after a window in which the exchange settles their open positions.

## What problem does it solve?

Order-book exchanges running on Layer 2 typically hold user deposits in a contract and match trades off-chain. Users need an escape hatch: if the operator stops cooperating, they must be able to force their money out on-chain. But a naive forced withdrawal is abusable. A trader sitting on a losing perpetual position could yank their collateral before the loss is settled, leaving the exchange to absorb it.

ERC-6787 proposes a common interface for such DEXs, with a forced withdrawal split into two steps so both sides are protected.

## How it works

The interface has four functions and one event:

| Function | Purpose |
| --- | --- |
| `deposit(token, amount)` | Move tokens into the exchange contract |
| `withdraw(token, amount)` | Normal, cooperative withdrawal |
| `prepareForceWithdraw(token, amount)` | Phase 1: announce a forced withdrawal; returns a unique `requestID` |
| `commitForceWithdraw(requestID)` | Phase 2: execute the forced withdrawal once the waiting period has passed |

`PrepareForceWithdraw(user, tokenAddress, amount)` must be emitted when phase 1 succeeds.

Between the two phases the exchange has a timeout window to check the user's account, forcibly cancel open orders and settle trades. After that, the user can commit the withdrawal directly without the operator's help. Each request has its own `requestID`, so a user can run several in parallel. The design assumes the exchange keeps a 1:1 reserve of deposited assets in the contract.

## Sample code

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

User flow (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();
```

## Things to know

- Status is **Draft**. The spec is short and leaves many things to the implementation.
- The length of the waiting period, how the exchange settles positions within it, and what happens if it doesn't, are not specified.
- The prepare event does not include the `requestID`, so off-chain tooling cannot match it to a later commit from logs alone. In the sample, the `staticCall` trick only works if the next ID is predictable; a real integration should get it from the implementation's own event or view function.
- Security considerations in the spec are marked "needs discussion". Treat any implementation as requiring careful audit.


---

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