Performing a forced transaction (Forced withdrawal, forced trade) (StarkEx Perpetual Trading)

A user can always access their offchain funds from L1 by initiating a forced withdrawal, which sometimes requires a forced trade to precede it. The user initiates these operations onchain. In an extreme case, where the operator does not serve a forced action request within the freeze grace period, then any user, not just the user that submitted the request, can freeze the contract and withdraw their funds.

A valid onchain request activates a forced action request, but even if the corresponding offchain transaction itself is invalid, the application nonetheless must serve the request, either fulfilling the request, or proving that the request is invalid.

Forced transactions are irreversible. Neither you nor the user can cancel a forced trade or forced withdrawal request.

StarkEx Perpetual Trading includes the following forced actions:

  • Forced withdrawal

  • Forced trade

    It is recommended to first close a position with forced trade and only then to use forced withdrawal when there is only collateral left in the position. Otherwise the value of the synthetic asset at the time the user sends the forced withdrawal request might reduce the amount of colleral in the position to be lower than the requested amount, preventing the forced withdrawal request from being accepted.

For complete information on functions, events, and constants mentioned in this topic, see the Forced operations reference.

Performing a forced trade

Forced withdrawal can only withdraw collateral. When a user holds any synthetic assets and wants to withdraw the entire value of their position, they must first execute a forced trade to sell their synthetic assets in exchange for collateral.

The second position in a forced trade can belong to the same user.

Prerequisites
  • The private Stark key that matches the public Stark key of the relevant position.

  • The contract is not frozen.

Procedure
  1. This step is not necessary for StarkEx Perpetual Trading 1.0.
    The user calls the registerSender or registerEthAddress function.

  2. If the user must trade synthetic assets so that their position only holds collateral, they must find a party to trade with and agree on the terms of the trade.

    StarkEx Perpetual Trading 2.0 and later

    Both sides of the trade must be registered, so if the other party is not yet registered, they must also call the registerSender or registerEthAddress function.

  3. After agreeing on the terms of the trade, one party calls the onchain forcedTradeRequest function, which includes the signature of the second party.

    The operator must serve the request within the amount of time defined by the FREEZE_GRACE_PERIOD constant. The default time period is seven days.

    After receiving the function call, the StarkEx smart contract emits the LogForcedTradeRequest event.

    If the operator serves the request, and the request is proven to be valid, the user can subsequently execute a forced withdrawal request, and the next step is not necessary. See Performing a forced withdrawal.

  4. If the freeze grace period passes, and the forced operation is still in the pending forced operations area, see Freezing the operator.

Performing a forced withdrawal

Prerequisites
  • The private Stark key that matches the public Stark key of the relevant position.

  • The contract is not frozen.

Procedure
  1. The user calls the forcedWithdrawalRequest function.

    The operator must serve the request within the amount of time defined by the FREEZE_GRACE_PERIOD constant.

    After receiving the function call, the StarkEx smart contract emits the LogForcedWithdrawalRequest event.

    If the operator serves the request, the user can subsequently withdraw their funds to an Ethereum position, and the next step is not necessary.

  2. If the freeze grace period passes, and the forced operation is still in the pending forced operations area, see Freezing the operator.