ERC-6787 Two-phase withdrawal order book DEX standard (Order Book DEX with Two Phase Withdrawal)
Draft · Vaults & DeFi · created 2023-03-27
Defines a common interface for how order book decentralized exchanges (DEXs) handle deposits, withdrawals, and forced withdrawals. The core is a two-phase forced withdrawal that protects the assets of both users and the exchange.
Why it is needed
Decentralized exchanges gained popularity because users can control their assets directly, but existing approaches suffered from low liquidity and inefficient price discovery. As an alternative, order book DEXs that stack bids and asks on a ledger on Layer2 and match trades emerged, but without a standard interface to implement this, every exchange had to build it differently. Forced withdrawal is another problem. Protecting users requires a forced withdrawal function that lets them pull assets even if the exchange halts, but a malicious user who has taken a loss on a perpetual contract trade could abuse this function to dodge loss settlement. In other words, a safeguard meant to protect users could instead become a means of attacking the exchange. This standard tries to solve both problems at once by proposing a common interface together with a two-phase forced withdrawal procedure.
What to implement
function deposit(address token, uint256 amount) external;: Deposits the specified amount of the specified token into the exchange.function withdraw(address token, uint256 amount) external;: Withdraws the specified amount of the specified token from the exchange.function prepareForceWithdraw(address token, uint256 amount) external returns (uint256 requestID);: A phase-one function that initiates a forced withdrawal. It returns a unique requestID for each request, and the withdrawal can be executed once the timeout period has passed.function commitForceWithdraw(uint256 requestID) external;: A phase-two function that actually executes the forced withdrawal corresponding to a requestID once conditions are met.event PrepareForceWithdraw(address indexed user, address indexed tokenAddress, uint256 amount);: An event that must be emitted whenever a user successfully calls prepareForceWithdraw.
In depth
Basic deposit and withdrawal interface
deposit(address token, uint256 amount) deposits the specified amount of the token at the token address into the exchange, and withdraw(address token, uint256 amount) withdraws it the same way. Assets a user deposits are held in the exchange contract's account, and this structure lets the exchange prove 1:1 reserves in real time.
prepareForceWithdraw and requestID
prepareForceWithdraw(address token, uint256 amount) is the phase-one call announcing the intent to force a withdrawal. The withdrawal can be submitted after the default timeout period has passed. Since a single account may initiate multiple phase-two forced withdrawals in parallel, each request must return a unique requestID, which is used to call commitForceWithdraw afterward.
The exchange's role during the timeout period
Within the timeout period, the exchange must verify that the user's order status meets the expected criteria, and must forcibly cancel the user's orders and settle trades. This review step is what prevents an attack in which a user who has taken a loss on a perpetual contract uses forced withdrawal to evade that loss.
Conditions for executing commitForceWithdraw
commitForceWithdraw(uint256 requestID) actually executes the forced withdrawal once conditions are met. The request referenced by requestID must have first been initiated through prepareForceWithdraw. Once the forced withdrawal review period ends, the user can submit the request directly to complete the withdrawal.
The PrepareForceWithdraw event
The PrepareForceWithdraw(address indexed user, address indexed tokenAddress, uint256 amount) event MUST be emitted whenever a user successfully calls prepareForceWithdraw. External observers can use this event to track which user is trying to forcibly withdraw which token and how much.
Background on the two-phase commit design
Two phase commit is a distributed consistency protocol used to guarantee data safety and consistency in distributed systems. This standard combines a 1:1 reserve strategy, a decentralized liquidation and settlement interface, and a forced withdrawal function for Layer2 order book DEXs, and applies the same two-phase structure to withdrawals in order to prevent abuse of forced withdrawal. The original Rationale section presents a diagram of the two-phase withdrawal sequence.
Proposal status
This proposal is in Draft status. The content may still change, and the original Security Considerations section states only that it 'Needs discussion.' It is stated that no backwards compatibility issues have been found.