getSwapQuote
Gets a swap quote for exchanging tokens across chains or within the same chain. The response includes pricing, fees, estimated execution time, and a signingPayload for on-chain execution via executeSwapTransaction.
Usage
import { getSwapQuote } from '@dynamic-labs-sdk/client';
const quote = await getSwapQuote({
from: {
address: '0x742d35Cc6634C0532925a3b844Bc9e7595f7ABCD',
chain: 'EVM',
networkId: '1',
tokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
amount: '1000000', // 1 USDC (6 decimals)
},
to: {
address: '0x742d35Cc6634C0532925a3b844Bc9e7595f7ABCD',
chain: 'EVM',
networkId: '137',
tokenAddress: '0x0000000000000000000000000000000000000000', // Native MATIC
},
slippage: 0.005,
});
console.log('Signing payload ready:', !!quote.signingPayload);
Parameters
| Parameter | Type | Description |
|---|
from | object | Source token details. |
from.address | string | The wallet address sending the tokens. |
from.chain | Chain | The source chain (e.g., 'EVM', 'SOL'). |
from.networkId | string | The source network ID (e.g., '1' for Ethereum, '137' for Polygon). |
from.tokenAddress | string | The source token contract address. Use the zero address for native tokens on EVM and SOL. |
from.amount | string (optional) | The source token amount including all decimals (e.g., '1000000' for 1 USDC). Mutually exclusive with to.amount. |
to | object | Destination token details. |
to.address | string | The wallet address receiving the tokens. |
to.chain | Chain | The destination chain. |
to.networkId | string | The destination network ID. |
to.tokenAddress | string | The destination token contract address. |
to.amount | string (optional) | The desired destination amount. Mutually exclusive with from.amount. |
slippage | number (optional) | Maximum allowed slippage as a decimal (e.g., 0.005 for 0.5%). |
order | SwapQuoteOrder (optional) | Route preference: 'FASTEST' prioritizes speed, 'CHEAPEST' minimizes cost. |
maxPriceImpact | number (optional) | Hide routes with price impact above this threshold (e.g., 0.15 for 15%). |
client | DynamicClient (optional) | The Dynamic client instance. Only required when using multiple clients. |
Exactly one of from.amount or to.amount must be provided. Providing both or neither throws an InvalidParamError.
Returns
Promise<SwapQuoteResponse> - The quote response including a signingPayload field for on-chain execution.
Examples
Same-chain swap (USDC to ETH)
import { getSwapQuote } from '@dynamic-labs-sdk/client';
const quote = await getSwapQuote({
from: {
address: walletAccount.address,
chain: 'EVM',
networkId: '1',
tokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
amount: '10000000', // 10 USDC
},
to: {
address: walletAccount.address,
chain: 'EVM',
networkId: '1',
tokenAddress: '0x0000000000000000000000000000000000000000', // ETH
},
slippage: 0.005,
order: 'CHEAPEST',
});
Cross-chain bridge (ETH to Polygon)
import { getSwapQuote } from '@dynamic-labs-sdk/client';
const quote = await getSwapQuote({
from: {
address: walletAccount.address,
chain: 'EVM',
networkId: '1', // Ethereum
tokenAddress: '0x0000000000000000000000000000000000000000',
amount: '1000000000000000000', // 1 ETH (18 decimals)
},
to: {
address: walletAccount.address,
chain: 'EVM',
networkId: '137', // Polygon
tokenAddress: '0x0000000000000000000000000000000000000000',
},
order: 'FASTEST',
});
Quote and execute
import {
getSwapQuote,
executeSwapTransaction,
getSwapStatus,
} from '@dynamic-labs-sdk/client';
const swap = async (walletAccount) => {
// Step 1: Get quote
const quote = await getSwapQuote({
from: {
address: walletAccount.address,
chain: 'EVM',
networkId: '1',
tokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
amount: '5000000', // 5 USDC
},
to: {
address: walletAccount.address,
chain: 'EVM',
networkId: '1',
tokenAddress: '0x0000000000000000000000000000000000000000',
},
slippage: 0.005,
});
// Step 2: Execute
const { transactionHash } = await executeSwapTransaction({
walletAccount,
signingPayload: quote.signingPayload,
});
// Step 3: Check status
const status = await getSwapStatus({
txHash: transactionHash,
from: { chain: 'EVM', networkId: '1' },
to: { chain: 'EVM', networkId: '1' },
});
console.log('Swap status:', status.status);
return { transactionHash, status };
};
Supported Chains
The swap quote supports the following chains (mainnet only):