Skip to main content
Get raw Solana swap instructions that can be manually constructed and signed. This method provides more control over transaction construction compared to executeSwap.

Method signature

client.dex.getSolanaSwapInstruction(params: SwapParams): Promise<APIResponseSingle<SolanaSwapInstructionData>>

Parameters

chainIndex
string
required
Chain identifier. For Solana mainnet: "501"
fromTokenAddress
string
required
Source token address (SPL token or So11111111111111111111111111111111111111112 for SOL)
toTokenAddress
string
required
Destination token address (SPL token or wrapped SOL)
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 conditionsWhen enabled, maxAutoSlippagePercent must also be provided.
maxAutoSlippagePercent
string
Maximum slippage when using auto slippage (e.g., "0.05" for 5%)Required when 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 APIResponseSingle<SolanaSwapInstructionData> containing:
code
string
Response code ("0" for success)
msg
string
Response message
data
SolanaSwapInstructionData
Swap instruction data

Example

import { OKXDexClient } from '@okx-dex/okx-dex-sdk';

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!
});

// Get Solana swap instructions
const instructionData = await client.dex.getSolanaSwapInstruction({
  chainIndex: '501',
  fromTokenAddress: 'So11111111111111111111111111111111111111112', // SOL
  toTokenAddress: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', // USDC
  amount: '1000000000', // 1 SOL
  slippagePercent: '0.005',
  userWalletAddress: walletAddress,
  computeUnitPrice: '1000'
});

console.log('Instructions:', instructionData.data.instructionLists);
console.log('Expected output:', instructionData.data.routerResult.toTokenAmount);

Use cases

Custom transaction construction

Build complex transactions with additional instructions

Multi-instruction transactions

Combine swap with other Solana operations

Advanced signing

Use hardware wallets or custom signing logic

Transaction inspection

Review instructions before execution

Error handling

try {
  const instructionData = await client.dex.getSolanaSwapInstruction({
    chainIndex: '501',
    fromTokenAddress,
    toTokenAddress,
    amount,
    slippagePercent: '0.005',
    userWalletAddress
  });
  
  // Manually construct and sign transaction
  // ...
  
} catch (error: any) {
  if (error.message.includes('slippage')) {
    console.error('Invalid slippage configuration');
  } else if (error.message.includes('Empty instruction data')) {
    console.error('No swap route available');
  } else {
    console.error('Failed to get instructions:', error.message);
  }
}

See also

Build docs developers (and LLMs) love