The GSY DEX Matching Engine: Critical Component of the Grid Singularity Decentralised Exchange
This article is part of a series of publications on the Grid Singularity Decentralised Exchange (GSY DEX), including prior two publications, one delineating the symbiotic energy markets concept that underpins the GSY DEX architecture, and another describing the GSY DEX system components and the core on- and off-chain communication structure.
1. GSY DEX Matching Engine Overview
The GSY Matching Engine is a critical component of the Grid Singularity Decentralised Exchange (GSY DEX) purposed to efficiently and securely match the select market supply with demand, based on participant preferences and market conditions. Whilst it can be used for any commodity, the GSY Matching Engine has been tailored for the electricity market. It integrates directly with the GSY Off-Chain Storage for real-time bid and offer access via its Order Book Storage, and with the GSY Node for trade settlement in conjunction with a trade execution (energy delivery) verification service provided by the GSY DEX Execution Engine. The GSY Matching Engine, published under GPL v.3 licence on the GitHub repository and documented in the Grid Singularity wiki, currently deploys a pay-as-bid strategy, ensuring that buyers and sellers receive the best possible prices for their energy trades. Future versions will integrate pay-as-clear and automated market maker (AMM) as alternative trading mechanisms, based on the espoused development path. Furthermore, the intent is to render the GSY Matching Engine available as an autonomous service application in the Energy Web SmartFlow Launchpad, leveraging the Energy Web infrastructure and facilitating integration with a wider range of third-party applications, even beyond the energy market.
Multi-attribute auctions, which account for participant preferences and applicable market conditions, require more advanced clearing algorithms than single attribute auctions, such as those based on price alone. The optimal approach is to engage off-chain workers to operate matching algorithms and then require either the use of trusted execution environments (enclaves) or on-chain verification to determine whether the matching algorithm was correctly executed. To enable this solution, the matching process is decoupled from the settlement verification and moved to a dedicated component, the Matching Engine, as explained in our previous Medium article that describes the key GSY DEX system components and the core on- and off-chain communication structure. The GSY DEX Matching Engine has been designed to operate as (one or multiple) individual autonomous service(s), responsible for identifying suitable matches for multiple open order books. To manage the available GSY DEX Matching Engine services, the GSY Node needs to register each such service and associate it with a user account responsible for operating the respective service. Grid Singularity will facilitate the Matching Engine registration process for the GSY DEX.
The GSY DEX Matching Engine reads open bids and offers through the GSY DEX Matching API and submits matches for validation based on the process determined for the Grid Singularity Exchange. Since the exchange’s verification function (part of GSY Node) only checks the validity of each match rather than the result of a specific matching algorithm, there is no longer a need for an agreed matching algorithm, arbitration technique, or redundant off-chain worker computation. This alleviates the need for additional on-chain computational cost, avoiding a corresponding increase to the gas costs associated with matching. Moreover, the GSY Node verification function is equipped to ascertain whether the bid and offer limitations are satisfied by the matches generated by the GSY DEX Matching Engine (for instance, if the clearing energy rate is lower than or equal to the bid rate and higher than or equal to the offer rate). Therefore, multiple competing GSY Matching Engines can calculate matches for different markets in parallel, each using their own matching algorithm, and afterwards the GSY Node can select and verify the matches that are eligible for clearing and reject those that are violating their bid and offer requirements. Consequently, different matching algorithms can be applied in different energy communities, or in different buildings within an energy community to optimise their asset configuration and energy sharing preferences.
Finally, as the verification function is executed on-chain any potential errors or fraud attempts, such as double counting of trades and matching results, are eliminated. The validation of the actual delivery of the traded energy cannot be performed at this time, since the energy is traded ahead-of-time. Another GSY DEX service, the GSY Execution Engine, is tasked with reading the asset measurements of produced or consumed energy, thereby validating whether the energy delivery corresponds to the traded energy.
The main functions of the GSY DEX Matching Engine are the following:
- Order Retrieval: The GSY DEX Matching Engine retrieves orders from the GSY DEX Off-Chain Storage. This storage system holds all active bids and offers, which the GSY DEX Matching Engine reads to match suitable counterparts for energy trades.
- Matching Process: Pursuant to the pay-as-bid strategy, the GSY DEX Matching Engine pairs bids and offers according to price and quantity to maximise value for the exchange participants. Buy orders are organised in a descending order of price, prioritising buyers willing to pay more, while sell orders are arranged in an ascending order, prioritising sellers willing to sell energy at lower prices. This strategy ensures that both parties upon clearance pay or receive the desired price for their energy trade based on their submitted bids and offers.
- Submission to GSY Node: Once the GSY DEX Matching Engine has identified the optimal matches, it submits these matches to the GSY Node for further processing and execution. This seamless integration with the GSY Node and the GSY DEX Off-Chain Storage allows for efficient energy trading by offloading the GSY Node from the most computationally intensive operations of the matching process. At the same time, the immutability and integrity of the data are not jeopardised, since the GSY Node also participates in the verification process. Lastly, the GSY Execution Engine verifies that the energy trades correspond to the energy delivery.
2. GSY DEX Matching Engine and API Implementation
The GSY DEX Matching Engine is implemented in Rust, in order to take advantage of the ease of interconnection with other components of the GSY DEX, especially with the GSY Node, and the Rust performance benefits compared to other programming languages. The GSY DEX Matching Engine’s operation revolves around a loop that continuously submits requests to the GSY DEX Off-Chain Storage as to whether new open offers or bids have been created. If the Off-Chain Storage reports that new offers and bids have been created, the Matching Engine calls the applicable matching algorithm to calculate the matches and then transmits the calculated matches to the GSY Node. The main Rust modules that comprise the Matching Engine are the Algorithms Module and the Connectors Module, as illustrated in Figure 1 below, and further explained in the Grid Singularity wiki. Notably, the GSY DEX Matching Engine has been customised for the energy market by setting the traded commodity to energy and using kWh for the traded volume and cents/kWh for the trade rate and adapting other relevant parameters.
The GSY DEX Matching API defines the protocol of communication between the Matching Engine and other components of the GSY DEX, following the defined process for the Grid Singularity Engine. The API has two components, one for the inputs required by the Matching Engine to calculate the possible bid-offer matches, and one for transmitting the calculated matches to the GSY Node, for energy trades to be generated and trade settlement to be performed. The inputs required by the GSY DEX Matching Engine are the open bids and the offers that are available for a market slot (energy delivery time period). These are retrieved not via the GSY Node, but by leveraging the API of the GSY DEX Off-Chain Storage, thereby minimising the gas cost required for the operation of the Matching Engine, and consequently decreasing the computational effort required from the GSY Node. The bid-offer matches calculated by the GSY DEX Matching Engine are transmitted to the GSY Node via the Trade Settlement Pallet, which provides a decentralised trade verification functionality that allows its users to:
- Verify whether bid-offer matches can result in an energy trade and whether any of the matches violate a mandatory requirement of the bid or offer (e.g. the clearing rate of a match may violate the bid or offer energy rate).
- Generate the energy trade and perform the financial clearing between the buyer and the seller of energy; and
- Transmit the trade to the Off-Chain Storage for any further processing (e.g. by the Analytics Engine to generate performance indicators).
Moreover, the GSY DEX Matching API supports partial offer / bid matching, meaning that the energy from an offer or bid can also be matched partially. Its Trade Settlement Pallet is equipped to update the respective bid / offer with the residual energy (energy that remains after the trade was performed) and use the residual energy for matches that are processed at a later time, either in the same or in one of the following lists of matches.
3. Conclusion: GSY DEX Matching Engine Optimal Performance Design
To summarise, the GSY DEX Matching Engine is designed as an autonomous GSY DEX service, applied in conjunction with other services of the decentralised Grid Singularity Exchange, to achieve the following process optimisation:
- Enhanced Performance: The matching process is a computationally and memory-intensive operation that requires significant computational resources. By creating a dedicated matching engine, we ensure that the GSY Node can continue to handle other platform functions without being overloaded by the resource-intensive matching process.
- Gas/Transaction Costs: By creating a dedicated matching engine that interacts with the off-chain storage for the most computationally intensive operations rather than using the GSY Node to execute the entire matching process on-chain, transaction costs are significantly reduced.
- Scalability: The GSY DEX Matching Engine independent architecture ensures the platform remains responsive and efficient as the volume of market participants and transactions expands without affecting the performance of the GSY Node.
- Flexibility: As a standalone service, the GSY DEX Matching Engine can be easily updated or modified without impacting other platform components. This allows for the continuous improvement of the order matching process and overall energy trading experience for platform users. Moreover, the GSY DEX Matching Engine enables operation of multiple different matching algorithms in parallel, thus providing the flexibility to deploy different matching algorithms per market or energy community. This facilitates compliance with local regulation, while not restricting Community Managers to a single matching process, allowing them to tailor the matching process to their needs.
Authored by the Grid Singularity team: Spyridon Tzavikas, Ana Trbovich and Ewald Hesse
Note: The development of the GSY DEX Matching Engine v.1 has been supported in part by the European Union’s FEDECOM project.