Skip to main content
Execute a Solana token swap using raw instructions. This method provides more control than executeSwap by allowing you to work with Solana’s instruction-based architecture directly.

Method signature

client.dex.executeSolanaSwapInstructions(params: SwapParams): Promise<SwapResult>

How it works

This method:
  1. Calls getSolanaSwapInstruction to get swap instructions
  2. Constructs a Solana transaction from the instructions
  3. Signs the transaction with your configured wallet
  4. Submits the transaction to the Solana network
  5. Waits for confirmation

Parameters

chainIndex
string
required
Must be "501" for Solana mainnet
fromTokenAddress
string
required
Source token address (SPL token or So11111111111111111111111111111111111111112 for SOL)
toTokenAddress
string
required
Destination token address (SPL token address)
amount
string
required
Amount to swap in smallest unit (lamports for SOL, base units for SPL tokens)
userWalletAddress
string
required
Solana wallet address executing the swap
slippagePercent
string
Maximum acceptable slippage as a decimal (e.g., "0.005" for 0.5%)Either slippagePercent or autoSlippage must be provided.
autoSlippage
boolean
Enable automatic slippage calculation based on market conditions
maxAutoSlippagePercent
string
Maximum slippage when using auto slippage (required if autoSlippage is true)
computeUnitPrice
string
Compute unit price in micro-lamports for priority fees
computeUnitLimit
string
Maximum compute units to use for the transaction

Response

Returns SwapResult containing:
success
boolean
Whether the swap completed successfully
transactionId
string
Solana transaction signature
explorerUrl
string
URL to view the transaction on OKX explorer
details
object
Additional swap details

Prerequisites

You must configure a Solana wallet in your OKXDexClient configuration to use this method.
import { OKXDexClient } from '@okx-dex/okx-dex-sdk';
import { createWallet } from '@okx-dex/okx-dex-sdk/core/wallet';
import { Connection } from '@solana/web3.js';

const connection = new Connection(process.env.SOLANA_RPC_URL!);
const solanaWallet = createWallet(
  process.env.SOLANA_PRIVATE_KEY!,
  connection
);

const client = new OKXDexClient({
  apiKey: process.env.OKX_API_KEY!,
  secretKey: process.env.OKX_SECRET_KEY!,
  apiPassphrase: process.env.OKX_API_PASSPHRASE!,
  projectId: process.env.OKX_PROJECT_ID!,
  solana: {
    wallet: solanaWallet,
    computeUnits: 300000
  }
});

Example

// Execute Solana swap using instruction method
const result = await client.dex.executeSolanaSwapInstructions({
  chainIndex: '501',
  fromTokenAddress: 'So11111111111111111111111111111111111111112', // SOL
  toTokenAddress: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', // USDC
  amount: '500000000', // 0.5 SOL
  slippagePercent: '0.005',
  userWalletAddress: solanaWallet.address,
  computeUnitPrice: '1000' // Priority fee
});

console.log('Swap successful:', result.success);
console.log('Transaction:', result.transactionId);
console.log('Explorer:', result.explorerUrl);

When to use this method

Choose executeSolanaSwapInstructions when you need:
  • More control over transaction construction
  • To add custom instructions before/after the swap
  • To inspect instructions before execution
  • To work with Solana’s native instruction format

Error handling

try {
  const result = await client.dex.executeSolanaSwapInstructions({
    chainIndex: '501',
    fromTokenAddress,
    toTokenAddress,
    amount,
    slippagePercent: '0.005',
    userWalletAddress
  });
  
  if (result.success) {
    console.log('Swap completed:', result.transactionId);
  }
  
} catch (error: any) {
  if (error.message.includes('Solana wallet configuration required')) {
    console.error('Configure solana.wallet in OKXDexClient');
  } else if (error.message.includes('Empty instruction data')) {
    console.error('No swap route found');
  } else if (error.message.includes('slippage')) {
    console.error('Invalid slippage configuration');
  } else {
    console.error('Swap failed:', error.message);
  }
}

Configuration options

Compute units and priority fees

const result = await client.dex.executeSolanaSwapInstructions({
  chainIndex: '501',
  fromTokenAddress,
  toTokenAddress,
  amount,
  slippagePercent: '0.005',
  userWalletAddress,
  computeUnitPrice: '2000',    // Higher priority
  computeUnitLimit: '400000'   // More compute budget
});

Auto slippage

const result = await client.dex.executeSolanaSwapInstructions({
  chainIndex: '501',
  fromTokenAddress,
  toTokenAddress,
  amount,
  autoSlippage: true,
  maxAutoSlippagePercent: '0.05', // 5% max
  userWalletAddress
});

See also

Build docs developers (and LLMs) love