Skip to main content

Overview

The OKXDexClient class is the primary entry point for the OKX DEX SDK. It handles authentication, configuration, and provides access to DEX and Bridge APIs.

Constructor

constructor(config: OKXConfig)
Creates a new instance of the OKX DEX client with the provided configuration.

Configuration Parameters

apiKey
string
required
Your OKX API key for authentication
secretKey
string
required
Your OKX secret key for signing requests
apiPassphrase
string
required
Your OKX API passphrase for authentication
projectId
string
required
Your OKX project ID
baseUrl
string
default:"https://web3.okx.com"
Base URL for OKX API endpoints
timeout
number
default:"30000"
Request timeout in milliseconds
maxRetries
number
default:"3"
Maximum number of retry attempts for failed requests
networks
NetworkConfigs
Chain-specific configuration settings. See NetworkConfigs below.
solana
SolanaConfig
Solana-specific configuration. See SolanaConfig below.
sui
SuiConfig
Sui-specific configuration. See SuiConfig below.
evm
EVMConfig
EVM-specific configuration. See EVMConfig below.

Properties

dex
DexAPI
Instance of DexAPI for performing DEX operations like swaps, quotes, and approvals
bridge
BridgeAPI
Instance of BridgeAPI for cross-chain bridge operations

Type Definitions

NetworkConfigs

interface NetworkConfigs {
  [chainIndex: string]: ChainConfig;
}
Configuration object mapping chain indices to chain-specific settings.

ChainConfig

id
string
required
Chain identifier
explorer
string
required
Block explorer URL for the chain
defaultSlippage
string
required
Default slippage tolerance for swaps on this chain
maxSlippage
string
required
Maximum allowed slippage tolerance
computeUnits
number
Compute units for transaction execution (Solana)
confirmationTimeout
number
Timeout for transaction confirmation in milliseconds
maxRetries
number
Maximum retry attempts for chain-specific operations
dexContractAddress
string
DEX contract address for the chain

SolanaConfig

wallet
Wallet
required
Solana wallet instance for signing transactions
computeUnits
number
Compute units allocated for Solana transactions
maxRetries
number
Maximum retry attempts for Solana operations

SuiConfig

privateKey
string
required
Private key for Sui wallet
walletAddress
string
required
Sui wallet address
connection
object
Sui network connection settings
connection.rpcUrl
string
required
RPC endpoint URL for Sui network
connection.wsEndpoint
string
WebSocket endpoint for real-time updates

EVMConfig

wallet
EVMWallet
EVM wallet instance for signing transactions on EVM-compatible chains

Examples

Basic Initialization

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

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

// Access DEX API
const quote = await client.dex.getQuote({
  chainIndex: '1',
  fromTokenAddress: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
  toTokenAddress: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
  amount: '1000000000000000000',
  slippagePercent: '0.5'
});

// Access Bridge API
const bridgeQuote = await client.bridge.getQuote({
  fromChainIndex: '1',
  toChainIndex: '56',
  fromTokenAddress: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
  toTokenAddress: '0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d',
  amount: '1000000'
});

Advanced Configuration

import { OKXDexClient } from '@okxweb3/dex-sdk';
import { Keypair } from '@solana/web3.js';

const solanaKeypair = Keypair.fromSecretKey(
  new Uint8Array([/* your secret key */])
);

const client = new OKXDexClient({
  apiKey: 'your-api-key',
  secretKey: 'your-secret-key',
  apiPassphrase: 'your-passphrase',
  projectId: 'your-project-id',
  timeout: 60000,
  maxRetries: 5,
  solana: {
    wallet: {
      getAddress: () => solanaKeypair.publicKey.toBase58(),
      signTransaction: async (tx) => {
        tx.sign(solanaKeypair);
        return tx;
      }
    },
    computeUnits: 200000,
    maxRetries: 3
  },
  networks: {
    '501': {
      id: 'solana',
      explorer: 'https://solscan.io',
      defaultSlippage: '0.5',
      maxSlippage: '5.0',
      computeUnits: 200000,
      confirmationTimeout: 60000
    }
  }
});

Custom Base URL

const client = new OKXDexClient({
  apiKey: 'your-api-key',
  secretKey: 'your-secret-key',
  apiPassphrase: 'your-passphrase',
  projectId: 'your-project-id',
  baseUrl: 'https://custom-proxy.example.com'
});

See Also

Build docs developers (and LLMs) love