# Order Matching

In a decentralized system like Yamata, orders can be processed both **off-chain** (via a sequencer) and **on-chain** (directly recorded on the blockchain). The role of the **Validating Nodes (VNs)** is to ensure that all transactions are processed fairly and consistently across these two parallel paths.

The process of matching and verifying transactions involves several potential scenarios, which we’ll break down below.

<figure><img src="/files/KNskdEp4nH90z990rUQY" alt=""><figcaption><p>Order Matching</p></figcaption></figure>

## **Matching Off-Chain and On-Chain Orders**

In all cases, an order will be processed by the **off-chain sequencer (Submission A)** and will also appear **on-chain (Submission B)**. The VNs are responsible for ensuring that these two versions of the transaction match. However, because there is no **globally coordinated time** between off-chain and on-chain transactions, we cannot rely on on-chain ordering for verification. **Instead:**

### **Off-Chain Sequencer (*****Submission A*****)**

Handles the orders based on when they arrive at the API. It maintains the order of transactions as they are received and processes them accordingly.

### **On-Chain Orders (*****Submission B*****)**

Ensures that the transactions are **included** on the blockchain, but it cannot guarantee the same ordering as the off-chain sequencer. On-chain ordering can be influenced by gas fees and miner decisions, making it less reliable for verifying the exact sequence.

**How it Works:**

1. The **sequencer** is the authoritative source for order sequencing. The VNs verify that the **on-chain orders match the off-chain orders**, but they do not compare the exact order between the two systems since the blockchain does not offer an objective time ordering.
2. If there is a mismatch in the **content** (not the order) of the transaction between the sequencer and on-chain, the VNs can raise a **challenge** to investigate further.

***

## **Off-Chain Discrepancies**

In this scenario, we are not dealing with on-chain orders, but rather a discrepancy in how the sequencer itself has processed two or more off-chain orders. For example, two orders may arrive off-chain in a particular sequence, but the sequencer may process them out of order.

**Example**:

* **Order A**: Sell **1 BTC** for **1 ETH**.
* **Order B**: Sell **1 ETH** for **1 BTC**.
* **Order C**: Sell **1 ETH** for **1 BTC**.

Order A could match either Order B or Order C, and the correct match depends on which order, B or C, arrived first at the sequencer. If **Order B** arrived first at the API, it should be processed before Order C. However, if the sequencer mistakenly processes **Order C first**, this is a discrepancy that the VNs must address.

**How it Works:**

1. The VNs monitor the **off-chain sequencing** of transactions. If they detect that the sequencer processed orders out of their correct order (e.g., processed **Order C** before **Order B** when **B** arrived first), they can raise a **challenge**.
2. The challenge process ensures that the sequencer cannot manipulate the order of transactions and that everything is processed fairly based on the time of arrival at the API.

***

## **Ensuring No Exclusion of Orders**

Another role of the **on-chain mechanism** is to ensure that no valid orders are excluded by the sequencer. While the **sequencer** is trusted to process orders efficiently off-chain, the **on-chain mechanism** acts as a safeguard to make sure that no orders are left out of the system entirely.

Even if the **order of transactions** on-chain doesn’t match the sequencer’s order, as long as all orders are **included**, the system remains fair.

**How it Works:**

1. The primary goal of the on-chain system is to make sure that no order is excluded. If the sequencer fails to post an order on-chain, the VNs will detect this and raise a challenge.
2. This process ensures that the sequencer cannot simply ignore or exclude orders, while it is in control of the off-chain order processing.

***

## **Cycle of Validation**

As long as the orders from the sequencer and on-chain match, the VNs continue to validate each **epoch** in a circular fashion, simply ensuring that everything is correct and moving on to the next batch.&#x20;

<figure><img src="/files/isvZUBxVufdc1zV7FtDu" alt=""><figcaption><p>Cycle of Validation Flow</p></figcaption></figure>

However, when the VNs detect a discrepancy, whether in the order, content, or exclusion of transactions, they immediately move into the [**challenge process**](/yamata-nodes/order-validation/challenge-process.md) to investigate and resolve the issue.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.yamata.io/yamata-nodes/order-validation/order-matching.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
