Run Your First Program#
In your codebase, plug in your private wallet key using Initialize http client
, then run the Get Quote
application.
Initialize HTTP Client#
import { OKXDexClient } from '@okx-dex/okx-dex-sdk';
import 'dotenv/config';
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!,
// Required for executing swaps
solana: {
connection: {
rpcUrl: process.env.SOLANA_RPC_URL!,
confirmTransactionInitialTimeout: 60000},
privateKey: process.env.SOLANA_PRIVATE_KEY!,
walletAddress: process.env.SOLANA_WALLET_ADDRESS!
}
}
);
Get Quote (SOL → USDC)#
async function main() {
try {
// Get a quote
const quote = await client.dex.getQuote({
chainIndex: '501',
// Native token contract address
fromTokenAddress: 'So11111111111111111111111111111111111111112',
toTokenAddress: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
amount: '1000000000',
slippage: '0.1'
});
console.log('Swap Quote:', JSON.stringify(quote, null, 2));
} catch (error) {
console.error('Error:', error);
}
}
main();
Explanation
Key parameter description:
- chainIndex: unique identifier of each public chain. (501 = Solana network)
- slippage: It is recommended to adjust dynamically according to market volatility (range 0.1-1%)
- amount: needs to be converted to the minimum unit of tokens (such as SOL's Lamport accuracy)
Here are additional examples of setting up programs
Table of contents