Skip to main content

Method

async executeSwap(params: SwapParams): Promise<SwapResult>
Executes a complete token swap by fetching swap data, signing the transaction, and broadcasting it to the blockchain. This is a high-level method that handles the entire swap process.

Parameters

params
SwapParams
required
Swap request parameters (same as getSwapData)

Response

success
boolean
Whether the swap was successful
transactionId
string
Transaction hash/signature
explorerUrl
string
Block explorer URL for the transaction
details
object
Swap execution details

Configuration Requirements

EVM Chains

Requires EVM wallet configuration:
const client = new OKXClient({
  apiKey: 'your-api-key',
  secretKey: 'your-secret-key',
  apiPassphrase: 'your-passphrase',
  projectId: 'your-project-id',
  evm: {
    wallet: evmWallet // EVMWallet instance
  }
});

Solana

Requires Solana wallet configuration:
const client = new OKXClient({
  apiKey: 'your-api-key',
  secretKey: 'your-secret-key',
  apiPassphrase: 'your-passphrase',
  projectId: 'your-project-id',
  solana: {
    wallet: solanaWallet, // Wallet instance
    computeUnits: 300000
  }
});

Example

import { OKXClient } from '@okxweb3/okx-api';
import { EVMWallet } from '@okxweb3/okx-api/evm';

const evmWallet = new EVMWallet('your-private-key');

const client = new OKXClient({
  apiKey: 'your-api-key',
  secretKey: 'your-secret-key',
  apiPassphrase: 'your-passphrase',
  projectId: 'your-project-id',
  evm: { wallet: evmWallet }
});

try {
  const result = await client.dex.executeSwap({
    chainIndex: '1',
    fromTokenAddress: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
    toTokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
    amount: '1000000000000000000', // 1 ETH
    slippagePercent: '0.005',
    userWalletAddress: evmWallet.getAddress()
  });

  console.log('Swap successful!');
  console.log('Transaction:', result.transactionId);
  console.log('Explorer:', result.explorerUrl);
  console.log('Details:', result.details);
} catch (error) {
  console.error('Swap failed:', error.message);
}

Response Example

{
  "success": true,
  "transactionId": "0x1234567890abcdef...",
  "explorerUrl": "https://web3.okx.com/explorer/ethereum/tx/0x1234567890abcdef...",
  "details": {
    "fromToken": {
      "symbol": "ETH",
      "amount": "1000000000000000000",
      "decimal": "18"
    },
    "toToken": {
      "symbol": "USDC",
      "amount": "2487500000",
      "decimal": "6"
    },
    "priceImpact": "0.15"
  }
}

Workflow

  1. Calls getSwapData() to fetch transaction data
  2. Retrieves network configuration for the chain
  3. Creates appropriate executor (EVM, Solana, or Sui) using SwapExecutorFactory
  4. Executor signs and broadcasts the transaction
  5. Returns swap result with transaction details

Error Handling

try {
  const result = await client.dex.executeSwap(params);
} catch (error) {
  if (error.message.includes('slippage')) {
    // Handle slippage validation error
  } else if (error.message.includes('wallet')) {
    // Handle wallet configuration error
  } else {
    // Handle other errors
  }
}

Build docs developers (and LLMs) love