Solana Swap
The most efficient solution for integrating Solana-based token swaps into your projects.

Overview
Solana Swap provides a streamlined API for executing token swaps on the Solana blockchain. Built and maintained by Solana Tracker, this library offers fast market updates and comprehensive access to multiple Solana DEXs through a unified interface.
Features
- Fast Market Updates: Fastest Solana swap api available.
- Multi-DEX Support: Integrated with major Solana DEXs
- High Performance: Optimized for speed and reliability, even during network congestion
- Developer-Friendly: Simple interface with comprehensive documentation
- Jito Integration: Support for Jito bundles for MEV protection
- Auto Priority Fees: Automatic priority fee calculation
- Custom Fee Support: Add your own fees on swaps
- Percentage-based Swaps: Swap percentages of wallet balance
- Custom Send Endpoints: Support for specialized RPC endpoints (Helius, Nextblock, etc.)
- WebSocket Confirmations: Efficient transaction confirmation via WebSocket
- Detailed Error Handling: Get comprehensive transaction error information
- HTTP/HTTPS Support: Works with both secure and local RPC endpoints
- Connection Keep-Alive: Maintains warm connections for better performance
Supported DEXs
- Raydium
- Raydium CPMM
- Pump.fun
- Pump.fun AMM
- Raydium Launchoad
- MoonIt
- Letsbonk.fun
- Jupiter Studio
- Believe
- Meteora Dynamic
- Moonshot
- Orca
- Jupiter (Private Self-Hosted API)
Installation
npm install solana-swap
yarn add solana-swap
pnpm add solana-swap
Or clone the repository:
git clone https://github.com/YZYLAB/solana-swap.git
Quick Start
import { Keypair } from "@solana/web3.js";
import bs58 from "bs58";
import { SolanaTracker } from "solana-swap";
async function swap() {
const keypair = Keypair.fromSecretKey(
bs58.decode("YOUR_SECRET_KEY_HERE")
);
const solanaTracker = new SolanaTracker(
keypair,
"https://rpc-mainnet.solanatracker.io/?api_key=YOUR_API_KEY",
"YOUR_API_KEY",
false
);
const swapResponse = await solanaTracker.getSwapInstructions(
"So11111111111111111111111111111111111111112",
"4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R",
0.0001,
30,
keypair.publicKey.toBase58(),
0.0005,
);
try {
const txid = await solanaTracker.performSwap(swapResponse, {
sendOptions: { skipPreflight: true },
confirmationRetries: 30,
confirmationRetryTimeout: 500,
lastValidBlockHeightBuffer: 150,
resendInterval: 1000,
confirmationCheckInterval: 1000,
commitment: "processed",
skipConfirmationCheck: false,
useWebSocket: true
});
console.log("Transaction ID:", txid);
console.log("Transaction URL:", `https://solscan.io/tx/${txid}`);
} catch (error) {
console.error("Error performing swap:", error.message);
}
}
swap();
Advanced Features
Auto Amount
Use "auto"
to swap the entire balance of a token:
const swapResponse = await solanaTracker.getSwapInstructions(
fromToken,
toToken,
"auto",
slippage,
payerPublicKey,
priorityFee
);
Percentage-based Swaps
Swap a percentage of your wallet balance:
const swapResponse = await solanaTracker.getSwapInstructions(
fromToken,
toToken,
"50%",
slippage,
payerPublicKey,
priorityFee
);
Auto Priority Fees
Let the API automatically determine the optimal priority fee:
const swapResponse = await solanaTracker.getSwapInstructions(
fromToken,
toToken,
amount,
slippage,
payerPublicKey,
"auto",
false,
{
priorityFeeLevel: "medium"
}
);
Auto Slippage
Let the API automatically determine the optimal slippage:
const swapResponse = await solanaTracker.getSwapInstructions(
fromToken,
toToken,
amount,
"auto",
payerPublicKey,
priorityFee
);
Custom Fees
Add your own fees to swaps:
const swapResponse = await solanaTracker.getSwapInstructions(
fromToken,
toToken,
amount,
slippage,
payerPublicKey,
priorityFee,
false,
{
fee: {
wallet: "YOUR_FEE_WALLET_ADDRESS",
percentage: 0.25
},
feeType: "add"
}
);
Custom Tips
Add custom tips for services like Jito or validators:
const swapResponse = await solanaTracker.getSwapInstructions(
fromToken,
toToken,
amount,
slippage,
payerPublicKey,
priorityFee,
false,
{
customTip: {
wallet: "TIP_WALLET_ADDRESS",
amount: 0.001
}
}
);
Transaction Versions
Choose between versioned transactions (v0) or legacy transactions:
const swapResponse = await solanaTracker.getSwapInstructions(
fromToken,
toToken,
amount,
slippage,
payerPublicKey,
priorityFee,
false,
{
txVersion: "v0"
}
);
Direct Routes Only
Disable multi-hop swaps for direct pool routes only:
const swapResponse = await solanaTracker.getSwapInstructions(
fromToken,
toToken,
amount,
slippage,
payerPublicKey,
priorityFee,
false,
{
onlyDirectRoutes: true
}
);
Custom Send Endpoints
Use specialized RPC endpoints for sending transactions:
await solanaTracker.setCustomSendTransactionEndpoint(
"https://ams-sender.helius-rpc.com/fast"
);
await solanaTracker.setCustomSendTransactionEndpoint(
"https://london.nextblock.io",
{
'Authorization': 'YOUR_API_KEY'
}
);
await solanaTracker.setCustomSendTransactionEndpoint(null);
WebSocket Confirmations
Use WebSocket for more efficient transaction confirmations:
const txid = await solanaTracker.performSwap(swapResponse, {
sendOptions: { skipPreflight: true },
confirmationRetries: 30,
confirmationRetryTimeout: 500,
commitment: "processed",
useWebSocket: true
});
Detailed Error Information
Get comprehensive error details when transactions fail:
const result = await solanaTracker.performSwapWithDetails(swapResponse, {
sendOptions: { skipPreflight: true },
confirmationRetries: 30,
confirmationRetryTimeout: 500,
commitment: "processed",
useWebSocket: true
});
if (result.error) {
console.error("Transaction failed:", result.signature);
console.error("Error type:", result.error.type);
console.error("Error message:", result.error.message);
if (result.error.programId) {
console.error("Program that failed:", result.error.programId);
}
if (result.error.instructionIndex !== undefined) {
console.error("Instruction index:", result.error.instructionIndex);
}
} else {
console.log("Transaction successful:", result.signature);
}
Jito Integration
Execute transactions with Jito bundles for MEV protection:
const txid = await solanaTracker.performSwap(swapResponse, {
sendOptions: { skipPreflight: true },
confirmationRetries: 30,
confirmationCheckInterval: 500,
commitment: "processed",
jito: {
enabled: true,
tip: 0.0001,
},
});
Debug Mode
Enable debug logging for troubleshooting:
const solanaTracker = new SolanaTracker(keypair, rpc, apiKey, true);
const txid = await solanaTracker.performSwap(swapResponse, {
debug: true,
});
solanaTracker.setDebug(true);
Full Example with All Features
const solanaTracker = new SolanaTracker(
keypair,
"https://api.mainnet-beta.solana.com",
"YOUR_API_KEY",
true
);
await solanaTracker.setCustomSendTransactionEndpoint(
"https://mainnet.block-engine.jito.wtf/api/v1/transactions",
{ 'Authorization': 'Bearer YOUR_TOKEN' }
);
const swapResponse = await solanaTracker.getSwapInstructions(
"So11111111111111111111111111111111111111112",
"4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R",
"50%",
"auto",
keypair.publicKey.toBase58(),
"auto",
false,
{
priorityFeeLevel: "high",
fee: {
wallet: "YOUR_FEE_WALLET_ADDRESS",
percentage: 0.5
},
customTip: {
wallet: "TIP_WALLET_ADDRESS",
amount: 0.001
},
feeType: "add",
txVersion: "v0",
onlyDirectRoutes: false
}
);
const result = await solanaTracker.performSwapWithDetails(swapResponse, {
sendOptions: { skipPreflight: true },
confirmationRetries: 30,
confirmationRetryTimeout: 500,
commitment: "processed",
useWebSocket: true
});
if (result.error) {
console.error("Swap failed:", result.error);
} else {
console.log("Swap successful:", result.signature);
}
solanaTracker.destroy();
API Reference
Constructor
new SolanaTracker(
keypair: Keypair,
rpc: string,
apiKey?: string,
debug?: boolean
)
getSwapInstructions
getSwapInstructions(
from: string,
to: string,
fromAmount: number | string | "auto",
slippage: number | "auto",
payer: string,
priorityFee?: number | "auto",
forceLegacy?: boolean,
additionalOptions?: {
priorityFeeLevel?: "min" | "low" | "medium" | "high" | "veryHigh" | "unsafeMax",
fee?: { wallet: string; percentage: number },
customTip?: { wallet: string; amount: number },
feeType?: "add" | "deduct",
txVersion?: "v0" | "legacy",
onlyDirectRoutes?: boolean
}
): Promise<SwapResponse>
performSwap
performSwap(
swapResponse: SwapResponse,
options?: {
sendOptions?: SendOptions,
confirmationRetries?: number,
confirmationRetryTimeout?: number,
lastValidBlockHeightBuffer?: number,
resendInterval?: number,
confirmationCheckInterval?: number,
commitment?: Commitment,
skipConfirmationCheck?: boolean,
useWebSocket?: boolean,
debug?: boolean,
jito?: {
enabled: boolean,
tip: number
}
}
): Promise<string>
performSwapWithDetails
performSwapWithDetails(
swapResponse: SwapResponse,
options?: PerformSwapOptions
): Promise<{
signature: string;
error?: {
type: "InstructionError" | "InsufficientFunds" | "AccountNotFound" | "ProgramError" | "Unknown";
message: string;
instructionIndex?: number;
programId?: string;
rawError?: any;
}
}>
setCustomSendTransactionEndpoint
setCustomSendTransactionEndpoint(
endpoint: string | null,
headers?: Record<string, string>
): Promise<void>
Additional Methods
updateRpcEndpoint(rpc: string): void
getCustomSendEndpoint(): string | null
setDebug(enabled: boolean): void
destroy(): void
getTransactionDetails(signature: string): Promise<ParsedTransactionWithMeta | null>
getRate(
from: string,
to: string,
amount: number | string | "auto",
slippage: number | "auto"
): Promise<RateResponse>
Example Projects
Using this library in production? Let us know to be featured here.
CommonJS Usage
For projects using CommonJS:
const { SolanaTracker } = require("solana-swap");
Pricing
Our standard fee is 0.5% on successful transactions. For high-volume applications, we offer discounted rates (as low as 0.1%) for qualified projects.
Contact
For business inquiries or volume discounts:
Documentation
For full documentation, visit our API Docs.
License
MIT