Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

Background

This article explores ERC6787, which proposes a two-phase withdrawal mechanism for order book DEXs so that users can reliably withdraw their own assets.

This article translates, summarizes, and supplements the material below.

I also cover a range of other EIPs, BIPs, SLIPs, CAIPs, ENSIPs, RFCs, and ACPs.

Overview

The Order Book DEX Standard defines a common interface for decentralized exchanges (DEXs) that use order books to trade assets. It specifies fundamental functionality for depositing assets, withdrawing them, and trading, allowing different developers and projects to operate using the same mechanism.

A key feature is a proposed two-phase forced withdrawal mechanism designed to protect both users' and exchanges' assets. It allows users to withdraw their assets safely even if they cannot trust the exchange. At the same time, it aims to protect the exchange's assets by preventing users from abusing forced withdrawals to evade losses.

By standardizing Layer 2 order book DEXs, this specification aims to make it easier for developers to build interoperable exchanges, with the expectation of improving liquidity and usability across the network.

Motivation

Layer 2 order book DEXs have attracted attention as a way to enable fast trading while reducing on-chain processing costs. However, without a common interface, projects have historically used inconsistent approaches to asset management and withdrawals, making it difficult for users to assess their safety.

The primary motivation for the Order Book DEX Standard is to standardize the essential functions that protect users' assets. By unifying critical asset management operations such as deposits, withdrawals, and forced withdrawals, developers can build secure DEXs in a consistent manner.

ERC6787 also introduces a two-phase forced withdrawal mechanism. The goal is to balance user protection with exchange protection. Layer 2 DEXs are assumed to maintain assets with 1:1 reserves and use decentralized clearing and settlement mechanisms. Ordinary forced withdrawals are important for protecting users, but allowing them without restrictions creates a risk that users could withdraw funds to evade losses incurred in futures trading, particularly perpetual contracts.

The two-phase approach addresses this by introducing an initial stage for acknowledging a user's forced withdrawal request. After a specified verification period, the user can finalize the forced withdrawal if no issues remain. This mechanism protects both the user's assets and the exchange's ability to recover losses owed to it.

The two-phase approach draws on the two-phase commit protocol, a mechanism used to maintain data consistency in distributed systems. Applying the idea of preventing inconsistent intermediate transaction states to asset management in Layer 2 DEXs improves safety and robustness.

Ultimately, the Order Book DEX Standard aims to improve safety for both users and exchanges while addressing existing challenges such as insufficient liquidity and inefficient price discovery. By allowing developers to build DEXs on a common foundation, it aims to improve overall reliability and usability and create an environment that more users can trust.

Specification

Events

PrepareForceWithdraw
javascript
event PrepareForceWithdraw(address indexed user, address indexed tokenAddress, uint256 amount);

Emitted when a user successfully calls the forced withdrawal preparation function. It is emitted when the user calls prepareForceWithdraw and the exchange accepts the forced withdrawal request. External systems and frontends can use it to detect new forced withdrawal requests. The event contains the user's address, the target token address, and the requested withdrawal amount. Because forced withdrawals have two phases, this event is important evidence that the first phase has been accepted.

Parameters

  • user
    • The address of the user who initiated the forced withdrawal.
  • tokenAddress
    • The address of the token contract from which assets are to be withdrawn.
  • amount
    • The amount of tokens requested for forced withdrawal.

Functions

deposit
javascript
function deposit(address token, uint256 amount) external;

Deposits the specified token into the exchange. Before trading on the exchange, users must deposit the relevant tokens into the contract. This function takes the token contract address and amount and moves the assets into an account managed by the exchange. After the deposit, the exchange updates the user's internal balance so that the assets can be used for orders and trades.

Arguments

  • token
    • The contract address of the token to deposit.
  • amount
    • The amount of tokens to deposit.
withdraw
javascript
function withdraw(address token, uint256 amount) external;

Withdraws the specified token from the exchange. Users call this function when they want to move assets from the exchange back to their own wallets. Based on the specified token and amount, it returns the actual tokens from the user's internal exchange balance to their wallet. An ordinary withdrawal can be performed freely by the user and is not a forced withdrawal.

Arguments

  • token
    • The contract address of the token to withdraw.
  • amount
    • The amount of tokens to withdraw.
prepareForceWithdraw
javascript
function prepareForceWithdraw(address token, uint256 amount) external returns (uint256 requestID);

Starts the first phase of a forced withdrawal. Layer 2 order book DEXs provide a forced withdrawal mechanism for situations in which users cannot trust the exchange. This function serves as an advance notice of forced withdrawal, expressing the user's intention to withdraw the specified token and amount. Because the mechanism uses two phases, the user first submits a request with this function. During the subsequent timeout period, the exchange checks the user's orders and, if necessary, forcibly cancels orders or performs settlement. Since multiple forced withdrawals may be processed concurrently, each request is assigned a unique requestID.

Arguments

  • token
    • The contract address of the token targeted by the forced withdrawal.
  • amount
    • The amount the user wishes to withdraw.

Return value

  • requestID
    • An ID that uniquely identifies the forced withdrawal request.
commitForceWithdraw
javascript
function commitForceWithdraw(uint256 requestID) external;

Finalizes a two-phase forced withdrawal. The caller specifies the requestID issued by prepareForceWithdraw to complete the forced withdrawal. If the exchange has checked orders and performed settlement within the timeout period and no issues remain, calling this function returns the assets to the user. This allows users to recover their assets without requiring the exchange's cooperation, protecting assets in the Layer 2 environment.

Arguments

  • requestID
    • The ID identifying the forced withdrawal request.

Additional Explanation

Two-phase forced withdrawal sequence for an order book DEX https://eips.ethereum.org/EIPS/eip-6787

The original diagram is retained and may contain Japanese labels.

This sequence illustrates how a user of a Layer 2 order book DEX performs a two-phase forced withdrawal. Forced withdrawals are important when the exchange backend stops responding or when a user wants to protect their assets immediately.

Communication proceeds through Alice → DEX Front → Wallet → Alice's Account Abstraction (AA) → the DEX's Account Abstraction (DEX AA).

1. The User Connects to the DEX

Alice first opens the DEX frontend. She connects her wallet and subscribes so that her account can receive DEX-related operations.

2. Ordinary Deposit (deposit)

Alice deposits assets into the DEX.

  1. The DEX frontend sends Alice's operation (UserOp) to the Wallet.
  2. The Wallet forwards the operation for Alice to her AA.
  3. AliceAA sends deposit to DEX AA to deposit the assets.
  4. DEX AA returns the result, completing the deposit.

At this point, Alice can trade normally.

3. Requesting a Forced Withdrawal (prepareForceWithdraw)

Alice now begins preparing a forced withdrawal.

  1. Alice requests a forced withdrawal through the frontend.
  2. The operation proceeds through Wallet → Alice AA, and Alice AA sends prepareForceWithdraw to DEX AA.
  3. DEX AA accepts the request and issues and returns a unique requestID.

This requestID is required later when the forced withdrawal is executed.

4. Exchange Checks (Order Status Verification and Settlement)

After accepting prepareForceWithdraw, the DEX checks whether the user is attempting to abuse the forced withdrawal mechanism.

Typical checks include the following.

  • Whether the user has outstanding orders that have not been settled.
  • Whether there are unaccounted-for losses (particularly on perpetual futures).
  • Whether any assets remain available for withdrawal after netting against the reserves.

Orders are canceled or balances adjusted if necessary. Once the checks are complete, the DEX notifies the frontend.

5. Waiting for the Timeout

Because forced withdrawals use a two-phase mechanism, users cannot withdraw assets immediately after calling prepareForceWithdraw.

The reasons for providing a timeout period are as follows.

  • To give the exchange time to check and update the user's order status.
  • To prevent users from abusing forced withdrawals to avoid losses.
  • To ensure consistency using the same concept as Two-Phase Commit.

After this period elapses, the process can advance to the forced withdrawal execution phase.

6. Executing the Forced Withdrawal (commitForceWithdraw)

After the timeout, Alice calls commitForceWithdraw.

  1. The operation passes through フロント → Wallet → Alice AA (frontend → wallet → Alice AA).
  2. Alice AA sends commitForceWithdraw(requestID) to DEX AA.
  3. DEX AA processes the corresponding forced withdrawal request and ultimately returns the assets to Alice's wallet.

This completes the two-phase forced withdrawal.

Citation

Jessica (@qizheng09), Roy (@royshang), Jun (@SniperUsopp), "ERC-6787: Order Book DEX with Two Phase Withdrawal [DRAFT]," Ethereum Improvement Proposals, no. 6787, March 2023. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-6787.

Conclusion

In this article, we explored ERC6787, which proposes a mechanism for reliably withdrawing users' assets from order book DEXs by dividing withdrawals into two phases. I hope you found it useful.

If you have questions, feel free to contact me via Twitter DM.

Twitter @cardene777

I also share information on other platforms, so feel free to explore them.

https://chaldene.net/