# Demo - Fermi v0.2

[<br>](https://fermi-dex.gitbook.io/fermi-dex-docs/technical-overview/technical-overview)The code for the on-chain programs is open-sourced here: <https://github.com/Fermi-DEX/Fermi-orderbook/blob/main/programs/fermi-dex/src/lib.rs>

{% hint style="info" %}
Solana Devnet often undergoes ledger resets - The transactions here are up to date as of 13th october, 2023. In case you are not able to locate them on the links below, please contact us and we will update them.
{% endhint %}

The script for easily testing these features can be found in our SDK, under [examples/CMLA.ts](https://github.com/Fermi-DEX/fermi-sdk/blob/main/examples/CMLA.ts)\
\
**On-chain Logic**

{% hint style="info" %}
Note: SolanaFM links sometimes auto-redirect to mainnet. If you get the error “No transaction found” on any of the links below, simply manually switch to “Solana Devnet” using the button on the top right, and you will see the transaction.
{% endhint %}

To understand the on-chain logic that enables cross-market liquidity abstraction, please check out our docs - specifically, the technical overview with code snippets: <https://fermi-dex.gitbook.io/fermi-dex-docs/technical-overview/technical-overview>\
\
**Testing**\
After initial configuration (as per the[ SDK README](https://github.com/Fermi-DEX/fermi-sdk/blob/main/README.md)), full testing of the functionality on Devnet can be done via the following command:\
\
`npx ts-node ./examples/CMLA.ts` \
\
In CLMA.ts we demonstrate how a market maker (Alice) can now use a single deposit of 1000 USDC to open orders on two (or more) different markets on Fermi DEX. Later when a taker (Bob) fills one of the orders, we show how he is able to finalize the order, settling the funds on a just-in-time basis. All of the following steps are contained in the test file above.<br>

Program ID on devnet: 4jnBbnBjuJB4Qpv7YzBdqb56wEDgC4cVB6uzUzEGMsiH

Alice :  5TwNSHaijveFjz9pD1qKXvqoU9dRgEAnCWKM8LcbQQ8M

Bob :  D1MCk3t8B6Cp1GnUnNkKBtMS5iXG4FEq4U3yNvJgtdDz

**Setup Steps:**\
We’ll create two markets, for USDC/WSOL and USDC/X in this example.

1. Create tokens\
   Create 3 token mint addresses for USDC, WSOL, X.\
   USDC Mint: 2PHGhotqqPpneGatrEYxQKkNmPwh4JBfuvWzevRPt5kc

wSOL Mint: HZRxKzLSZwP4DC8fHuoTtNBoBhXhDJQhhScvpzE5un84

X Token Mint: 7ewB2zr6WiSHzPMhSVKk8nJbJrrCK1wEC8hrSEbLYbeV

2. Initialize markets on fermi DEX (one time)\
   Based on the above token mints, we will initialize two markets on fermi dex: USDC -wSOL and USDC - X. Store the constants (note, these will be readily available for markets deployed on mainnet - see appendix).

Initialise Market-1 (USDC - wSOL) transaction: <https://solana.fm/tx/3HgtERccJwC5Vje7zwjhu7vhBY9WArktxA9pM5RrWBHHp1G4Eaita2Gnat24Qyjg1SiajpTDckQZD4B35VJVbH1K?cluster=devnet-qn1> &#x20;

Market 1 PDA: 5rY6TdgSHz4vFQgSoK6jpp3uCCmusAHZ98oA9wDKKviX<br>

Initialise Market-2 (USDC-X) transaction: <https://solana.fm/tx/5KNfLejXARZc9XGwvDTwZHNmi6ZEesqA3jqyRG9uQj16pjJ3fYCEaWkUKDWo6oHUrRjUaJaegwJc2QEPgDztyYCa>[?cluster=devnet-solana](https://solana.fm/tx/nQHNTAKHFQX6pzN6Utrn8GkPtMPjg12LGAwbnd2GzsNuM8gdDgJgfVSqY8mNL5e6JSSkAP5DyydZtc3sQqfWhMw?cluster=devnet-solana) &#x20;

Market 2 PDA: Hs9abWTkcsRxBWEEusv8qegfmxPfYzU9soRF1fLQqELi

3. Airdrop tokens to Alice and Bob. We airdrop 1000 USDC to Alice, and 1 X to Bob.

**Steps Executed**

1. Alice creates a new Bid, agreeing to pay 1000 USDC for 50 wSOL.

Transaction (Bid) on Market-1: <https://solana.fm/tx/2ztH9iZ1eTbDkMtTsGm6iSUq48XPm1VQX9wEcaM6TFmQP17XueJXbsSjEyJpx43Tz1Qz3bTy7DjtJwWPuwe9uqzS?cluster=devnet-qn1>&#x20;

2. Alice uses the same liquidity to create a bid on another market, agreeing to pay 100,000 USDC for 1 X Token.

Transaction (Bid) on Market-2: <https://solana.fm/tx/5jwjzA2xnH2dcBCK3yMX8N5mN2wzuF2MhxSfmS63z4zbftckjUYXy6S89BEkhwScquXCUZRh4HSFzZuuJwmcQfiT?cluster=devnet-qn1>&#x20;

3. After some time, Bob (a taker) wishes to sell his X tokens, agreeing to sell 1 X token for 100,000 USDC:

Transaction (Ask) on Market-2: <https://solana.fm/tx/3nVNMCX6exEn5RT6ipwNG2JL1YfahF7cMxeLALR1H4rJ93CaKVDutNxdQLAAGrZZk9aBmU2buSsLmGRaGeMW9YsC?cluster=devnet-qn1>

4. Bob’s order is matched with Alice's order (as can be seen in the eventQ). Now Bob can call “finalize\_bid” and “finalize\_ask”, to transfer and settle the funds on a just-in-time basis. In finaliseBid, 1 USDC is transferred from Alice to the market vault. In FinaliseAsk, 1 X is transferred from Bob to the market vault. Thereafter, appropriate accounting changes representing settlement are made.

FinaliseBid transaction: <https://solana.fm/tx/28Ec2UL2HyTQgWkcmwSKZMt5f8WwFcUUTX1yNqVGJ5z171XxdFW3r2WkmueY4s5do2ZuA8srYdtAYGgcTHpskZFg?cluster=devnet-qn1>\
FinaliseAsk transaction: <https://solana.fm/tx/4fJFRE5sBmmHNp65paT2e4M2P4hj6UwJrfUXxehkYPkhyC4q9nmTN5doDGwSNGYxfrptbFu8fXmPVeWRAsjnGZzx?cluster=devnet-qn1>&#x20;

5. After executing finalize successfully, the post-trade balances of Alice and Bob are updated in their openorders accounts.They may now withdraw these funds or utilize them in other trades. \[See below on how to withdraw]

**Replication**

We provide a testing script to replicate the above scenario on devnet.We’ve provided comments in the script as per the above steps, to clarify what happens at each stage.

**Testing**\
After initial configuration (as per the[ SDK README](https://github.com/Fermi-DEX/fermi-sdk/blob/main/README.md)), full testing of the functionality on Devnet can be done via the following command:\
\
npx ts-node ./examples/CMLA.ts \
\
\
**Supplementary Features**\
In combination with the core CMLA functionalit,y we provide this supplementary documentation regarding Order Cancellation, Deposit Funds, & Withdraw Funds capabilities.\
\
**Functions and logic**\
We provide 6 additional user facing function in our program, along with appropriate tests:\
\
In the following demo transactions, here are the relevant accounts on Devnet:\
User (Alice): 5TwNSHaijveFjz9pD1qKXvqoU9dRgEAnCWKM8LcbQQ8M\
PCTokenMint: E1HZo6mT5VxcaiXQ7QGFwfgp2uqZJ6mJdcLP4yC8GoQU\
CoinMint: GXMh94is6GfF4YDYAhxnZi6eEgvNHSfKi3sECaYSGpQq\
Market (Vaults): BcyN6WopDK6zx9JZHrNkvuh587rhCwsy8pXfpYxzxMHL\
\
**Cancel\_Bid and Cancel\_Ask**\
\
These functions can be called by the creator of any order on the orderbook. After verifying the authority (signer) is indeed the owner, the program proceeds to find and remove the specific order (as represented by a unique OrderId) from the bidsPda or asksPda, as the case may be. This can be verified by viewing the bids/Asks PDA before & after the invocation of cancel - after cancel, that specific orderId will no longer be seen in the respective PDA.\
\
Further, the orders are also removed from the users’ openorders struct. No orders will be matched to this order after cancellation.&#x20;

Example transaction -\
CancelBid:  <https://solana.fm/tx/TVimeadSKonQGjGcP4cZJ8GcVUax5HqGwJtrGk9q92ufeRZgQSSuJ2Dbf9xfNoyvFdZ3JdzAmagmDF8THccVkVv?cluster=devnet-solana>&#x20;

CancelAsk:\
<https://solana.fm/tx/2iXv9XgDmuyhTYFXCLFhXDMpG6g3NbHisoSXHrjTBtVHHpQ2o4fynfVKYUabk3qzk52y1oV651hYDWhJsfdEM7QG?cluster=devnet-solana>&#x20;

**Deposit\_PC and Deposit\_Coin**

These functions serve as an additional way for users to deposit funds at their convenience, in advance of finalization, for current or future trades. Funds deposited in this manner are always withdrawable, until finalization has been successfully invoked for both counterparties on an order using these funds - and in that case, the trade token becomes instantly available for withdrawal. Thus, at no point are the funds locked and not withdrawable.

\
Example transaction -&#x20;

Deposit\_PC:<https://solana.fm/tx/3N4UayjDzCzVM2bQt6q4CaM41h3hAJYxxJFo3s3apGPx33sBt7xZbYoZBB9ux8jHhaJBjLaHCQfLX5T8GkocNQry>[?cluster=devnet-solana](https://solana.fm/tx/2iXv9XgDmuyhTYFXCLFhXDMpG6g3NbHisoSXHrjTBtVHHpQ2o4fynfVKYUabk3qzk52y1oV651hYDWhJsfdEM7QG?cluster=devnet-solana) \
Deposit\_Coin: <https://solana.fm/tx/2x9y8ohfWNEVW38Wa16H4gfyi3bVbgoHkXkrR4HBQS6iVjXqtqhvZzu9mNMntv5BPPPSPJe9esSu1RyQLjjUjjaX>[?cluster=devnet-solana](https://solana.fm/tx/2iXv9XgDmuyhTYFXCLFhXDMpG6g3NbHisoSXHrjTBtVHHpQ2o4fynfVKYUabk3qzk52y1oV651hYDWhJsfdEM7QG?cluster=devnet-solana) \ <br>

**Withdraw\_PC and Withdraw\_Coin**

These functions are designed to transfer funds from the pc\_vault and coin\_vault to the user’s wallet. First, we validate that the user has enough balance in his openorders account to fulfil the withdrawal request. If so, we proceed with the withdrawal, using the market as authority for the transfer (as the vaults are PDAs owned by the market).\
\
Example Transactions:\
WithdrawCoin:\
<https://solana.fm/tx/vnvK74b5Awb6WjZgHzDGtbiHAVAxGpyNETKVnyuPk4o9wr7R3fgMZYrBb4C6eWn89c6qdKs3E8G1Cvhqq8ogaTd>[?cluster=devnet-solana](https://solana.fm/tx/2iXv9XgDmuyhTYFXCLFhXDMpG6g3NbHisoSXHrjTBtVHHpQ2o4fynfVKYUabk3qzk52y1oV651hYDWhJsfdEM7QG?cluster=devnet-solana)\
WithdrawPC: <https://solana.fm/tx/5uTYP7qw35tC8qr98aourXF2AAfdHHq1DLa5K8dKHykuFgEpK2bUDauwSr822axAR5Ab9LcWLqvSjf5GdsewnBxe>[?cluster=devnet-solana](https://solana.fm/tx/2iXv9XgDmuyhTYFXCLFhXDMpG6g3NbHisoSXHrjTBtVHHpQ2o4fynfVKYUabk3qzk52y1oV651hYDWhJsfdEM7QG?cluster=devnet-solana)\ <br>

\ <br>


---

# 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/technical-overview/demo-fermi-v0.2.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.
