Get Swap Quotes
Estimate fees and amounts with quoteSwap before executing a swap.
This guide shows how to quote before swapping and use quotes for fee estimation. Quotes use quoteSwap(), which works with read-only accounts as well as signing accounts.
Quote before swapping
You can preview fee and token amounts for the same parameters you would pass to swap() using quoteSwap():
const quote = await swapProtocol.quoteSwap({
tokenIn: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
tokenInAmount: 1000000n
})
console.log('Estimated fee (wei):', quote.fee)
console.log('Tokens in (base units):', quote.tokenInAmount)
console.log('Tokens out (base units):', quote.tokenOutAmount)You can quote an exact-output style trade the same way by passing tokenOutAmount instead of tokenInAmount to quoteSwap():
const quote = await swapProtocol.quoteSwap({
tokenIn: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
tokenOutAmount: 500000000000000000n
})
console.log('Estimated fee (wei):', quote.fee)
console.log('Required token in (base units):', quote.tokenInAmount)Fee estimation
You can read quote.fee from quoteSwap() as the estimated total swap fee in wei before calling swap():
const quote = await swapProtocol.quoteSwap({
tokenIn: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
tokenInAmount: 1000000n
})
const maxFee = 200000000000000n
console.log('Quoted fee (wei):', quote.fee, 'cap:', maxFee)You can compare that estimate to swapMaxFee on VeloraProtocolEvm and only then call swap() when the quote is within your cap:
const maxFee = 200000000000000n
const quote = await swapProtocol.quoteSwap({
tokenIn: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
tokenInAmount: 1000000n
})
if (quote.fee <= maxFee) {
const result = await swapProtocol.swap({
tokenIn: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
tokenInAmount: 1000000n
})
console.log('Swap hash:', result.hash)
}On-chain conditions can change between quote and execution. The executed swap() may still differ slightly from the last quoteSwap() result.