Implementing Frequent Batch Auction on SUAVE
category
Application
date
May 14, 2024
slug
implementing-frequent-batch-auction-on-suave
type
Post
lang
en
status
Published
Cross post with Flashbots Collective.
In this article, we delve into the implementation of a Frequent Batch Auction (FBA) on SUAVE.
The FBA mechanism involves users placing and canceling orders, with an operator executing batch fills at regular intervals.
We explore the main functions and data structures involved in this process, with a special focus on the executeFills function.
You can find the code in this GitHub repository:
suave-fbadex
titania-research-labs • Updated Oct 29, 2024
Overview of the FBA Process
The FBA process revolves around three main functions:
placeOrder
: Allows users to place orders in the order book.
cancelOrder
: Enables users to cancel their previously placed orders.
executeFills
: Executed by the operator at regular intervals to match and fulfill orders.
Key Roles and Interaction Diagram
We can illustrate the interaction between the main participants
- Users: the users who place and cancel orders
- Operator: the entity responsible for executing fills
- Contract: the smart contract that manages the order book and fills
- Storage: Confidential Data Store that stores orders and the contract storage that stores cancels
Data Structures
The primary data structures involved in the FBA process include the
Fill
, Cancel
, PlaceResult
, and CancelResult
structs.Order
: Represents the order with price, amount, side, and order ID.
Fill
: Represents the filled orders with price and amount.
Cancel
: Represents the canceled orders with order ID and side.
FillResult
: Represents the result of a filled order with price, side, and amount.
CancelResult
: Represents the result of a canceled order with order ID and side.
The placeOrder
/ cancelOrder
Functions
The
placeOrder
function allows users to submit their buy or sell orders to the order book. Depending on the order side (buy or sell), the order is inserted into the appropriate heap (bids or asks). The FBAHeap
library is used to manage the order book efficiently and stores the orders and their metadata on Credential Data Store.The
cancelOrder
function allows users to cancel their previously placed orders. The function stores the cancellation details in an array which will be processed during the executeFills
function. The cancellations are stored in the contract storage, not in the FBAHeap
.The executeFills
Function
The
executeFills
function is the heart of the FBA process. It is responsible for matching and filling orders. It consists of three main parts:- Processing Cancel Orders: Cancels any orders that were marked for cancellation.
- Matching Orders: Matches buy and sell orders based on the clearing price.
Detailed Implementation of executeFills
The
executeFills
function first processes the cancel orders by deleting the canceled orders from the order book. It then matches the buy and sell orders based on the clearing price. The function iterates through the orders, updating the order book and fills array accordingly.Conclusion
In this article, we explored the implementation of a Frequent Batch Auction (FBA) on SUAVE. We discussed the main functions involved in the FBA process, the key data structures used, and the detailed implementation of the executeFills function. For more information about the code, you can check out the GitHub repository:
And this is the article on future work: