async function advancedSwap(
wallet: WalletAdapter,
tokenIn: string,
tokenOut: string,
amount: string,
options: {
slippage?: number;
onlyProtocols?: string[];
excludedProtocols?: string[];
} = {}
) {
try {
// Get quote with advanced options
const quote = await getSwapQuote({
chainId: 'solana',
tokenIn,
tokenOut,
amount,
walletAddress: wallet.publicKey!.toString(),
slippage: options.slippage?.toString() || '1',
onlyProtocols: options.onlyProtocols?.join(','),
excludedProtocols: options.excludedProtocols?.join(','),
});
if (quote.error) {
throw new Error(quote.error);
}
console.log('Quote received:', {
estimatedOut: quote.data.estimatedAmountOut,
slippage: quote.data.estimatedSlippage,
requestId: quote.data.requestId,
});
// ... continue with signing and sending
} catch (error) {
console.error('Swap error:', error);
throw error;
}
}
// Usage: Only use Raydium and Orca
advancedSwap(
wallet,
'So11111111111111111111111111111111111111112', // SOL
'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', // USDC
'100000000', // 0.1 SOL
{
slippage: 2,
onlyProtocols: ['raydium-amm', 'orca-whirlpool'],
}
);
// Usage: Exclude specific protocol
advancedSwap(
wallet,
'So11111111111111111111111111111111111111112',
'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
'100000000',
{
excludedProtocols: ['0xSomeFactoryAddress'],
}
);