Skip to main content

Order Execution

The order bot consists of a Rust executable that is continuously notified of any perpetual exchange EVM events, notably CreateOrder and PriceUpdate events. After being notified, it uses our Query Engine to fetch any newly available EVM events emitted by exchange contracts up to the latest block.

Operation

The order bot will perform an "update cycle" each time a relevant perpetual exchange event is emitted on-chain. On each update cycle,

  1. All exchange EVM events since the last update cycle block number are imported into the database via the Query Engine.
  2. The order bot identifies all unexpired, pending orders on the exchange that reference an active exchange product. This query runs locally in the database.
  3. For orders that specify a price trigger condition, the order bot checks if that product's price, published by the oracle, meets the order's price trigger condition.
  4. For orders that specify a % PnL trigger condition, the order bot will check if the target position's latest % PnL has met the trader's % PnL trigger condition. If the target position is not yet open, the order will remain pending until it expires. The PnL calculation uses the logic on-chain to ensure there is no discrepancy.
  5. Orders that are ready to execute are submitted on-chain via OrderBookV1.executeOrders. At this point, it's possible that an otherwise-valid order might fail to execute for external reasons, such as exchange risk thresholds. Failing order executions will be retried several times, but if they fail to execute more than a certain number of times they will be marked as locally excluded.