# Finalizing Orders

Once a trade has been matched, the finalise button will become visible on the UX for both the counterparties to the trade, like so:<br>

<figure><img src="https://2294933522-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FewLyC4ny0ztkYYXokV4j%2Fuploads%2FUSF2tjiRSfTZiq9NcS0I%2FScreenshot%202023-04-07%20at%204.07.41%20pm.png?alt=media&#x26;token=55ea4490-4524-4af5-bb6b-0c3c2ce5f30c" alt=""><figcaption><p>Finalising orders using the GUI</p></figcaption></figure>

Similarly, users of the SDK will find that the "EventQListener" will detect when an order has been filled, and provide template transactions for "*FinaliseBid*" and "*FinaliseAsk*".\
\
Either user may then proceed to finalize the order. Upon clicking Finalize, the program logic checks that both the counterparties to the trade have enough tokens in their openorders accounts. If not, it attempts to transfer the appropriate amount of funds from the party to the program owned "vault". Once th\
If so, it proceeds with modifying the balances according to the trade terms, and then marks the trade as finalized (to prevent double spends). After finalization is completed, the user can instantly see their updated balances from their OpenOrdersPDA:<br>

<figure><img src="https://2294933522-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FewLyC4ny0ztkYYXokV4j%2Fuploads%2FT4ImR0eUiQFg1GmoJz92%2FScreenshot%202023-04-07%20at%204.10.42%20pm.png?alt=media&#x26;token=08cae3c1-5cd8-41bc-a1f6-441f6d4b9829" alt=""><figcaption><p>Updated Balances visible on GUI</p></figcaption></figure>

These funds will then available for withdrawal at any time. For a deeper dive into the logic executed at the `finalizeOrders` step, please see [just-in-time-liquidity](https://fermi-dex.gitbook.io/fermi-dex-docs/technical-overview/architecture/just-in-time-liquidity "mention").\
\
Example Finalise Transaction: <https://solscan.io/tx/2bSohxq73L9gouc4dmuQnkkd1uctNxMuNw2xmJms1RntvUNb5cdWt7NqMmpujhjPTGLZFMZxzFqVEJcyAzLGx1uH?cluster=devnet>


---

# 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://fermi-dex.gitbook.io/fermi-dex-docs/fermi-dex-usage-guide/fermi-dex-how-it-works/finalizing-orders.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.
