🕹️Demo - Fermi v0.2

DEPRECATED - ONLY FOR REFERENCE A series of transactions demonstrating the current functionality of Fermi DEX on Devnet. Executed via example scripts on the Fermi SDK.

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

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.

The script for easily testing these features can be found in our SDK, under examples/CMLA.ts On-chain Logic

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.

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), 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.

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

  1. 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

Market 1 PDA: 5rY6TdgSHz4vFQgSoK6jpp3uCCmusAHZ98oA9wDKKviX

Initialise Market-2 (USDC-X) transaction: https://solana.fm/tx/5KNfLejXARZc9XGwvDTwZHNmi6ZEesqA3jqyRG9uQj16pjJ3fYCEaWkUKDWo6oHUrRjUaJaegwJc2QEPgDztyYCa?cluster=devnet-solana

Market 2 PDA: Hs9abWTkcsRxBWEEusv8qegfmxPfYzU9soRF1fLQqELi

  1. 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

  1. 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

  1. 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

  1. 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

  1. 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), 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.

Example transaction - CancelBid: https://solana.fm/tx/TVimeadSKonQGjGcP4cZJ8GcVUax5HqGwJtrGk9q92ufeRZgQSSuJ2Dbf9xfNoyvFdZ3JdzAmagmDF8THccVkVv?cluster=devnet-solana

CancelAsk: https://solana.fm/tx/2iXv9XgDmuyhTYFXCLFhXDMpG6g3NbHisoSXHrjTBtVHHpQ2o4fynfVKYUabk3qzk52y1oV651hYDWhJsfdEM7QG?cluster=devnet-solana

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 -

Deposit_PC:https://solana.fm/tx/3N4UayjDzCzVM2bQt6q4CaM41h3hAJYxxJFo3s3apGPx33sBt7xZbYoZBB9ux8jHhaJBjLaHCQfLX5T8GkocNQry?cluster=devnet-solana Deposit_Coin: https://solana.fm/tx/2x9y8ohfWNEVW38Wa16H4gfyi3bVbgoHkXkrR4HBQS6iVjXqtqhvZzu9mNMntv5BPPPSPJe9esSu1RyQLjjUjjaX?cluster=devnet-solana

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 WithdrawPC: https://solana.fm/tx/5uTYP7qw35tC8qr98aourXF2AAfdHHq1DLa5K8dKHykuFgEpK2bUDauwSr822axAR5Ab9LcWLqvSjf5GdsewnBxe?cluster=devnet-solana

Last updated