Adding Priority Fees to a CLMM Close Position in Solana using Raydium’s SDK v2
Raydium, a popular decentralized exchange (DEX) platform, has provided its SDK v2 for developing custom trading applications. One common scenario where users may need to implement priority fees is when closing out positions that require liquidity at or above the market price. This article will guide you through adding priority fees to a CLMM (Closing Market Maker) close position script in Solana using Raydium’s SDK v2.
Prerequisites
- Familiarity with Raydium’s SDK and SOL programming language
- Basic understanding of trading logic and priority fees
Step 1: Create a New Script
Create a new file for your custom script, e.g., clmm_close_position.py
. This file will contain the logic to close out positions using Raydium’s SDK v2.
import { sdk } from 'raydium-sdk';
/**
* CLMM close position script.
*
* @param {SDK} sdk - Raydium SDK instance.
*/
export async function closePosition(position) {
// Get the current market maker (CM) and the closing price.
const cm = position.cm;
const closingPrice = position.price;
// Check if the CM has priority fees enabled.
if (cm.hasPriorityFeesEnabled()) {
// Calculate the fee for the transaction based on the market maker's bid/ask spread.
const fee = (closingPrice - cm.bid) / cm.ask * 0.0001; // 100 SOL
// Close the position using Raydium's SDK v2.
await sdk.closePosition(cm, closingPrice, {
amount: 1, // Close out one unit of the position
fee,
});
} else {
// If priority fees are not enabled, close out the position as usual.
await sdk.closePosition(cm, closingPrice);
}
}
Step 2: Register the Script with Raydium
To use the script, you need to register it with Raydium. This involves creating a new Script
instance and registering your custom function.
import { Script } from 'raydium-sdk';
// Create a new script instance.
const clmmClosePosition = new Script();
// Register the script with Raydium.
clmmClosePosition.register('closePosition', closePosition);
Step 3: Call the Script in Your Trade Logic
To use the CLMM close position script, you can call it from your trade logic in Solana. Here’s an example:
import { sdk } from 'raydium-sdk';
// Get the current market maker (CM) and the closing price.
const cm = await sdk.getMarketMaker();
const closingPrice = await sdk.getClosePrice(cm);
// Call the CLMM close position script to close out the position.
await clmmClosePosition.closePosition(closingPrice);
With these steps, you can now use Raydium’s SDK v2 and Solana programming language to add priority fees to a CLMM close position in your custom trading application.