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
Swap request parameters (same as getSwapData) Chain identifier (e.g., “1” for Ethereum, “501” for Solana)
Contract address of the token to swap from
Contract address of the token to swap to
Amount to swap (in token’s smallest unit)
Maximum acceptable slippage (0-1). Required if autoSlippage is false.
Enable automatic slippage calculation
Maximum slippage when autoSlippage is enabled
Address to receive swapped tokens
Response
Whether the swap was successful
Transaction hash/signature
Block explorer URL for the transaction
Swap execution details Destination token details (same structure as fromToken)
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
Calls getSwapData() to fetch transaction data
Retrieves network configuration for the chain
Creates appropriate executor (EVM, Solana, or Sui) using SwapExecutorFactory
Executor signs and broadcasts the transaction
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
}
}