- Run
solana-test-validator
- Get the deployed programId of the FermiDex program.
- Paste the programId into the
config.json
in fermi-sdk - Now create a new market by running
npx ts-node ./examples/newMarket.example.ts
- Copy paste the generated market constants from the console output to the
config.json
marketConstants field.
You now are ready to interact with the market through various functions available in sdk
import * as FermiDex from "../src
Go to config.json
- Replace
rpcUrl
withhttps://api.devnet.solana.com
or any custom endpoint for devnet - Replace
programId
with address of deployed program on devnet [contact team for latest programID: tg: @dtree33] - Run
npx ts-node ./examples
The Fermi Protocol SDK provides functionalities to interact with the Fermi DEX on Solana. In this tutorial, we will cover the following steps:
- Setting up the environment
- Creating a market
- Airdropping tokens
- Fetching token balances
- Placing orders
- Finalizing orders
Make sure all the keypairs have sol in it , else airdrop some using solana airdrop <sol-amount> <address>
To begin, import the necessary modules and configurations:
import { Connection, PublicKey } from "@solana/web3.js";
import * as FermiDex from "../src";
import { rpcUrl } from "../config.json";
import {markets} from "./markets.ts"
Initialize your connection:
const connection = new Connection(rpcUrl);
Load local keypairs for the owner and two users:
const owner = FermiDex.getLocalKeypair("~/.config/solana/id.json");
const user1 = FermiDex.getLocalKeypair("./test-keypairs/user1/key.json");
const user2 = FermiDex.getLocalKeypair("./test-keypairs/user2/key.json");
To initialize a market:
await FermiDex.initialiseMarket(owner, connection);
Create a client with a authority keypair , connection and specify which market to interact with
/*
Example market :
{
marketPda: "6U2rPcw5vqdc6cpPq4APpNmmdhZe3Qfnv36Wgz4xi94H",
coinMint: "34ma6yJYJ1MANrzYB16iVTaAay8uisw5qUr42Np5Vze6",
pcMint: "HvhQgh71p4Tz7g7bBNbsMCYShpesPt16kG2YtaYFwFE4",
}
*/
const client = new FermiDex.FermiClient({
market:markets[0],
connection,
authority:userKp
})
Airdrop coin and PC tokens
// airdrop coin tokens
await FermiDex.airdropToken({
receiverPk: bobKp.publicKey,
amount: 1000 * (10 ** 9),
connection,
mint: new PublicKey(currentMarket.coinMint),
ownerKp: owner,
});
//airdrop pc tokens
await FermiDex.airdropToken({
receiverPk: bobKp.publicKey,
amount: 1000 * (10 ** 9),
connection,
mint: new PublicKey(currentMarket.pcMint),
ownerKp: owner,
});
To fetch and display the balances for both users:
// Fetch custom token balance of wallet by specifying mint address of token
const cutomTokenBalance = await FermiDex.getTokenBalance(userKp.publicKey,new PublicKey(selectedMarket.pcMint),connection);
// Fetch Pc token balance of current market
const pcTokenBalance = await client.getWalletPcBalance()
// Fetch Coin token balance of current market
const coinTokenBalance = await client.getWalletCoinBalance()
// Fetch open orders account balance
const openOrdersTokenBalances = await client.fetchOpenOrdersAccountBalances();
Place a buy order for bob
and a sell order for alice
:
const buyOrder = await bobClient.placeBuyOrder(30,1)
console.log({buyOrder})
const sellOrder = await aliceClient.placeSellOrder(30,1)
console.log({sellOrder})
Finalizing orders involves matching and executing trades. Here's how to do it:
// Get a map of finalisable orders
const matchedEventsAlice = await aliceClient.getFinalisableOrderMap();
console.log({ matchedEventsAlice });
const matchedOrders = Object.keys(matchedEventsAlice); // Get
const orderIdToFinalise = matchedOrders[0]; // Select which order to finalise
const match = matchedEventsAlice[orderIdToFinalise];
const finaliseSellOrder = await aliceClient.finaliseSellOrder(
aliceKp, // Keypair
match.eventSlot1, // number
match.eventSlot2 // number
);
console.log({ finaliseSellOrder });
const finaliseBuyOrder = await bobClient.finaliseBuyOrder(
bobKp,
match.eventSlot1,
match.eventSlot2
);
console.log({ finaliseBuyOrder });
To fetch asks & bids
const bids = await client.getBids()
const asks = await client.getAsks()
To fetch event queue
const eventQueue = await client.getEventQueue();
To fetch openOrders account
const oo = await client.getOpenOrders()
To cancel order , you have to specify orderId which needs to be cancelled
// orderIdToCancel : string
const cancelBuy = await client.cancelBuyOrder(orderIdToCancel);
const cancelSell = await client.cancelSellOrder(orderIdToCancel);
To deposit tokens in open orders account from wallet
// amount: number
const depositPc = await client.depositPcTokens(amount);
const depositCoin = await client.depositCoinTokens(amount);
To withdraw tokens from open orders account to wallet
// amount : number
const withdrawPc = await client.withdrawPcTokens(10);
const withdrawCoins = await client.withdrawPcTokens(10);