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 (account units):', 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 (account units):', quote.fee)
console.log('Required token in (base units):', quote.tokenInAmount)With an ERC‑4337 account, pass the optional second argument to preview the fee for a specific paymaster, sponsorship, or native-fee configuration:
const quote = await swapAA.quoteSwap({
tokenIn: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
tokenInAmount: 1000000n
}, {
paymasterToken: {
address: '0xdAC17F958D2ee523a2206206994597C13D831ec7'
}
})Fee estimation
Read quote.fee in the account's fee units: native wei for a standard EVM account, token base units for token-paymaster fees, or zero for sponsorship. The quote excludes any separate input-token approval. quoteSwap() does not enforce swapMaxFee.
The following examples use a standard EVM account, so the cap is in wei:
const quote = await swapProtocol.quoteSwap({
tokenIn: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
tokenInAmount: 1000000n
})
const maxFee = 200000000000000n
console.log('Quoted fee (wei):', quote.fee, 'cap:', maxFee)Compare the estimate with an exclusive cap and pass that cap plus an accepted output floor to swap(). Execution obtains a fresh quote and rejects a fee equal to or above swapMaxFee. With beta.9, minAmountOut also rejects an output below your floor. The following example allows a 1% reduction in the previewed output; obtain the user's approval of that tolerance and recipient before sending:
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,
minAmountOut: quote.tokenOutAmount * 9900n / 10000n
}, { swapMaxFee: maxFee })
console.log('Swap hash:', result.hash)
}Each call obtains a fresh rate. There is no retained quote or route, and a fee cap alone does not constrain token output. For SELL, the builder receives minAmountOut as its destination amount. BUY retains its requested exact output. Returned token amounts describe the new rate rather than settled balance changes; confirm the transaction before reporting receipt.